Skip to content

Commit

Permalink
feat: support resourceQueryExclude option (#165)
Browse files Browse the repository at this point in the history
* 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 <ricardogobbosouza@yahoo.com.br>
  • Loading branch information
ben-lau and ricardogobbosouza committed Jun 23, 2022
1 parent 67efb34 commit 464120f
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.md
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
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
* @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
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
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
@@ -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
@@ -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
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
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
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

0 comments on commit 464120f

Please sign in to comment.