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

allow ordering oneOf rules #133

Merged
merged 1 commit into from
Jan 14, 2019
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
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,66 @@ config.module
.loader('file-loader')
```

#### Config module rules oneOfs (conditional rules): ordering before
Specify that the current `oneOf` context should operate before another named
`oneOf`. You cannot use both `.before()` and `.after()` on the same `oneOf`.

```js
config.module
.rule(name)
.oneOf(name)
.before()

// Example

config.module
.rule('scss')
.test(/\.scss$/)
.oneOf('normal')
.use('sass')
.loader('sass-loader')
.end()
.end()
.oneOf('sass-vars')
.before('normal')
.resourceQuery(/\?sassvars/)
.use('sass-vars')
.loader('sass-vars-to-js-loader')
```

#### Config module rules oneOfs (conditional rules): ordering after
Specify that the current `oneOf` context should operate after another named
`oneOf`. You cannot use both `.before()` and `.after()` on the same `oneOf`.

```js
config.module
.rule(name)
.oneOf(name)
.after()

// Example

config.module
.rule('scss')
.test(/\.scss$/)
.oneOf('vue')
.resourceQuery(/\?vue/)
.use('vue-style')
.loader('vue-style-loader')
.end()
.end()
.oneOf('normal')
.use('sass')
.loader('sass-loader')
.end()
.end()
.oneOf('sass-vars')
.after('vue')
.resourceQuery(/\?sassvars/)
.use('sass-vars')
.loader('sass-vars-to-js-loader')
```

---

### Merging Config
Expand Down
167 changes: 87 additions & 80 deletions src/Rule.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,105 @@
const ChainedMap = require('./ChainedMap');
const ChainedSet = require('./ChainedSet');
const Orderable = require('./Orderable');
const Use = require('./Use');

module.exports = class Rule extends ChainedMap {
constructor(parent, name) {
super(parent);
this.name = name;
this.names = [];

let rule = this;
while (rule instanceof Rule) {
this.names.unshift(rule.name);
rule = rule.parent;
const Rule = Orderable(
class extends ChainedMap {
constructor(parent, name) {
super(parent);
this.name = name;
this.names = [];

let rule = this;
while (rule instanceof Rule) {
this.names.unshift(rule.name);
rule = rule.parent;
}

this.uses = new ChainedMap(this);
this.include = new ChainedSet(this);
this.exclude = new ChainedSet(this);
this.oneOfs = new ChainedMap(this);
this.extend([
'enforce',
'issuer',
'parser',
'resource',
'resourceQuery',
'sideEffects',
'test',
'type',
]);
}

this.uses = new ChainedMap(this);
this.include = new ChainedSet(this);
this.exclude = new ChainedSet(this);
this.oneOfs = new ChainedMap(this);
this.extend([
'enforce',
'issuer',
'parser',
'resource',
'resourceQuery',
'sideEffects',
'test',
'type',
]);
}

use(name) {
return this.uses.getOrCompute(name, () => new Use(this, name));
}

oneOf(name) {
return this.oneOfs.getOrCompute(name, () => new Rule(this, name));
}

pre() {
return this.enforce('pre');
}

post() {
return this.enforce('post');
}

toConfig() {
const config = this.clean(
Object.assign(this.entries() || {}, {
include: this.include.values(),
exclude: this.exclude.values(),
oneOf: this.oneOfs.values().map(oneOf => oneOf.toConfig()),
use: this.uses.values().map(use => use.toConfig()),
})
);

Object.defineProperties(config, {
__ruleNames: { value: this.names },
});

return config;
}
use(name) {
return this.uses.getOrCompute(name, () => new Use(this, name));
}

merge(obj, omit = []) {
if (!omit.includes('include') && 'include' in obj) {
this.include.merge(obj.include);
oneOf(name) {
return this.oneOfs.getOrCompute(name, () => new Rule(this, name));
}

if (!omit.includes('exclude') && 'exclude' in obj) {
this.exclude.merge(obj.exclude);
pre() {
return this.enforce('pre');
}

if (!omit.includes('use') && 'use' in obj) {
Object.keys(obj.use).forEach(name => this.use(name).merge(obj.use[name]));
post() {
return this.enforce('post');
}

if (!omit.includes('oneOf') && 'oneOf' in obj) {
Object.keys(obj.oneOf).forEach(name =>
this.oneOf(name).merge(obj.oneOf[name])
toConfig() {
const config = this.clean(
Object.assign(this.entries() || {}, {
include: this.include.values(),
exclude: this.exclude.values(),
oneOf: this.oneOfs.values().map(oneOf => oneOf.toConfig()),
use: this.uses.values().map(use => use.toConfig()),
})
);
}

if (!omit.includes('test') && 'test' in obj) {
this.test(obj.test instanceof RegExp ? obj.test : new RegExp(obj.test));
Object.defineProperties(config, {
__ruleNames: { value: this.names },
});

return config;
}

return super.merge(obj, [
...omit,
'include',
'exclude',
'use',
'oneOf',
'test',
]);
merge(obj, omit = []) {
if (!omit.includes('include') && 'include' in obj) {
this.include.merge(obj.include);
}

if (!omit.includes('exclude') && 'exclude' in obj) {
this.exclude.merge(obj.exclude);
}

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

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

if (!omit.includes('test') && 'test' in obj) {
this.test(obj.test instanceof RegExp ? obj.test : new RegExp(obj.test));
}

return super.merge(obj, [
...omit,
'include',
'exclude',
'use',
'oneOf',
'test',
]);
}
}
};
);

module.exports = Rule;
29 changes: 29 additions & 0 deletions test/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,32 @@ test('merge with omit', t => {
],
});
});

test('ordered oneOfs', t => {
const rule = new Rule();
rule
.oneOf('first')
.test(/\.first$/)
.end()
.oneOf('second')
.test(/\.second$/)
.end()
.oneOf('third')
.test(/\.third$/)
.end()
.oneOf('alpha')
.test(/\.alpha$/)
.before('first')
.end()
.oneOf('beta')
.test(/\.beta$/)
.after('second');

t.deepEqual(rule.toConfig().oneOf.map(o => o.test), [
/\.alpha$/,
/\.first$/,
/\.second$/,
/\.beta$/,
/\.third$/,
]);
});