Browse Source

Upgrading webpack-chain to v3.1 (#157)

v6-dev
Eli Perelman 8 years ago
committed by GitHub
parent
commit
d009882e6d
  1. 17
      packages/neutrino-middleware-compile-loader/index.js
  2. 61
      packages/neutrino-middleware-eslint/index.js
  3. 9
      packages/neutrino-preset-airbnb-base/index.js
  4. 7
      packages/neutrino-preset-jest/src/index.js
  5. 37
      packages/neutrino-preset-node/index.js
  6. 42
      packages/neutrino-preset-react/index.js
  7. 60
      packages/neutrino-preset-web/index.js
  8. 2
      packages/neutrino/package.json
  9. 6
      packages/neutrino/yarn.lock

17
packages/neutrino-middleware-compile-loader/index.js

@ -1,17 +1,8 @@
module.exports = ({ config }, options) => { module.exports = ({ config }, options) => config.module
const rule = config.module
.rule('compile') .rule('compile')
.test(options.test || /\.jsx?$/) .test(options.test || /\.jsx?$/)
.when(options.include, rule => rule.include.merge(options.include))
.when(options.exclude, rule => rule.exclude.merge(options.exclude))
.use('babel') .use('babel')
.loader(require.resolve('babel-loader')) .loader(require.resolve('babel-loader'))
.options(options.babel) .options(options.babel);
.end();
if (options.include) {
rule.include.merge(options.include);
}
if (options.exclude) {
rule.exclude.merge(options.exclude);
}
};

61
packages/neutrino-middleware-eslint/index.js

@ -2,25 +2,47 @@ const merge = require('deepmerge');
const clone = require('lodash.clonedeep'); const clone = require('lodash.clonedeep');
const { join } = require('path'); const { join } = require('path');
const IF_NOT_DEV = process.env.NODE_ENV !== 'development';
const MODULES = join(__dirname, 'node_modules'); const MODULES = join(__dirname, 'node_modules');
module.exports = (neutrino, options) => { module.exports = (neutrino, options) => {
const { config } = neutrino; const isNotDev = process.env.NODE_ENV !== 'development';
const lint = config.module.rule('lint');
config.resolve.modules.add(MODULES); // eslint-disable-next-line no-param-reassign
config.resolveLoader.modules.add(MODULES); neutrino.eslintrc = () => {
const options = clone(neutrino.config.module.rule('lint').use('eslint').get('options'));
options.extends = options.baseConfig.extends;
options.useEslintrc = true;
options.env = options.envs.reduce((env, key) => Object.assign(env, { [key]: true }), {});
options.globals = options.globals.reduce((globals, key) => Object.assign(globals, { [key]: true }), {});
['envs', 'baseConfig', 'failOnError', 'emitWarning', 'emitError'].map(method => delete options[method]);
return options;
};
lint neutrino.config
.resolve
.modules
.add(MODULES)
.end()
.end()
.resolveLoader
.modules
.add(MODULES)
.end()
.end()
.module
.rule('lint')
.test(options.test || /\.(js|jsx)$/) .test(options.test || /\.(js|jsx)$/)
.pre() .pre()
.when(options.include, rule => rule.include.merge(options.include))
.when(options.exclude, rule => rule.exclude.merge(options.exclude))
.use('eslint') .use('eslint')
.loader(require.resolve('eslint-loader')) .loader(require.resolve('eslint-loader'))
.options(merge({ .options(merge({
failOnError: IF_NOT_DEV, failOnError: isNotDev,
emitWarning: IF_NOT_DEV, emitWarning: isNotDev,
emitError: IF_NOT_DEV, emitError: isNotDev,
cwd: neutrino.options.root, cwd: neutrino.options.root,
useEslintrc: false, useEslintrc: false,
root: true, root: true,
@ -41,25 +63,4 @@ module.exports = (neutrino, options) => {
globals: ['process'], globals: ['process'],
rules: {} rules: {}
}, options.eslint || {})); }, options.eslint || {}));
if (options.include) {
lint.include.merge(options.include);
}
if (options.exclude) {
lint.exclude.merge(options.exclude);
}
// eslint-disable-next-line no-param-reassign
neutrino.eslintrc = () => {
const options = clone(config.module.rule('lint').use('eslint').get('options'));
options.extends = options.baseConfig.extends;
options.useEslintrc = true;
options.env = options.envs.reduce((env, key) => Object.assign(env, { [key]: true }), {});
options.globals = options.globals.reduce((globals, key) => Object.assign(globals, { [key]: true }), {});
['envs', 'baseConfig', 'failOnError', 'emitWarning', 'emitError'].map(method => delete options[method]);
return options;
};
}; };

9
packages/neutrino-preset-airbnb-base/index.js

@ -23,10 +23,7 @@ module.exports = (neutrino, options) => {
} }
}, options)); }, options));
if (!options.include && !options.exclude) { neutrino.config.module.rule('lint')
neutrino.config.module .when(!options.include && !options.exclude,
.rule('lint') rule => rule.include.add(neutrino.options.source));
.include
.add(neutrino.options.source);
}
}; };

7
packages/neutrino-preset-jest/src/index.js

@ -60,12 +60,11 @@ module.exports = (neutrino) => {
} }
}); });
if (neutrino.config.module.rules.has('lint')) { neutrino.config.when(neutrino.config.module.rules.has('lint'), () => neutrino
neutrino.use(loaderMerge('lint', 'eslint'), { .use(loaderMerge('lint', 'eslint'), {
plugins: ['jest'], plugins: ['jest'],
envs: ['jest'] envs: ['jest']
}); }));
}
const options = normalizeJestOptions(jestOptions, neutrino.config, args); const options = normalizeJestOptions(jestOptions, neutrino.config, args);
const configFile = join(tmpdir(), 'config.json'); const configFile = join(tmpdir(), 'config.json');

37
packages/neutrino-preset-node/index.js

@ -13,7 +13,6 @@ const { path } = require('ramda');
const MODULES = join(__dirname, 'node_modules'); const MODULES = join(__dirname, 'node_modules');
module.exports = (neutrino) => { module.exports = (neutrino) => {
const { config } = neutrino;
let pkg = {}; let pkg = {};
/* eslint-disable global-require, no-empty */ /* eslint-disable global-require, no-empty */
@ -46,8 +45,14 @@ module.exports = (neutrino) => {
} }
}); });
config.performance.hints(false); const hasSourceMap = (pkg.dependencies && 'source-map-support' in pkg.dependencies) ||
config (pkg.devDependencies && 'source-map-support' in pkg.devDependencies);
neutrino.config
.when(hasSourceMap, () => neutrino.use(banner))
.performance
.hints(false)
.end()
.target('node') .target('node')
.node .node
.set('__filename', false) .set('__filename', false)
@ -79,31 +84,24 @@ module.exports = (neutrino) => {
.resolveLoader .resolveLoader
.modules .modules
.add(neutrino.options.node_modules) .add(neutrino.options.node_modules)
.add(MODULES); .add(MODULES)
.end()
const hasSourceMap = (pkg.dependencies && 'source-map-support' in pkg.dependencies) || .end()
(pkg.devDependencies && 'source-map-support' in pkg.devDependencies); .when(process.env.NODE_ENV !== 'development', () => {
if (hasSourceMap) {
neutrino.use(banner);
}
if (process.env.NODE_ENV !== 'development') {
neutrino.use(clean, { paths: [neutrino.options.output] }); neutrino.use(clean, { paths: [neutrino.options.output] });
neutrino.use(copy, { neutrino.use(copy, {
patterns: [{ context: neutrino.options.source, from: '**/*' }], patterns: [{ context: neutrino.options.source, from: '**/*' }],
options: { ignore: ['*.js*'] } options: { ignore: ['*.js*'] }
}); });
} else { }, (config) => {
config.devtool('inline-sourcemap'); config.devtool('inline-sourcemap');
config.entry('index').add('webpack/hot/poll?1000'); config.entry('index').add('webpack/hot/poll?1000');
config.output.devtoolModuleFilenameTemplate('[absolute-resource-path]'); config.output.devtoolModuleFilenameTemplate('[absolute-resource-path]');
neutrino.use(hot); neutrino.use(hot);
neutrino.use(startServer, neutrino.options.entry); neutrino.use(startServer, neutrino.options.entry);
} })
.when(neutrino.config.module.rules.has('lint'), () => neutrino
if (config.module.rules.has('lint')) { .use(loaderMerge('lint', 'eslint'), {
neutrino.use(loaderMerge('lint', 'eslint'), {
envs: ['node'], envs: ['node'],
rules: { rules: {
// enforce return after a callback // enforce return after a callback
@ -141,6 +139,5 @@ module.exports = (neutrino) => {
// disallow use of synchronous methods (off by default) // disallow use of synchronous methods (off by default)
'no-sync': 'off' 'no-sync': 'off'
} }
}); }));
}
}; };

42
packages/neutrino-preset-react/index.js

@ -5,8 +5,6 @@ const { join } = require('path');
const MODULES = join(__dirname, 'node_modules'); const MODULES = join(__dirname, 'node_modules');
module.exports = (neutrino) => { module.exports = (neutrino) => {
const { config } = neutrino;
neutrino.use(web); neutrino.use(web);
neutrino.use(loaderMerge('compile', 'babel'), { neutrino.use(loaderMerge('compile', 'babel'), {
presets: [require.resolve('babel-preset-react')], presets: [require.resolve('babel-preset-react')],
@ -18,24 +16,33 @@ module.exports = (neutrino) => {
} }
}); });
config.resolve.modules.add(MODULES); neutrino.config
config.resolve.extensions.add('.jsx'); .resolve
config.resolve.alias.set('react-native', 'react-native-web'); .modules
config.resolveLoader.modules.add(MODULES); .add(MODULES)
config.externals({ .end()
.extensions
.add('.jsx')
.end()
.alias
.set('react-native', 'react-native-web')
.end()
.end()
.resolveLoader
.modules
.add(MODULES)
.end()
.end()
.externals({
'react/addons': true, 'react/addons': true,
'react/lib/ExecutionEnvironment': true, 'react/lib/ExecutionEnvironment': true,
'react/lib/ReactContext': 'window' 'react/lib/ReactContext': 'window'
}); })
.when(process.env.NODE_ENV === 'development', config => config
if (process.env.NODE_ENV === 'development') {
config
.entry('index') .entry('index')
.prepend(require.resolve('react-hot-loader/patch')); .prepend(require.resolve('react-hot-loader/patch')))
} .when(neutrino.config.module.rules.has('lint'), () => neutrino
.use(loaderMerge('lint', 'eslint'), {
if (config.module.rules.has('lint')) {
neutrino.use(loaderMerge('lint', 'eslint'), {
plugins: ['react'], plugins: ['react'],
baseConfig: { baseConfig: {
extends: ['plugin:react/recommended'] extends: ['plugin:react/recommended']
@ -64,6 +71,5 @@ module.exports = (neutrino) => {
] ]
}] }]
} }
}); }));
}
}; };

60
packages/neutrino-preset-web/index.js

@ -42,8 +42,6 @@ function devServer({ config }, options) {
} }
module.exports = (neutrino) => { module.exports = (neutrino) => {
const { config } = neutrino;
if (!path(['options', 'compile', 'targets', 'browsers'], neutrino)) { if (!path(['options', 'compile', 'targets', 'browsers'], neutrino)) {
Object.assign(neutrino.options, { Object.assign(neutrino.options, {
compile: { compile: {
@ -83,28 +81,38 @@ module.exports = (neutrino) => {
} }
}); });
if (process.env.NODE_ENV !== 'test') { neutrino.config
neutrino.use(chunk); .when(process.env.NODE_ENV !== 'test', () => neutrino.use(chunk))
}
config
.target('web') .target('web')
.context(neutrino.options.root) .context(neutrino.options.root)
.entry('index') .entry('index')
.add(require.resolve('babel-polyfill')) .add(require.resolve('babel-polyfill'))
.add(neutrino.options.entry); .add(neutrino.options.entry)
.end()
config.output .output
.path(neutrino.options.output) .path(neutrino.options.output)
.publicPath('./') .publicPath('./')
.filename('[name].bundle.js') .filename('[name].bundle.js')
.chunkFilename('[id].[chunkhash].js'); .chunkFilename('[id].[chunkhash].js')
.end()
config.resolve.modules.add('node_modules').add(neutrino.options.node_modules).add(MODULES); .resolve
config.resolve.extensions.add('.js').add('.json'); .modules
config.resolveLoader.modules.add(neutrino.options.node_modules).add(MODULES); .add('node_modules')
.add(neutrino.options.node_modules)
config.node .add(MODULES)
.end()
.extensions
.add('.js')
.add('.json')
.end()
.end()
.resolveLoader
.modules
.add(neutrino.options.node_modules)
.add(MODULES)
.end()
.end()
.node
.set('console', false) .set('console', false)
.set('global', true) .set('global', true)
.set('process', true) .set('process', true)
@ -113,16 +121,14 @@ module.exports = (neutrino) => {
.set('__dirname', 'mock') .set('__dirname', 'mock')
.set('setImmediate', true) .set('setImmediate', true)
.set('fs', 'empty') .set('fs', 'empty')
.set('tls', 'empty'); .set('tls', 'empty')
.end()
if (config.module.rules.has('lint')) { .when(neutrino.config.module.rules.has('lint'), () => neutrino
neutrino.use(loaderMerge('lint', 'eslint'), { .use(loaderMerge('lint', 'eslint'), {
globals: ['Buffer'], globals: ['Buffer'],
envs: ['browser', 'commonjs'] envs: ['browser', 'commonjs']
}); }))
} .when(process.env.NODE_ENV === 'development', (config) => {
if (process.env.NODE_ENV === 'development') {
const protocol = process.env.HTTPS ? 'https' : 'http'; const protocol = process.env.HTTPS ? 'https' : 'http';
const host = process.env.HOST || pathOr('localhost', ['options', 'config', 'devServer', 'host'], neutrino); const host = process.env.HOST || pathOr('localhost', ['options', 'config', 'devServer', 'host'], neutrino);
const port = process.env.PORT || pathOr(5000, ['options', 'config', 'devServer', 'port'], neutrino); const port = process.env.PORT || pathOr(5000, ['options', 'config', 'devServer', 'port'], neutrino);
@ -140,7 +146,7 @@ module.exports = (neutrino) => {
.entry('index') .entry('index')
.add(`webpack-dev-server/client?${protocol}://${host}:${port}/`) .add(`webpack-dev-server/client?${protocol}://${host}:${port}/`)
.add('webpack/hot/dev-server'); .add('webpack/hot/dev-server');
} else { }, (config) => {
neutrino.use(clean, { paths: [neutrino.options.output] }); neutrino.use(clean, { paths: [neutrino.options.output] });
neutrino.use(minify); neutrino.use(minify);
neutrino.use(copy, { neutrino.use(copy, {
@ -148,5 +154,5 @@ module.exports = (neutrino) => {
options: { ignore: ['*.js*'] } options: { ignore: ['*.js*'] }
}); });
config.output.filename('[name].[chunkhash].bundle.js'); config.output.filename('[name].[chunkhash].bundle.js');
} });
}; };

2
packages/neutrino/package.json

@ -34,7 +34,7 @@
"ramda": "^0.23.0", "ramda": "^0.23.0",
"ramda-fantasy": "^0.7.0", "ramda-fantasy": "^0.7.0",
"webpack": "^2.3.1", "webpack": "^2.3.1",
"webpack-chain": "^3.0.0", "webpack-chain": "^3.1.0",
"webpack-dev-server": "^2.4.2", "webpack-dev-server": "^2.4.2",
"yargs": "^7.0.2" "yargs": "^7.0.2"
}, },

6
packages/neutrino/yarn.lock

@ -2218,9 +2218,9 @@ wbuf@^1.1.0, wbuf@^1.4.0:
dependencies: dependencies:
minimalistic-assert "^1.0.0" minimalistic-assert "^1.0.0"
webpack-chain@^3.0.0: webpack-chain@^3.1.0:
version "3.0.0" version "3.1.0"
resolved "https://registry.yarnpkg.com/webpack-chain/-/webpack-chain-3.0.0.tgz#20d0b92e84cb294035e1fa03ce2fb8805cce51b6" resolved "https://registry.yarnpkg.com/webpack-chain/-/webpack-chain-3.1.0.tgz#53e82857624d48794443238db99248e598550e03"
dependencies: dependencies:
deepmerge "^1.3.2" deepmerge "^1.3.2"

Loading…
Cancel
Save