diff --git a/src/index.ts b/src/index.ts index 242d3ef..59671ed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,11 +2,18 @@ import fs from 'node:fs'; import type { Compiler, RspackPluginInstance } from '@rspack/core'; export interface IPreactRefreshRspackPluginOptions { + include?: string | RegExp | (string | RegExp)[] | null; + exclude?: string | RegExp | (string | RegExp)[] | null; overlay?: { module: string; }; } +interface NormalizedPluginOptions extends IPreactRefreshRspackPluginOptions { + include: NonNullable; + exclude: NonNullable; +} + const PREACT_PATHS = [ 'preact', 'preact/compat', @@ -54,9 +61,12 @@ const NAME = 'PreactRefreshRspackPlugin'; class PreactRefreshRspackPlugin implements RspackPluginInstance { name = NAME; + private options: NormalizedPluginOptions; - constructor(private options: IPreactRefreshRspackPluginOptions) { + constructor(options: IPreactRefreshRspackPluginOptions) { this.options = { + include: options?.include ?? /\.([jt]sx?)$/, + exclude: options?.exclude ?? /node_modules/, overlay: options?.overlay, }; } @@ -87,9 +97,9 @@ class PreactRefreshRspackPlugin implements RspackPluginInstance { ...compiler.options.resolve.alias, }; compiler.options.module.rules.unshift({ - include: /\.([jt]sx?)$/, + include: this.options.include, exclude: { - or: [/node_modules/, ...INTERNAL_PATHS].filter(Boolean), + or: [this.options.exclude, ...INTERNAL_PATHS].filter(Boolean), }, use: 'builtin:preact-refresh-loader', }); diff --git a/test/Config.test.js b/test/Config.test.js new file mode 100644 index 0000000..ca361d7 --- /dev/null +++ b/test/Config.test.js @@ -0,0 +1,5 @@ +const { describeByWalk, createConfigCase } = require('@rspack/test-tools'); + +describeByWalk(__filename, (name, src, dist) => { + createConfigCase(name, src, dist); +}); diff --git a/test/configCases/.gitignore b/test/configCases/.gitignore new file mode 100644 index 0000000..cf4bab9 --- /dev/null +++ b/test/configCases/.gitignore @@ -0,0 +1 @@ +!node_modules diff --git a/test/configCases/condition/exclude/file.js b/test/configCases/condition/exclude/file.js new file mode 100644 index 0000000..347e208 --- /dev/null +++ b/test/configCases/condition/exclude/file.js @@ -0,0 +1 @@ +require("foo"); diff --git a/test/configCases/condition/exclude/index.js b/test/configCases/condition/exclude/index.js new file mode 100644 index 0000000..f29e264 --- /dev/null +++ b/test/configCases/condition/exclude/index.js @@ -0,0 +1,7 @@ +require('./file'); + +it("should exclude selected file when compiling", done => { + expect(__webpack_modules__[require.resolve('./file.js')].toString()) + .not.toContain('__prefresh_utils__'); + done(); +}); diff --git a/test/configCases/condition/exclude/rspack.config.js b/test/configCases/condition/exclude/rspack.config.js new file mode 100644 index 0000000..fc7a97b --- /dev/null +++ b/test/configCases/condition/exclude/rspack.config.js @@ -0,0 +1,12 @@ +const ReactRefreshRspackPlugin = require('../../../..'); + +/** @type {import('@rspack/core').Configuration} */ +module.exports = { + mode: 'development', + target: 'web', + context: __dirname, + entry: './index.js', + plugins: [new ReactRefreshRspackPlugin({ + exclude: /file\.js/, + })], +}; diff --git a/test/configCases/condition/include/file.js b/test/configCases/condition/include/file.js new file mode 100644 index 0000000..347e208 --- /dev/null +++ b/test/configCases/condition/include/file.js @@ -0,0 +1 @@ +require("foo"); diff --git a/test/configCases/condition/include/index.js b/test/configCases/condition/include/index.js new file mode 100644 index 0000000..5e6c5ae --- /dev/null +++ b/test/configCases/condition/include/index.js @@ -0,0 +1,7 @@ +require('./file'); + +it("should include selected file when compiling", done => { + expect(__webpack_modules__[require.resolve('foo')].toString()) + .toContain('__prefresh_utils__'); + done(); +}); diff --git a/test/configCases/condition/include/rspack.config.js b/test/configCases/condition/include/rspack.config.js new file mode 100644 index 0000000..ba4cc01 --- /dev/null +++ b/test/configCases/condition/include/rspack.config.js @@ -0,0 +1,13 @@ +const ReactRefreshRspackPlugin = require('../../../..'); + +/** @type {import('@rspack/core').Configuration} */ +module.exports = { + mode: 'development', + target: 'web', + context: __dirname, + entry: './index.js', + plugins: [new ReactRefreshRspackPlugin({ + exclude: /$^/, // match nothing + include: /foo/, + })], +}; diff --git a/test/configCases/node_modules/foo/index.js b/test/configCases/node_modules/foo/index.js new file mode 100644 index 0000000..e7134e7 --- /dev/null +++ b/test/configCases/node_modules/foo/index.js @@ -0,0 +1 @@ +module.exports = "foo"; diff --git a/test/configCases/node_modules/foo/package.json b/test/configCases/node_modules/foo/package.json new file mode 100644 index 0000000..9035eef --- /dev/null +++ b/test/configCases/node_modules/foo/package.json @@ -0,0 +1,4 @@ +{ + "name": "foo", + "main": "./index.js" +} \ No newline at end of file