-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathwith-react.ts
79 lines (73 loc) · 2.02 KB
/
with-react.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Configuration } from '@rspack/core';
import { SharedConfigContext } from './model';
import { withWeb, WithWebOptions } from './with-web';
export type WithReactOptions = Omit<WithWebOptions, 'cssModules'>;
export function withReact(opts:WithWebOptions = {}) {
return function makeConfig(
config: Configuration,
{ options, context }: SharedConfigContext
): Configuration {
const isDev =
process.env.NODE_ENV === 'development' || options.mode === 'development';
config = withWeb({ ...opts, cssModules: true })(config, {
options,
context,
});
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ReactRefreshPlugin = require('@rspack/plugin-react-refresh');
const react = {
runtime: 'automatic',
development: isDev,
refresh: isDev,
};
return {
...config,
plugins: [
...(config.plugins || []),
isDev && new ReactRefreshPlugin(),
].filter(Boolean),
module: {
...config.module,
rules: [
...(config.module.rules || []),
{
test: /\.jsx$/,
loader: 'builtin:swc-loader',
exclude: /node_modules/,
options: {
jsc: {
parser: {
syntax: 'ecmascript',
jsx: true,
},
transform: {
react,
},
externalHelpers: true,
},
},
type: 'javascript/auto',
},
{
test: /\.tsx$/,
loader: 'builtin:swc-loader',
exclude: /node_modules/,
options: {
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
},
transform: {
react,
},
externalHelpers: true,
},
},
type: 'javascript/auto',
},
],
},
};
};
}