Skip to content

Commit 464120f

Browse files
feat: support resourceQueryExclude option (#165)
* feat: resource query exclude option * docs: update resource query exclude * fix: fix quotes and parameters * fix: fix the types * chore: update lock file * style: formatting Co-authored-by: Ricardo Gobbo de Souza <[email protected]>
1 parent 67efb34 commit 464120f

File tree

11 files changed

+64
-5
lines changed

11 files changed

+64
-5
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ type exclude = string | Array<string>;
130130

131131
Specify the files and/or directories to exclude. Must be relative to `options.context`.
132132

133+
### `resourceQueryExclude`
134+
135+
- Type:
136+
137+
```ts
138+
type resourceQueryExclude = RegExp | Array<RegExp>;
139+
```
140+
141+
- Default: `[]`
142+
143+
Specify the resource query to exclude.
144+
133145
### `files`
134146

135147
- Type:

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class ESLintWebpackPlugin {
3838
this.getContext(compiler)
3939
),
4040
extensions: arrify(this.options.extensions),
41+
resourceQueryExclude: arrify(this.options.resourceQueryExclude || []).map(
42+
(item) => (item instanceof RegExp ? item : new RegExp(item))
43+
),
4144
files: parseFiles(this.options.files || '', this.getContext(compiler)),
4245
};
4346

@@ -69,7 +72,7 @@ class ESLintWebpackPlugin {
6972

7073
/**
7174
* @param {Compiler} compiler
72-
* @param {Options} options
75+
* @param {Omit<Options, 'resourceQueryExclude'> & {resourceQueryExclude: RegExp[]}} options
7376
* @param {string[]} wanted
7477
* @param {string[]} exclude
7578
*/
@@ -104,13 +107,14 @@ class ESLintWebpackPlugin {
104107
// Add the file to be linted
105108
compilation.hooks.succeedModule.tap(this.key, ({ resource }) => {
106109
if (resource) {
107-
const [file] = resource.split('?');
110+
const [file, query] = resource.split('?');
108111

109112
if (
110113
file &&
111114
!files.includes(file) &&
112115
isMatch(file, wanted, { dot: true }) &&
113-
!isMatch(file, exclude, { dot: true })
116+
!isMatch(file, exclude, { dot: true }) &&
117+
options.resourceQueryExclude.every((reg) => !reg.test(query))
114118
) {
115119
files.push(file);
116120

src/options.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const schema = require('./options.json');
3636
* @property {boolean=} quiet
3737
* @property {OutputReport=} outputReport
3838
* @property {number|boolean=} threads
39+
* @property {RegExp|RegExp[]=} resourceQueryExclude
3940
*/
4041

4142
/** @typedef {PluginOptions & ESLintOptions} Options */
@@ -50,6 +51,7 @@ function getOptions(pluginOptions) {
5051
emitError: true,
5152
emitWarning: true,
5253
failOnError: true,
54+
resourceQueryExclude: [],
5355
...pluginOptions,
5456
...(pluginOptions.quiet ? { emitError: true, emitWarning: false } : {}),
5557
};

src/options.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
"description": "Specify the files and/or directories to exclude. Must be relative to `options.context`.",
2323
"anyOf": [{ "type": "string" }, { "type": "array" }]
2424
},
25+
"resourceQueryExclude": {
26+
"description": "Specify the resource query to exclude.",
27+
"anyOf": [{ "instanceof": "RegExp" }, { "type": "array" }]
28+
},
2529
"failOnError": {
2630
"description": "Will cause the module build to fail if there are any errors, to disable set to `false`.",
2731
"type": "boolean"

test/fixtures/media/some-video.ts

1.59 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line import/no-unresolved
2+
require('./media/some-video.ts?media');

test/resource-query.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pack from './utils/pack';
2+
3+
describe('resource-query', () => {
4+
it('should exclude the match resource query', (done) => {
5+
const compiler = pack(
6+
'resource-query',
7+
{
8+
resourceQueryExclude: /media/,
9+
extensions: ['.js', '.ts'],
10+
},
11+
{
12+
module: { rules: [{ resourceQuery: /media/, type: 'asset/source' }] },
13+
}
14+
);
15+
16+
compiler.run((err, stats) => {
17+
expect(err).toBeNull();
18+
expect(stats.hasWarnings()).toBe(false);
19+
expect(stats.hasErrors()).toBe(false);
20+
done();
21+
});
22+
});
23+
});

test/utils/pack.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,12 @@ import webpack from 'webpack';
22

33
import conf from './conf';
44

5+
/**
6+
* new a test webpack compiler
7+
* @param {String} context
8+
* @param {import('../../src/options').Options} pluginConf
9+
* @param {webpack.Configuration} webpackConf
10+
* @returns {ReturnType<webpack>}
11+
*/
512
export default (context, pluginConf = {}, webpackConf = {}) =>
613
webpack(conf(context, pluginConf, webpackConf));

types/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ declare class ESLintWebpackPlugin {
88
options: import('./options').PluginOptions;
99
/**
1010
* @param {Compiler} compiler
11-
* @param {Options} options
11+
* @param {Omit<Options, 'resourceQueryExclude'> & {resourceQueryExclude: RegExp[]}} options
1212
* @param {string[]} wanted
1313
* @param {string[]} exclude
1414
*/
1515
run(
1616
compiler: Compiler,
17-
options: Options,
17+
options: Omit<Options, 'resourceQueryExclude'> & {
18+
resourceQueryExclude: RegExp[];
19+
},
1820
wanted: string[],
1921
exclude: string[]
2022
): Promise<void>;

0 commit comments

Comments
 (0)