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

feat: Add include and exclude option #2

Merged
merged 4 commits into from
Sep 15, 2024
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
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.