Skip to content

Commit

Permalink
Merge pull request #2 from xymopen/main
Browse files Browse the repository at this point in the history
feat: Add include and exclude option
  • Loading branch information
LingyuCoder authored Sep 15, 2024
2 parents 3fb2bc6 + e1ba700 commit d4c5c5c
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPreactRefreshRspackPluginOptions['include']>;
exclude: NonNullable<IPreactRefreshRspackPluginOptions['exclude']>;
}

const PREACT_PATHS = [
'preact',
'preact/compat',
Expand Down Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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',
});
Expand Down
5 changes: 5 additions & 0 deletions test/Config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { describeByWalk, createConfigCase } = require('@rspack/test-tools');

describeByWalk(__filename, (name, src, dist) => {
createConfigCase(name, src, dist);
});
1 change: 1 addition & 0 deletions test/configCases/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules
1 change: 1 addition & 0 deletions test/configCases/condition/exclude/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/exclude/index.js
Original file line number Diff line number Diff line change
@@ -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();
});
12 changes: 12 additions & 0 deletions test/configCases/condition/exclude/rspack.config.js
Original file line number Diff line number Diff line change
@@ -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/,
})],
};
1 change: 1 addition & 0 deletions test/configCases/condition/include/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/include/index.js
Original file line number Diff line number Diff line change
@@ -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();
});
13 changes: 13 additions & 0 deletions test/configCases/condition/include/rspack.config.js
Original file line number Diff line number Diff line change
@@ -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/,
})],
};
1 change: 1 addition & 0 deletions test/configCases/node_modules/foo/index.js

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

4 changes: 4 additions & 0 deletions test/configCases/node_modules/foo/package.json

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

0 comments on commit d4c5c5c

Please sign in to comment.