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

Updating rule definition shortcuts, adding oneOf #36

Merged
merged 1 commit into from
Sep 12, 2017
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
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,31 @@ config.module
.tap(options => merge(options, { plugins: ['babel-plugin-syntax-object-rest-spread'] }));
```

#### Config module rules oneOfs (conditional rules):

```js
config.module.rules{}.oneOfs : ChainedMap<Rule>

config.module
.rule(name)
.oneOf(name)

// Example

config.module
.rule('css')
.oneOf('inline')
.resourceQuery(/inline/)
.use('url')
.loader('url-loader')
.end()
.end()
.oneOf('external')
.resourceQuery(/external/)
.use('file')
.loader('file-loader')
```

---

### Merging Config
Expand Down Expand Up @@ -861,12 +886,19 @@ config.merge({
[key]: value,

enforce,
test,
issuer,
parser,
resource,
resourceQuery,
test,

include: [...paths],
exclude: [...paths],

oneOf: {
[name]: Rule
},

use: {
[name]: {
loader: LoaderString,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"changelog": "changelog mozilla-neutrino/webpack-chain all --markdown > CHANGELOG.md"
},
"dependencies": {
"deepmerge": "^1.5.0"
"deepmerge": "^1.5.1"
},
"devDependencies": {
"ava": "^0.21.0",
"ava": "^0.22.0",
"changelog": "^1.4.0",
"webpack": "^3.4.1"
"webpack": "^3.5.6"
}
}
20 changes: 18 additions & 2 deletions src/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ const ChainedMap = require('./ChainedMap');
const ChainedSet = require('./ChainedSet');
const Use = require('./Use');

module.exports = class extends ChainedMap {
module.exports = class Rule extends ChainedMap {
constructor(parent) {
super(parent);
this.uses = new ChainedMap(this);
this.include = new ChainedSet(this);
this.exclude = new ChainedSet(this);
this.extend(['parser', 'test', 'enforce']);
this.oneOfs = new ChainedMap(this);
this.extend(['parser', 'test', 'enforce', 'issuer', 'resource', 'resourceQuery']);
}

use(name) {
Expand All @@ -19,6 +20,14 @@ module.exports = class extends ChainedMap {
return this.uses.get(name);
}

oneOf(name) {
if (!this.oneOfs.has(name)) {
this.oneOfs.set(name, new Rule(this));
}

return this.oneOfs.get(name);
}

pre() {
return this.enforce('pre');
}
Expand All @@ -31,6 +40,7 @@ module.exports = class extends ChainedMap {
return this.clean(Object.assign(this.entries() || {}, {
include: this.include.values(),
exclude: this.exclude.values(),
oneOf: this.oneOfs.values().map(r => r.toConfig()),
use: this.uses.values().map(use => use.toConfig())
}));
}
Expand All @@ -53,6 +63,12 @@ module.exports = class extends ChainedMap {
.forEach(name => this.use(name).merge(value[name]));
}

case 'oneOf': {
return Object
.keys(value)
.forEach(name => this.oneOf(name).merge(value[name]))
}

case 'test': {
return this.test(value instanceof RegExp ? value : new RegExp(value));
}
Expand Down
53 changes: 52 additions & 1 deletion test/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ test('use', t => {
t.true(rule.uses.has('babel'));
});

test('oneOf', t => {
const rule = new Rule();
const instance = rule.oneOf('babel').end();

t.is(instance, rule);
t.true(rule.oneOfs.has('babel'));
});

test('pre', t => {
const rule = new Rule();
const instance = rule.pre();
Expand Down Expand Up @@ -78,13 +86,24 @@ test('toConfig with values', t => {
.test(/\.js$/)
.use('babel')
.loader('babel-loader')
.options({ presets: ['alpha'] });
.options({ presets: ['alpha'] })
.end()
.oneOf('inline')
.resourceQuery(/inline/)
.use('url')
.loader('url-loader');

t.deepEqual(rule.toConfig(), {
test: /\.js$/,
enforce: 'pre',
include: ['alpha', 'beta'],
exclude: ['alpha', 'beta'],
oneOf: [{
resourceQuery: /inline/,
use: [{
loader: 'url-loader'
}]
}],
use: [{
loader: 'babel-loader',
options: {
Expand All @@ -101,6 +120,16 @@ test('merge empty', t => {
test: /\.js$/,
include: ['alpha', 'beta'],
exclude: ['alpha', 'beta'],
oneOf: {
inline: {
resourceQuery: /inline/,
use: {
url: {
loader: 'url-loader'
}
}
}
},
use: {
babel: {
loader: 'babel-loader',
Expand All @@ -118,6 +147,12 @@ test('merge empty', t => {
test: /\.js$/,
include: ['alpha', 'beta'],
exclude: ['alpha', 'beta'],
oneOf: [{
resourceQuery: /inline/,
use: [{
loader: 'url-loader'
}]
}],
use: [{
loader: 'babel-loader',
options: {
Expand All @@ -143,6 +178,16 @@ test('merge with values', t => {
enforce: 'pre',
include: ['alpha', 'beta'],
exclude: ['alpha', 'beta'],
oneOf: {
inline: {
resourceQuery: /inline/,
use: {
url: {
loader: 'url-loader'
}
}
}
},
use: {
babel: {
options: {
Expand All @@ -157,6 +202,12 @@ test('merge with values', t => {
enforce: 'pre',
include: ['gamma', 'delta', 'alpha', 'beta'],
exclude: ['alpha', 'beta'],
oneOf: [{
resourceQuery: /inline/,
use: [{
loader: 'url-loader'
}]
}],
use: [{
loader: 'babel-loader',
options: {
Expand Down
Loading