Skip to content
This repository was archived by the owner on May 22, 2020. It is now read-only.

Commit 0e81134

Browse files
author
Maël Nison
committed
First commit
0 parents  commit 0e81134

17 files changed

+150359
-0
lines changed

.babelrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": ["react", ["env", {
3+
"targets": {
4+
"browsers": ["last 2 versions"]
5+
}
6+
}]],
7+
"plugins": [
8+
"transform-class-properties",
9+
"transform-decorators-legacy"
10+
]
11+
}

.eslintrc.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"parser": "babel-eslint",
3+
"plugins": [
4+
"prettier",
5+
"react"
6+
],
7+
"rules": {
8+
"prettier/prettier": [ "error", {
9+
"singleQuote": true,
10+
"trailingComma": "all",
11+
"bracketSpacing": false,
12+
"printWidth": 120,
13+
"parser": "babylon"
14+
} ]
15+
}
16+
}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist
2+
node_modules
3+
yarn-error.log
4+
.pnp*

.yarnrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
plugnplay-experimental true

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Plug'n'play sample app
2+
3+
This app showcase a wide range of popular packages, and show that Plug'n'play has no issue working with them. Feel free to open PRs to add more, even if they would make little sense in a classic application!
4+
5+
The packages currently configured are:
6+
7+
- Babel
8+
- ESLint
9+
- Gulp
10+
- Http-server
11+
- Jest
12+
- Prettier
13+
- React
14+
- Uglifyjs
15+
- Webpack

gulpfile.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const gulp = require(`gulp`);
2+
const gulpif = require(`gulp-if`);
3+
const uglify = require(`gulp-uglify`);
4+
const webpack = require(`webpack-stream`);
5+
6+
gulp.task('default', function() {
7+
8+
return gulp.src(`src/app.js`)
9+
.pipe(webpack(require(`./webpack.config.js`), require(`webpack`)))
10+
.pipe(gulpif(/\.js$/, uglify()))
11+
.pipe(gulp.dest(`dist/`));
12+
13+
});

jest.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
resolver: './scripts/jest-resolver',
3+
};

package.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"private": true,
3+
"dependencies": {
4+
"babel-eslint": "^8.2.2",
5+
"core-decorators": "^0.20.0",
6+
"eslint": "^4.19.1",
7+
"eslint-plugin-prettier": "^2.6.0",
8+
"eslint-plugin-react": "^7.7.0",
9+
"lodash": "^4.17.5",
10+
"prettier": "^1.12.0",
11+
"react": "^16.2.0",
12+
"react-dom": "^16.2.0"
13+
},
14+
"devDependencies": {
15+
"babel-core": "^6.4.1",
16+
"babel-loader": "^7.1.4",
17+
"babel-plugin-transform-class-properties": "^6.24.1",
18+
"babel-plugin-transform-decorators-legacy": "^1.3.4",
19+
"babel-preset-env": "^1.6.1",
20+
"babel-preset-react": "^6.24.1",
21+
"gulp": "^3.9.1",
22+
"gulp-if": "^2.0.2",
23+
"gulp-uglify": "^3.0.0",
24+
"html-webpack-plugin": "^3.1.0",
25+
"http-server": "^0.11.1",
26+
"jest": "23.0.0-alpha.6r",
27+
"jest-environment-jsdom": "23.0.0-alpha.6r",
28+
"webpack": "^4.4.1",
29+
"webpack-cli": "^2.0.13",
30+
"webpack-stream": "^4.0.3"
31+
},
32+
"scripts": {
33+
"build": "webpack",
34+
"serve": "http-server -c-1 dist",
35+
"bench:size": "./scripts/bench-size.sh"
36+
},
37+
"resolutions": {
38+
"jest-config": "file:/Users/mael/jest/packages/jest-config"
39+
}
40+
}

scripts/bench-size.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
export YARN_PLUGNPLAY_EXPERIMENTAL=false
4+
./yarn.js install >/dev/null
5+
./yarn.js build >/dev/null
6+
7+
echo Before:
8+
ls -lh ./dist/app.js
9+
10+
export YARN_PLUGNPLAY_EXPERIMENTAL=true
11+
./yarn.js install >/dev/null
12+
./yarn.js build >/dev/null
13+
14+
echo After:
15+
ls -lh ./dist/app.js

scripts/jest-resolver.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let pnp = require('../.pnp.js');
2+
3+
module.exports = (request, {basedir}) => {
4+
return pnp.resolveRequest(request, `${basedir}/`);
5+
};

scripts/webpack-resolver.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
let pnp;
2+
3+
try {
4+
pnp = require(`../.pnp.js`);
5+
} catch (error) {}
6+
7+
module.exports = {
8+
apply: function(resolver) {
9+
if (!pnp) {
10+
return;
11+
}
12+
13+
const resolvedHook = resolver.ensureHook(`resolved`);
14+
resolver.getHook(`resolve`).tapAsync(`PnpResolver`, (request, resolveContext, callback) => {
15+
if (request.context.issuer === undefined) {
16+
return callback();
17+
}
18+
19+
let issuer;
20+
let resolution;
21+
22+
if (!request.context.issuer) {
23+
issuer = `${request.path}/`;
24+
} else if (request.context.issuer.startsWith(`/`)) {
25+
issuer = request.context.issuer;
26+
} else {
27+
throw new Error(`Cannot successfully resolve this dependency`);
28+
}
29+
30+
try {
31+
resolution = pnp.resolveRequest(request.request, issuer);
32+
} catch (error) {
33+
// TODO This is not good! But the `debug` package tries to require `supports-color` without declaring it in its
34+
// package.json, and Webpack accepts this because it`s in a try/catch, so we need to do it as well.
35+
return callback(error);
36+
}
37+
38+
resolver.doResolve(resolvedHook, Object.assign({}, request, {
39+
path: resolution,
40+
}), null, resolveContext, callback);
41+
});
42+
}
43+
};

sources/__tests__/fibonacci.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {fibonacci} from '../fibonacci';
2+
3+
describe(`fibonacci`, () => {
4+
it(`should work for negative values`, () => {
5+
expect(fibonacci(-10)).toEqual(-55);
6+
expect(fibonacci(-9)).toEqual(34);
7+
expect(fibonacci(-8)).toEqual(-21);
8+
expect(fibonacci(-7)).toEqual(13);
9+
expect(fibonacci(-6)).toEqual(-8);
10+
expect(fibonacci(-5)).toEqual(5);
11+
expect(fibonacci(-4)).toEqual(-3);
12+
expect(fibonacci(-3)).toEqual(2);
13+
expect(fibonacci(-2)).toEqual(-1);
14+
expect(fibonacci(-1)).toEqual(1);
15+
});
16+
17+
it(`should work for zero values`, () => {
18+
expect(fibonacci(-0)).toEqual(0);
19+
expect(fibonacci(+0)).toEqual(0);
20+
});
21+
22+
it(`should work for positive values`, () => {
23+
expect(fibonacci(+1)).toEqual(1);
24+
expect(fibonacci(+2)).toEqual(1);
25+
expect(fibonacci(+5)).toEqual(5);
26+
expect(fibonacci(+10)).toEqual(55);
27+
});
28+
29+
it(`should return -Infinity for -Infinity`, () => {
30+
expect(fibonacci(-Infinity)).toEqual(-Infinity);
31+
});
32+
33+
it(`should return +Infinity for +Infinity`, () => {
34+
expect(fibonacci(+Infinity)).toEqual(+Infinity);
35+
});
36+
});

sources/fibonacci.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function fibonacci(n) {
2+
if (n === -Infinity) return -Infinity;
3+
4+
if (n === +Infinity) return +Infinity;
5+
6+
if (n === 0) return 0;
7+
8+
let A = (1 + Math.sqrt(5)) / 2;
9+
let B = (1 - Math.sqrt(5)) / 2;
10+
11+
let res = Math.ceil((Math.pow(A, n) - Math.pow(B, n)) / Math.sqrt(5));
12+
13+
if (res <= 0) res -= 1;
14+
15+
return res;
16+
}

sources/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {autobind} from 'core-decorators';
2+
import React from 'react';
3+
import ReactDOM from 'react-dom';
4+
5+
import {fibonacci} from './fibonacci';
6+
7+
class Application extends React.PureComponent {
8+
state = {
9+
n: `1`,
10+
};
11+
12+
@autobind
13+
handleChange(e) {
14+
this.setState({
15+
n: e.target.value,
16+
});
17+
}
18+
19+
render() {
20+
return (
21+
<div>
22+
Everything is working just fine! And as a bonus, here's a fibonacci number calculator:
23+
<div style={{marginTop: 10}}>
24+
<input value={this.state.n} onChange={this.handleChange} />
25+
<input value={fibonacci(parseInt(this.state.n, 10) || 1)} readOnly={true} />
26+
</div>
27+
</div>
28+
);
29+
}
30+
}
31+
32+
ReactDOM.render(<Application />, document.body);

webpack.config.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const HtmlWebpackPlugin = require(`html-webpack-plugin`);
2+
const PnpWebpackPlugin = require(`./scripts/webpack-resolver`);
3+
4+
module.exports = {
5+
6+
mode: `production`,
7+
8+
entry: {
9+
[`app`]: `./sources/index.js`,
10+
},
11+
12+
output: {
13+
filename: `[name].js`,
14+
},
15+
16+
module: {
17+
rules: [{
18+
test: /\.js$/,
19+
exclude: /node_modules/,
20+
loader: require.resolve('babel-loader'),
21+
options: {},
22+
}]
23+
},
24+
25+
resolve: {
26+
plugins: [
27+
PnpWebpackPlugin,
28+
]
29+
},
30+
31+
resolveLoader: {
32+
plugins: [
33+
PnpWebpackPlugin,
34+
]
35+
},
36+
37+
plugins: [
38+
new HtmlWebpackPlugin()
39+
]
40+
41+
};

0 commit comments

Comments
 (0)