-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathindex.ts
58 lines (50 loc) · 1.69 KB
/
index.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
import type { RsbuildPlugin } from '@rsbuild/core';
import { modifyBabelLoaderOptions } from '@rsbuild/plugin-babel';
import type { SolidPresetOptions } from './types';
export type PluginSolidOptions = {
/**
* Options passed to `babel-preset-solid`.
* @see https://www.npmjs.com/package/babel-preset-solid
*/
solidPresetOptions?: SolidPresetOptions;
};
export const PLUGIN_SOLID_NAME = 'rsbuild:solid';
export function pluginSolid(options: PluginSolidOptions = {}): RsbuildPlugin {
return {
name: PLUGIN_SOLID_NAME,
setup(api) {
api.modifyBundlerChain(
async (chain, { CHAIN_ID, environment, isProd, target }) => {
const environmentConfig = environment.config;
modifyBabelLoaderOptions({
chain,
CHAIN_ID,
modifier: (babelOptions) => {
babelOptions.presets = [
[
require.resolve('babel-preset-solid'),
options.solidPresetOptions || {},
],
];
babelOptions.parserOpts = { plugins: ['jsx', 'typescript'] };
const usingHMR =
!isProd && environmentConfig.dev.hmr && target === 'web';
if (usingHMR) {
babelOptions.plugins ??= [];
babelOptions.plugins.push([
require.resolve('solid-refresh/babel'),
]);
chain.resolve.alias.merge({
'solid-refresh': require.resolve(
'solid-refresh/dist/solid-refresh.mjs',
),
});
}
return babelOptions;
},
});
},
);
},
};
}