Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Support Webpack 4.x #51

Merged
merged 8 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dist: trusty
language: node_js
cache: yarn
node_js:
- '6.10'
- '6.11.5'
- '7'
- '8'
install:
Expand Down
56 changes: 53 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# webpack-chain

Use a chaining API to generate and simplify the modification of
Webpack 2 and 3 configurations.
Webpack version 2-4 configurations.

This documentation corresponds to v4 of webpack-chain.

Expand Down Expand Up @@ -30,7 +30,7 @@ This is easier explained through the examples following.
## Installation

`webpack-chain` requires Node.js v6.9 and higher. `webpack-chain` also
only creates configuration objects designed for use in Webpack 2 and 3.
only creates configuration objects designed for use in Webpack version 2, 3, and 4.

You may install this package using either Yarn or npm (choose one):

Expand Down Expand Up @@ -347,6 +347,7 @@ config
.context(context)
.externals(externals)
.loader(loader)
.mode(mode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config merging section of the README also references the top level options, could you add mode there too? (I see parallelism is also missing from that, perhaps worth fixing whilst we're there)

Also, could you add the optimization options to the README too? :-)

.parallelism(parallelism)
.profile(profile)
.recordsPath(recordsPath)
Expand Down Expand Up @@ -573,6 +574,32 @@ config.performance
.assetFilter(assetFilter)
```

#### Configuring optimizations: shorthand methods

```js
config.optimization : ChainedMap

config.optimization
.concatenateModules(concatenateModules)
.flagIncludedChunks(flagIncludedChunks)
.mergeDuplicateChunks(mergeDuplicateChunks)
.minimize(minimize)
.minimizer(minimizer)
.namedChunks(namedChunks)
.namedModules(namedModules)
.nodeEnv(nodeEnv)
.noEmitOnErrors(noEmitOnErrors)
.occurrenceOrder(occurrenceOrder)
.portableRecords(portableRecords)
.providedExports(providedExports)
.removeAvailableModules(removeAvailableModules)
.removeEmptyChunks(removeEmptyChunks)
.runtimeChunk(runtimeChunk)
.sideEffects(sideEffects)
.splitChunks(splitChunks)
.usedExports(usedExports)
```

#### Config plugins

```js
Expand Down Expand Up @@ -929,10 +956,12 @@ config.merge({
amd,
bail,
cache,
devtool,
context,
devtool,
externals,
loader,
mode,
parallelism,
profile,
recordsPath,
recordsInputPath,
Expand Down Expand Up @@ -984,6 +1013,27 @@ config.merge({
[key]: value
},

optimizations: {
concatenateModules,
flagIncludedChunks,
mergeDuplicateChunks,
minimize,
minimizer,
namedChunks,
namedModules,
nodeEnv,
noEmitOnErrors,
occurrenceOrder,
portableRecords,
providedExports,
removeAvailableModules,
removeEmptyChunks,
runtimeChunk,
sideEffects,
splitChunks,
usedExports,
},

performance: {
[key]: value,

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"devDependencies": {
"ava": "^0.25.0",
"changelog": "^1.4.0",
"webpack": "^3.6.0"
"webpack": "^4.5.0"
}
}
9 changes: 7 additions & 2 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Output = require('./Output');
const DevServer = require('./DevServer');
const Plugin = require('./Plugin');
const Module = require('./Module');
const Optimization = require('./Optimization');
const Performance = require('./Performance');

module.exports = class extends ChainedMap {
Expand All @@ -15,6 +16,7 @@ module.exports = class extends ChainedMap {
this.entryPoints = new ChainedMap(this);
this.module = new Module(this);
this.node = new ChainedMap(this);
this.optimization = new Optimization(this);
this.output = new Output(this);
this.performance = new Performance(this);
this.plugins = new ChainedMap(this);
Expand All @@ -24,14 +26,15 @@ module.exports = class extends ChainedMap {
'amd',
'bail',
'cache',
'devtool',
'context',
'devtool',
'externals',
'loader',
'mode',
'parallelism',
'profile',
'recordsPath',
'recordsInputPath',
'recordsPath',
'recordsOutputPath',
'stats',
'target',
Expand Down Expand Up @@ -66,6 +69,7 @@ module.exports = class extends ChainedMap {
resolveLoader: this.resolveLoader.toConfig(),
devServer: this.devServer.toConfig(),
module: this.module.toConfig(),
optimization: this.optimization.entries(),
plugins: this.plugins.values().map(plugin => plugin.toConfig()),
performance: this.performance.entries(),
entry: Object
Expand All @@ -81,6 +85,7 @@ module.exports = class extends ChainedMap {
'resolve',
'resolveLoader',
'devServer',
'optimization',
'performance',
'module'
];
Expand Down
18 changes: 17 additions & 1 deletion src/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ module.exports = class extends ChainedMap {
constructor(parent) {
super(parent);
this.rules = new ChainedMap(this);
this.defaultRules = new ChainedMap(this);
this.extend(['noParse']);
}

defaultRule(name) {
if (!this.defaultRules.has(name)) {
this.defaultRules.set(name, new Rule(this));
}

return this.defaultRules.get(name);
}

rule(name) {
if (!this.rules.has(name)) {
this.rules.set(name, new Rule(this));
Expand All @@ -18,6 +27,7 @@ module.exports = class extends ChainedMap {

toConfig() {
return this.clean(Object.assign(this.entries() || {}, {
defaultRules: this.defaultRules.values().map(r => r.toConfig()),
rules: this.rules.values().map(r => r.toConfig())
}));
}
Expand All @@ -29,6 +39,12 @@ module.exports = class extends ChainedMap {
.forEach(name => this.rule(name).merge(obj.rule[name]));
}

return super.merge(obj, ['rule']);
if (!omit.includes('defaultRule') && 'defaultRule' in obj) {
Object
.keys(obj.defaultRule)
.forEach(name => this.defaultRule(name).merge(obj.defaultRule[name]));
}

return super.merge(obj, ['rule', 'defaultRule']);
}
};
27 changes: 27 additions & 0 deletions src/Optimization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const ChainedMap = require('./ChainedMap');

module.exports = class extends ChainedMap {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a new test test/Optimization.js for this?

constructor(parent) {
super(parent);
this.extend([
'concatenateModules',
'flagIncludedChunks',
'mergeDuplicateChunks',
'minimize',
'minimizer',
'namedChunks',
'namedModules',
'nodeEnv',
'noEmitOnErrors',
'occurrenceOrder',
'portableRecords',
'providedExports',
'removeAvailableModules',
'removeEmptyChunks',
'runtimeChunk',
'sideEffects',
'splitChunks',
'usedExports',
]);
}
};
6 changes: 5 additions & 1 deletion src/Output.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ module.exports = class extends ChainedMap {
super(parent);
this.extend([
'auxiliaryComment',
'chunkCallbackName',
'chunkFilename',
'chunkLoadTimeout',
'crossOriginLoading',
'devtoolFallbackModuleFilenameTemplate',
'devtoolLineToLine',
'devtoolModuleFilenameTemplate',
'devtoolNamespace',
'filename',
'globalObject',
'hashDigest',
'hashDigestLength',
'hashFunction',
Expand All @@ -29,7 +32,8 @@ module.exports = class extends ChainedMap {
'sourceMapFilename',
'sourcePrefix',
'strictModuleExceptionHandling',
'umdNamedDefine'
'umdNamedDefine',
'webassemblyModuleFilename',
]);
}
};
4 changes: 2 additions & 2 deletions src/Performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module.exports = class extends ChainedMap {
constructor(parent) {
super(parent);
this.extend([
'assetFilter',
'hints',
'maxEntrypointSize',
'maxAssetSize',
'assetFilter'
'maxEntrypointSize'
]);
}
};
3 changes: 2 additions & 1 deletion src/Resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ module.exports = class extends ChainedMap {
this.extend([
'cachePredicate',
'cacheWithContext',
'concord',
'enforceExtension',
'enforceModuleExtension',
'symlinks',
'unsafeCache',
'symlinks'
]);
}

Expand Down
11 changes: 10 additions & 1 deletion src/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ module.exports = class Rule extends ChainedMap {
this.include = new ChainedSet(this);
this.exclude = new ChainedSet(this);
this.oneOfs = new ChainedMap(this);
this.extend(['parser', 'test', 'enforce', 'issuer', 'resource', 'resourceQuery']);
this.extend([
'enforce',
'issuer',
'parser',
'resource',
'resourceQuery',
'sideEffects',
'test',
'type'
]);
}

use(name) {
Expand Down
28 changes: 27 additions & 1 deletion test/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,24 @@ test('toConfig with values', t => {
.output
.path('build')
.end()
.mode('development')
.node
.set('__dirname', 'mock')
.end()
.optimization
.nodeEnv('PRODUCTION')
.end()
.target('node')
.plugin('stringify')
.use(StringifyPlugin)
.end()
.module
.defaultRule('inline')
.use('banner')
.loader('banner-loader')
.options({ prefix: 'banner-prefix.txt' })
.end()
.end()
.rule('compile')
.include
.add('alpha')
Expand All @@ -110,15 +120,25 @@ test('toConfig with values', t => {
.options({ presets: ['alpha'] });

t.deepEqual(config.toConfig(), {
mode: 'development',
node: {
__dirname: 'mock'
},
optimization: {
nodeEnv: 'PRODUCTION',
},
output: {
path: 'build'
},
target: 'node',
plugins: [new StringifyPlugin()],
module: {
defaultRules: [{
use: [{
loader: 'banner-loader',
options: { prefix: 'banner-prefix.txt' },
}]
}],
rules: [{
include: ['alpha', 'beta'],
exclude: ['alpha', 'beta'],
Expand All @@ -135,9 +155,10 @@ test('toConfig with values', t => {

test('validate empty', t => {
const config = new Config();

const errors = validate(config.toConfig());

t.is(errors.length, 1);
t.is(errors.length, 0);
});

test('validate with entry', t => {
Expand All @@ -161,6 +182,10 @@ test('validate with values', t => {
.output
.path('/build')
.end()
.mode('development')
.optimization
.nodeEnv('PRODUCTION')
.end()
.node
.set('__dirname', 'mock')
.end()
Expand All @@ -178,6 +203,7 @@ test('validate with values', t => {
.add('alpha')
.add('beta')
.end()
.sideEffects(false)
.post()
.pre()
.test(/\.js$/)
Expand Down
Loading