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

Add rule.resolve #265

Merged
merged 4 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,29 @@ config.module
.loader('sass-vars-to-js-loader')
```

#### Config module rules resolve

Specify a resolve configuration to be merged over the default `config.resolve`
for modules that match the rule.

See "Config resolve" sections above for full syntax.

**Note:** This option is supported by webpack since 4.36.1.

```js
config.module
.rule(name)
.resolve

// Example

config.module
.rule('scss')
.test(/\.scss$/)
.resolve
.symlinks(true)
```

---

### Merging Config
Expand Down
3 changes: 3 additions & 0 deletions src/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const ChainedMap = require('./ChainedMap');
const ChainedSet = require('./ChainedSet');
const Orderable = require('./Orderable');
const Use = require('./Use');
const Resolve = require('./Resolve');

function toArray(arr) {
return Array.isArray(arr) ? arr : [arr];
Expand All @@ -28,6 +29,7 @@ const Rule = Orderable(
this.exclude = new ChainedSet(this);
this.rules = new ChainedMap(this);
this.oneOfs = new ChainedMap(this);
this.resolve = new Resolve(this);
this.extend([
'enforce',
'issuer',
Expand Down Expand Up @@ -71,6 +73,7 @@ const Rule = Orderable(
rules: this.rules.values().map((rule) => rule.toConfig()),
oneOf: this.oneOfs.values().map((oneOf) => oneOf.toConfig()),
use: this.uses.values().map((use) => use.toConfig()),
resolve: this.resolve.toConfig(),
}),
);

Expand Down
8 changes: 8 additions & 0 deletions test/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ test('oneOf', () => {
expect(rule.oneOfs.has('babel')).toBe(true);
});

test('resolve', () => {
const rule = new Rule();
const instance = rule.resolve.alias.set('foo', 'bar').end().end();

expect(instance).toBe(rule);
expect(rule.resolve.alias.has('foo')).toBe(true);
});

test('pre', () => {
const rule = new Rule();
const instance = rule.pre();
Expand Down
3 changes: 2 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ declare namespace Config {

class EntryPoint extends TypedChainedSet<Config, string> {}

class Resolve extends ChainedMap<Config> {
class Resolve<T = Config> extends ChainedMap<T> {
alias: TypedChainedMap<this, string>;
aliasFields: TypedChainedSet<this, string>;
descriptionFiles: TypedChainedSet<this, string>;
Expand Down Expand Up @@ -230,6 +230,7 @@ declare namespace Config {
uses: TypedChainedMap<this, Use>;
include: TypedChainedSet<this, webpack.Condition>;
exclude: TypedChainedSet<this, webpack.Condition>;
resolve: Resolve<Rule<T>>;

parser(value: { [optName: string]: any }): this;
test(value: webpack.Condition | webpack.Condition[]): this;
Expand Down
6 changes: 6 additions & 0 deletions types/test/webpack-chain-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ config
.use('url')
.loader('url-loader')
.end()
.resolve
.symlinks(true)
.end()
.end()
.rules
.delete('inline')
Expand All @@ -336,6 +339,9 @@ config
.oneOfs
.delete('inline')
.end()
.resolve
.symlinks(true)
.end()
.end()
.rules
.delete('compile')
Expand Down