Skip to content
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
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,31 @@ export default {

## Options

### test

- Type: [Rspack.RuleSetCondition](https://rspack.rs/config/module-rules#condition)
- Default: `/\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/`

Specifies which files should be processed by the Preact Refresh loader. This option is passed to the `builtin:preact-refresh-loader` as the `rule.test` condition.

Works identically to Rspack's [rule.test](https://rspack.rs/config/module-rules#rulestest) option.

```js
new PreactRefreshRspackPlugin({
test: [/\.jsx$/, /\.tsx$/],
});
```

### include

- Type: [Rspack.RuleSetCondition](https://rspack.rs/config/module-rules#condition)
- Default: `/\.([jt]sx?)$/`
- Default: `undefined`

Explicitly includes files to be processed by the Preact Refresh loader. This option is passed to the `builtin:preact-refresh-loader` as the `rule.include` condition.

Use this to limit processing to specific directories or file patterns.

Include files to be processed by the plugin. The value is the same as the `rule.test` option in Rspack.
Works identically to Rspack's [rule.include](https://rspack.rs/config/module-rules#rulesinclude) option.

```js
new PreactRefreshRspackPlugin({
Expand Down
44 changes: 32 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ import type {
Compiler,
RspackPluginInstance,
RuleSetCondition,
RuleSetRule,
} from '@rspack/core';

const require = createRequire(import.meta.url);

export interface IPreactRefreshRspackPluginOptions {
/**
* Specifies which files should be processed by the Preact Refresh loader.
* This option is passed to the `builtin:preact-refresh-loader` as the `rule.test` condition.
* Works identically to Rspack's `rule.test` option.
* @default /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/
*/
test?: RuleSetCondition;
/**
* Include files to be processed by the plugin.
* The value is the same as the `rule.test` option in Rspack.
* @default /\.([jt]sx?)$/
* The value is the same as the `rule.include` option in Rspack.
* @default undefined
*/
include?: RuleSetCondition | null;
/**
Expand All @@ -38,8 +46,12 @@ export interface IPreactRefreshRspackPluginOptions {
preactPath?: string;
}

interface NormalizedPluginOptions extends IPreactRefreshRspackPluginOptions {
include: NonNullable<IPreactRefreshRspackPluginOptions['include']>;
interface NormalizedPluginOptions extends Omit<
IPreactRefreshRspackPluginOptions,
'test' | 'include' | 'exclude' | 'preactPath'
> {
test: NonNullable<IPreactRefreshRspackPluginOptions['test']>;
include: IPreactRefreshRspackPluginOptions['include'];
exclude: NonNullable<IPreactRefreshRspackPluginOptions['exclude']>;
preactPath: NonNullable<IPreactRefreshRspackPluginOptions['preactPath']>;
}
Expand All @@ -57,13 +69,14 @@ class PreactRefreshRspackPlugin implements RspackPluginInstance {
name = NAME;
private options: NormalizedPluginOptions;

constructor(options: IPreactRefreshRspackPluginOptions) {
constructor(options: IPreactRefreshRspackPluginOptions = {}) {
this.options = {
include: options?.include ?? /\.([jt]sx?)$/,
exclude: options?.exclude ?? /node_modules/,
overlay: options?.overlay,
test: options.test ?? /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/,
include: options.include,
exclude: options.exclude ?? /node_modules/,
overlay: options.overlay,
preactPath:
options?.preactPath ?? dirname(require.resolve('preact/package.json')),
options.preactPath ?? dirname(require.resolve('preact/package.json')),
};
}

Expand Down Expand Up @@ -101,8 +114,8 @@ class PreactRefreshRspackPlugin implements RspackPluginInstance {
...compiler.options.resolve.alias,
};

compiler.options.module.rules.unshift({
include: this.options.include,
const refreshRule: RuleSetRule = {
test: this.options.test,
exclude: {
or: [
this.options.exclude,
Expand All @@ -116,7 +129,14 @@ class PreactRefreshRspackPlugin implements RspackPluginInstance {
not: ['url'],
},
use: 'builtin:preact-refresh-loader',
});
};
if (
this.options.include !== null &&
typeof this.options.include !== 'undefined'
) {
refreshRule.include = this.options.include;
}
compiler.options.module.rules.unshift(refreshRule);

compiler.hooks.thisCompilation.tap(NAME, (compilation) => {
compilation.hooks.runtimeModule.tap(NAME, (runtimeModule) => {
Expand Down
1 change: 1 addition & 0 deletions test/configCases/condition/default-test/file.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 'mjs';
7 changes: 7 additions & 0 deletions test/configCases/condition/default-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('./file.mjs');

it('should process mjs files with default test', () => {
expect(
__webpack_modules__[require.resolve('./file.mjs')].toString(),
).toContain('__prefresh_utils__');
});
10 changes: 10 additions & 0 deletions test/configCases/condition/default-test/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { PreactRefreshRspackPlugin } = require('../../../../dist/index.js');

/** @type {import('@rspack/core').Configuration} */
module.exports = {
mode: 'development',
target: 'web',
context: __dirname,
entry: './index.js',
plugins: [new PreactRefreshRspackPlugin()],
};
1 change: 1 addition & 0 deletions test/configCases/condition/include-null/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'include-null';
7 changes: 7 additions & 0 deletions test/configCases/condition/include-null/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('./file');

it('should omit null include when compiling', () => {
expect(
__webpack_modules__[require.resolve('./file.js')].toString(),
).toContain('__prefresh_utils__');
});
14 changes: 14 additions & 0 deletions test/configCases/condition/include-null/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { PreactRefreshRspackPlugin } = require('../../../../dist/index.js');

/** @type {import('@rspack/core').Configuration} */
module.exports = {
mode: 'development',
target: 'web',
context: __dirname,
entry: './index.js',
plugins: [
new PreactRefreshRspackPlugin({
include: null,
}),
],
};
1 change: 1 addition & 0 deletions test/configCases/condition/test/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('foo');
7 changes: 7 additions & 0 deletions test/configCases/condition/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('./file');

it('should test selected file when compiling', () => {
expect(__webpack_modules__[require.resolve('foo')].toString()).toContain(
'__prefresh_utils__',
);
});
15 changes: 15 additions & 0 deletions test/configCases/condition/test/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { PreactRefreshRspackPlugin } = require('../../../../dist/index.js');

/** @type {import('@rspack/core').Configuration} */
module.exports = {
mode: 'development',
target: 'web',
context: __dirname,
entry: './index.js',
plugins: [
new PreactRefreshRspackPlugin({
exclude: /$^/, // match nothing
test: /foo/,
}),
],
};