Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support resourceQueryExclude option #165

Merged
merged 8 commits into from
Jun 23, 2022
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ type exclude = string | Array<string>;

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

### `resourceQueryExclude`

- Type:

```ts
type resourceQueryExclude = RegExp | Array<RegExp>;
```

- Default: `[]`

Specify the resource query to exclude.

### `files`

- Type:
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class ESLintWebpackPlugin {
this.getContext(compiler)
),
extensions: arrify(this.options.extensions),
resourceQueryExclude: arrify(this.options.resourceQueryExclude || []).map(
(item) => (item instanceof RegExp ? item : new RegExp(item))
),
files: parseFiles(this.options.files || '', this.getContext(compiler)),
};

Expand Down Expand Up @@ -69,7 +72,7 @@ class ESLintWebpackPlugin {

/**
* @param {Compiler} compiler
* @param {Options} options
* @param {Omit<Options, 'resourceQueryExclude'> & {resourceQueryExclude: RegExp[]}} options
ricardogobbosouza marked this conversation as resolved.
Show resolved Hide resolved
* @param {string[]} wanted
* @param {string[]} exclude
*/
Expand Down Expand Up @@ -104,13 +107,14 @@ class ESLintWebpackPlugin {
// Add the file to be linted
compilation.hooks.succeedModule.tap(this.key, ({ resource }) => {
if (resource) {
const [file] = resource.split('?');
const [file, query] = resource.split('?');

if (
file &&
!files.includes(file) &&
isMatch(file, wanted, { dot: true }) &&
!isMatch(file, exclude, { dot: true })
!isMatch(file, exclude, { dot: true }) &&
options.resourceQueryExclude.every((reg) => !reg.test(query))
) {
files.push(file);

Expand Down
2 changes: 2 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const schema = require('./options.json');
* @property {boolean=} quiet
* @property {OutputReport=} outputReport
* @property {number|boolean=} threads
* @property {RegExp|RegExp[]=} resourceQueryExclude
*/

/** @typedef {PluginOptions & ESLintOptions} Options */
Expand All @@ -50,6 +51,7 @@ function getOptions(pluginOptions) {
emitError: true,
emitWarning: true,
failOnError: true,
resourceQueryExclude: [],
...pluginOptions,
...(pluginOptions.quiet ? { emitError: true, emitWarning: false } : {}),
};
Expand Down
4 changes: 4 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"description": "Specify the files and/or directories to exclude. Must be relative to `options.context`.",
"anyOf": [{ "type": "string" }, { "type": "array" }]
},
"resourceQueryExclude": {
"description": "Specify the resource query to exclude.",
"anyOf": [{ "instanceof": "RegExp" }, { "type": "array" }]
},
"failOnError": {
"description": "Will cause the module build to fail if there are any errors, to disable set to `false`.",
"type": "boolean"
Expand Down
Binary file added test/fixtures/media/some-video.ts
Binary file not shown.
2 changes: 2 additions & 0 deletions test/fixtures/resource-query-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/no-unresolved
require('./media/some-video.ts?media');
23 changes: 23 additions & 0 deletions test/resource-query.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pack from './utils/pack';

describe('resource-query', () => {
it('should exclude the match resource query', (done) => {
const compiler = pack(
'resource-query',
{
resourceQueryExclude: /media/,
extensions: ['.js', '.ts'],
},
{
module: { rules: [{ resourceQuery: /media/, type: 'asset/source' }] },
}
);

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
done();
});
});
});
7 changes: 7 additions & 0 deletions test/utils/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ import webpack from 'webpack';

import conf from './conf';

/**
* new a test webpack compiler
* @param {String} context
* @param {import('../../src/options').Options} pluginConf
* @param {webpack.Configuration} webpackConf
* @returns {ReturnType<webpack>}
*/
export default (context, pluginConf = {}, webpackConf = {}) =>
webpack(conf(context, pluginConf, webpackConf));
6 changes: 4 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ declare class ESLintWebpackPlugin {
options: import('./options').PluginOptions;
/**
* @param {Compiler} compiler
* @param {Options} options
* @param {Omit<Options, 'resourceQueryExclude'> & {resourceQueryExclude: RegExp[]}} options
* @param {string[]} wanted
* @param {string[]} exclude
*/
run(
compiler: Compiler,
options: Options,
options: Omit<Options, 'resourceQueryExclude'> & {
resourceQueryExclude: RegExp[];
},
wanted: string[],
exclude: string[]
): Promise<void>;
Expand Down
2 changes: 2 additions & 0 deletions types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type PluginOptions = {
quiet?: boolean | undefined;
outputReport?: OutputReport | undefined;
threads?: (number | boolean) | undefined;
resourceQueryExclude?: (RegExp | RegExp[]) | undefined;
};
export type Options = PluginOptions & ESLintOptions;
/** @typedef {import("eslint").ESLint.Options} ESLintOptions */
Expand Down Expand Up @@ -58,6 +59,7 @@ export type Options = PluginOptions & ESLintOptions;
* @property {boolean=} quiet
* @property {OutputReport=} outputReport
* @property {number|boolean=} threads
* @property {RegExp|RegExp[]=} resourceQueryExclude
*/
/** @typedef {PluginOptions & ESLintOptions} Options */
/**
Expand Down