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

Commit

Permalink
Add rule.rules
Browse files Browse the repository at this point in the history
  • Loading branch information
opl- committed Dec 21, 2019
1 parent 7fb3c67 commit 8b703e4
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 16 deletions.
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,93 @@ config.module
}));
```

#### Config module rules nested rules:

```js
config.module.rules{}.rules : ChainedMap<Rule>
config.module
.rule(name)
.rule(name)
// Example
config.module
.rule('css')
.test(/\.css$/)
.use('style')
.loader('style-loader')
.end()
.rule('postcss')
.resourceQuery(/postcss/)
.use('postcss')
.loader('postcss-loader')
```

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

```js
config.module.rules{}.rules : ChainedMap<Rule>
config.module
.rule(name)
.rule(name)
.before(otherName)
// Example
config.module
.rule('css')
.use('style')
.loader('style-loader')
.end()
.rule('postcss')
.resourceQuery(/postcss/)
.use('postcss')
.loader('postcss-loader')
.end()
.end()
.rule('css-loader')
.resourceQuery(/css-loader/)
.before('postcss')
.use('css-loader')
.loader('css-loader')
```

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

```js
config.module.rules{}.rules : ChainedMap<Rule>
config.module
.rule(name)
.rule(name)
.after(otherName)
// Example
config.module
.rule('css')
.use('style')
.loader('style-loader')
.end()
.rule('postcss')
.resourceQuery(/postcss/)
.after('css-loader')
.use('postcss')
.loader('postcss-loader')
.end()
.end()
.rule('css-loader')
.resourceQuery(/css-loader/)
.use('css-loader')
.loader('css-loader')
```

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

```js
Expand Down Expand Up @@ -1233,6 +1320,10 @@ config.merge({
include: [...paths],
exclude: [...paths],
rules: {
[name]: Rule
},
oneOf: {
[name]: Rule
},
Expand Down
7 changes: 2 additions & 5 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,8 @@ module.exports = class extends ChainedMap {

// improve rule/use output
if (value && value.__ruleNames) {
const prefix = `/* ${configPrefix}.module.rule('${
value.__ruleNames[0]
}')${value.__ruleNames
.slice(1)
.map(r => `.oneOf('${r}')`)
const prefix = `/* ${configPrefix}.module${value.__ruleNames
.map(([t, r]) => `.${t}('${r}')`)
.join('')}${
value.__useName ? `.use('${value.__useName}')` : ``
} */\n`;
Expand Down
7 changes: 5 additions & 2 deletions src/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ module.exports = class extends ChainedMap {
}

defaultRule(name) {
return this.defaultRules.getOrCompute(name, () => new Rule(this, name));
return this.defaultRules.getOrCompute(
name,
() => new Rule(this, name, 'defaultRule'),
);
}

rule(name) {
return this.rules.getOrCompute(name, () => new Rule(this, name));
return this.rules.getOrCompute(name, () => new Rule(this, name, 'rule'));
}

toConfig() {
Expand Down
23 changes: 20 additions & 3 deletions src/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ const Use = require('./Use');

const Rule = Orderable(
class extends ChainedMap {
constructor(parent, name) {
constructor(parent, name, ruleType = 'rule') {
super(parent);
this.ruleType = ruleType;
this.name = name;
this.names = [];

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

this.uses = new ChainedMap(this);
this.include = new ChainedSet(this);
this.exclude = new ChainedSet(this);
this.rules = new ChainedMap(this);
this.oneOfs = new ChainedMap(this);
this.extend([
'enforce',
Expand All @@ -36,8 +38,15 @@ const Rule = Orderable(
return this.uses.getOrCompute(name, () => new Use(this, name));
}

rule(name) {
return this.rules.getOrCompute(name, () => new Rule(this, name, 'rule'));
}

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

pre() {
Expand All @@ -53,6 +62,7 @@ const Rule = Orderable(
Object.assign(this.entries() || {}, {
include: this.include.values(),
exclude: this.exclude.values(),
rules: this.rules.values().map(rule => rule.toConfig()),
oneOf: this.oneOfs.values().map(oneOf => oneOf.toConfig()),
use: this.uses.values().map(use => use.toConfig()),
}),
Expand Down Expand Up @@ -80,6 +90,12 @@ const Rule = Orderable(
);
}

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

if (!omit.includes('oneOf') && 'oneOf' in obj) {
Object.keys(obj.oneOf).forEach(name =>
this.oneOf(name).merge(obj.oneOf[name]),
Expand All @@ -99,6 +115,7 @@ const Rule = Orderable(
'include',
'exclude',
'use',
'rules',
'oneOf',
'test',
]);
Expand Down
41 changes: 41 additions & 0 deletions test/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,20 @@ test('toString', t => {
.use('babel')
.loader('babel-loader');

// Nested rules
config.module
.rule('alpha')
.rule('nested')
.use('babel')
.loader('babel-loader');

// Default rules
config.module
.defaultRule('default')
.rule('nested')
.use('babel')
.loader('babel-loader');

const envPluginPath = require.resolve('webpack/lib/EnvironmentPlugin');
const stringifiedEnvPluginPath = stringify(envPluginPath);

Expand Down Expand Up @@ -426,9 +440,36 @@ test('toString', t => {
]
},
module: {
defaultRules: [
/* config.module.defaultRule('default') */
{
rules: [
/* config.module.defaultRule('default').rule('nested') */
{
use: [
/* config.module.defaultRule('default').rule('nested').use('babel') */
{
loader: 'babel-loader'
}
]
}
]
}
],
rules: [
/* config.module.rule('alpha') */
{
rules: [
/* config.module.rule('alpha').rule('nested') */
{
use: [
/* config.module.rule('alpha').rule('nested').use('babel') */
{
loader: 'babel-loader'
}
]
}
],
oneOf: [
/* config.module.rule('alpha').oneOf('beta') */
{
Expand Down
Loading

0 comments on commit 8b703e4

Please sign in to comment.