@modern-js/main-doc 2.58.1 → 2.58.2
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/apis/app/hooks/config/icon.mdx +1 -1
- package/docs/en/apis/app/runtime/web-server/unstable_middleware.mdx +2 -2
- package/docs/en/configure/app/tools/esbuild.mdx +1 -1
- package/docs/en/configure/app/tools/swc.mdx +1 -1
- package/docs/en/guides/_meta.json +5 -0
- package/docs/en/guides/rsbuild-plugins/plugin-esbuild.mdx +205 -0
- package/docs/en/guides/rsbuild-plugins/plugin-swc.mdx +356 -0
- package/docs/zh/apis/app/hooks/config/icon.mdx +1 -1
- package/docs/zh/apis/app/runtime/web-server/unstable_middleware.mdx +2 -2
- package/docs/zh/configure/app/tools/esbuild.mdx +1 -1
- package/docs/zh/configure/app/tools/swc.mdx +1 -1
- package/docs/zh/guides/_meta.json +5 -0
- package/docs/zh/guides/rsbuild-plugins/plugin-esbuild.mdx +201 -0
- package/docs/zh/guides/rsbuild-plugins/plugin-swc.mdx +344 -0
- package/package.json +4 -4
- package/rspress.config.ts +2 -2
- package/src/components/ContentCard/index.tsx +1 -1
- package/src/components/FeatureLayout/index.tsx +1 -1
- package/src/components/Footer/index.tsx +2 -2
- package/src/components/ListCard/index.tsx +1 -1
- package/src/components/Sandpack/index.tsx +4 -2
- package/src/components/SecondaryTitle/index.tsx +1 -1
- package/src/i18n/zhCN.ts +1 -1
- package/src/index.ts +0 -1
- package/src/pages/index.tsx +13 -5
- package/src/.eslintrc.json +0 -3
@@ -42,7 +42,7 @@ When there is an `icon.*` file in the `config` directory at the root of the proj
|
|
42
42
|
After the build is completed, you can see the following tags automatically generated in HTML:
|
43
43
|
|
44
44
|
```html
|
45
|
-
<link rel="apple-touch-icon" sizes="
|
45
|
+
<link rel="apple-touch-icon" sizes="180x180" href="/static/image/icon.png" />
|
46
46
|
```
|
47
47
|
|
48
48
|
### Order
|
@@ -92,7 +92,7 @@ import {
|
|
92
92
|
} from '@modern-js/runtime/server';
|
93
93
|
import type { Vars } from '../shared/index';
|
94
94
|
|
95
|
-
const setPayload:
|
95
|
+
const setPayload: UnstableMiddlewaree<Vars> = async (
|
96
96
|
c: UnstableMiddlewareContext<Vars>,
|
97
97
|
next,
|
98
98
|
) => {
|
@@ -101,7 +101,7 @@ const setPayload: UnstableMiddleware = async (
|
|
101
101
|
await next();
|
102
102
|
};
|
103
103
|
|
104
|
-
export const unstableMiddleware: UnstableMiddleware[] = [setPayload];
|
104
|
+
export const unstableMiddleware: UnstableMiddleware<Vars>[] = [setPayload];
|
105
105
|
```
|
106
106
|
|
107
107
|
```ts title="src/routes/page.data.ts"
|
@@ -41,4 +41,4 @@ export default defineConfig({
|
|
41
41
|
});
|
42
42
|
```
|
43
43
|
|
44
|
-
For config details, please refer to [
|
44
|
+
For config details, please refer to [Esbuild Plugin Configuration](/guides/rsbuild-plugins/plugin-esbuild.html#config).
|
@@ -77,4 +77,4 @@ export default defineConfig({
|
|
77
77
|
});
|
78
78
|
```
|
79
79
|
|
80
|
-
For config details, please refer to [
|
80
|
+
For config details, please refer to [SWC Plugin Configuration](/guides/rsbuild-plugins/plugin-swc.html#config).
|
@@ -0,0 +1,205 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 3
|
3
|
+
---
|
4
|
+
|
5
|
+
# Esbuild Plugin
|
6
|
+
|
7
|
+
:::warning
|
8
|
+
**The esbuild feature in the current document is no longer maintained**, we recommend using the Rspack + SWC solution, because Rspack + SWC provide better build performance, richer features, and better compatibility.
|
9
|
+
|
10
|
+
Please refer to [「Use Rspack」](guides/advanced-features/rspack-start) for more information.
|
11
|
+
|
12
|
+
:::
|
13
|
+
|
14
|
+
import Esbuild from '@modern-js/builder-doc/docs/en/shared/esbuild.md';
|
15
|
+
|
16
|
+
<Esbuild />
|
17
|
+
|
18
|
+
## Quick Start
|
19
|
+
|
20
|
+
### Used in Modern.js framework
|
21
|
+
|
22
|
+
The Modern.js framework integrates the Builder's esbuild plugin by default, so you don't need to manually install and register the plugin, just use the [tools.esbuild](https://modernjs.dev/en/configure/app/tools/esbuild.html) configuration:
|
23
|
+
|
24
|
+
```js
|
25
|
+
export default defineConfig({
|
26
|
+
tools: {
|
27
|
+
esbuild: {
|
28
|
+
loader: {
|
29
|
+
target: 'chrome61',
|
30
|
+
},
|
31
|
+
minimize: {
|
32
|
+
target: 'chrome61',
|
33
|
+
},
|
34
|
+
},
|
35
|
+
},
|
36
|
+
});
|
37
|
+
```
|
38
|
+
|
39
|
+
## Config
|
40
|
+
|
41
|
+
The plugin will enable code transformation and minification by default. You can also customize the behavior of the plugin through configuration.
|
42
|
+
|
43
|
+
### loader
|
44
|
+
|
45
|
+
- **Type:**
|
46
|
+
|
47
|
+
```ts
|
48
|
+
type LoaderOptions = EsbuildLoaderOptions | false | undefined;
|
49
|
+
```
|
50
|
+
|
51
|
+
- **Default:**
|
52
|
+
|
53
|
+
```ts
|
54
|
+
const defaultOptions = {
|
55
|
+
target: 'es2015',
|
56
|
+
charset: builderConfig.output.charset,
|
57
|
+
};
|
58
|
+
```
|
59
|
+
|
60
|
+
This config is used to enable JavaScript/TypeScript transformation, which will replace babel-loader and ts-loader with esbuild-loader.
|
61
|
+
|
62
|
+
If you want to modify the options, you can check the [esbuild-loader documentation](https://github.com/privatenumber/esbuild-loader#loader).
|
63
|
+
|
64
|
+
#### Set JSX Format
|
65
|
+
|
66
|
+
When using esbuild for transformation, esbuild will read the `compilerOptions.jsx` field in `tsconfig.json` to determine which JSX syntax to use.
|
67
|
+
|
68
|
+
Therefore, you need to set the correct JSX syntax in `tsconfig.json`.
|
69
|
+
|
70
|
+
For example, React projects need to set `compilerOptions.jsx` to `react-jsx`:
|
71
|
+
|
72
|
+
```json
|
73
|
+
{
|
74
|
+
"compilerOptions": {
|
75
|
+
"jsx": "react-jsx"
|
76
|
+
}
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
80
|
+
#### Set the target environment
|
81
|
+
|
82
|
+
Use the `target` option to set the target environment for transformation. `target` can be set directly to the JavaScript language version, such as `es6`, `es2020`; it can also be set to several target environments, each target environment is an environment name followed by a version number, such as `['chrome58', 'edge16' ,'firefox57']`. For a detailed introduction of the `target` option, please refer to [esbuild - target](https://esbuild.github.io/api/#target).
|
83
|
+
|
84
|
+
target supports setting to the following environments:
|
85
|
+
|
86
|
+
- chrome
|
87
|
+
- edge
|
88
|
+
- firefox
|
89
|
+
- ie
|
90
|
+
- ios
|
91
|
+
- node
|
92
|
+
- opera
|
93
|
+
- safari
|
94
|
+
|
95
|
+
```ts
|
96
|
+
builderPluginEsbuild({
|
97
|
+
loader: {
|
98
|
+
target: 'chrome61',
|
99
|
+
},
|
100
|
+
});
|
101
|
+
```
|
102
|
+
|
103
|
+
#### Disable transformation
|
104
|
+
|
105
|
+
Set `loader` to `false` to disable esbuild transformation, and Builder will continue to use Babel to transform the code.
|
106
|
+
|
107
|
+
```ts
|
108
|
+
builderPluginEsbuild({
|
109
|
+
loader: false,
|
110
|
+
});
|
111
|
+
```
|
112
|
+
|
113
|
+
### minimize
|
114
|
+
|
115
|
+
- **Type:**
|
116
|
+
|
117
|
+
```ts
|
118
|
+
type MinimizeOptions = EsbuildMinifyOptions | false | undefined;
|
119
|
+
```
|
120
|
+
|
121
|
+
- **Default:**
|
122
|
+
|
123
|
+
```ts
|
124
|
+
const defaultOptions = {
|
125
|
+
css: true,
|
126
|
+
target: 'es2015',
|
127
|
+
format: builderTarget === 'web' ? 'iife' : undefined,
|
128
|
+
};
|
129
|
+
```
|
130
|
+
|
131
|
+
This option is used to enable minification for JavaScript and CSS.
|
132
|
+
|
133
|
+
If you want to modify the options, you can check the [esbuild-loader documentation](https://github.com/privatenumber/esbuild-loader#minifyplugin).
|
134
|
+
|
135
|
+
#### Set the target environment
|
136
|
+
|
137
|
+
Use the `target` option to set the target environment for minification.
|
138
|
+
|
139
|
+
```ts
|
140
|
+
builderPluginEsbuild({
|
141
|
+
minimize: {
|
142
|
+
target: 'chrome61',
|
143
|
+
},
|
144
|
+
});
|
145
|
+
```
|
146
|
+
|
147
|
+
#### Disable minification
|
148
|
+
|
149
|
+
Set `minimize` to `false` to disable esbuild minification, and Builder will continue to use Terser to minify the code.
|
150
|
+
|
151
|
+
```ts
|
152
|
+
builderPluginEsbuild({
|
153
|
+
minimize: false,
|
154
|
+
});
|
155
|
+
```
|
156
|
+
|
157
|
+
## Limitations
|
158
|
+
|
159
|
+
Although esbuild can significantly improve the build performance of existing webpack projects, it still has certain limitations that require special attention.
|
160
|
+
|
161
|
+
### Compatibility
|
162
|
+
|
163
|
+
As a compiler (i.e. `loader` capability), esbuild usually supports at least ES2015 (that is, ES6) syntax, and does not have the ability to automatically inject Polyfill.. If the production environment needs to downgrade to ES5 and below syntax, it is recommended to use SWC compilation.
|
164
|
+
|
165
|
+
You can specify the target syntax version by following config:
|
166
|
+
|
167
|
+
```ts
|
168
|
+
builderPluginEsbuild({
|
169
|
+
loader: {
|
170
|
+
target: 'es2015',
|
171
|
+
},
|
172
|
+
});
|
173
|
+
```
|
174
|
+
|
175
|
+
As a code minify tool (i.e. `minimize` capability), esbuild can minify the code in production environment, and usually supports ES2015 syntax at least.
|
176
|
+
|
177
|
+
If you set the compressed `target` to `es5`, you need to ensure that all codes have been compiled to ES5 codes, otherwise it will cause esbuild compilation error: `Transforming 'xxx' to the configured target environment ("es5") is not supported yet `.
|
178
|
+
|
179
|
+
Therefore, for projects that need to be compatible with ES5 and below syntax in the production environment, please be careful to enable the minimize capability, and it is recommended to use SWC compression.
|
180
|
+
|
181
|
+
You can specify the target syntax version by following config:
|
182
|
+
|
183
|
+
```js
|
184
|
+
builderPluginEsbuild({
|
185
|
+
minimize: {
|
186
|
+
target: 'es2015',
|
187
|
+
},
|
188
|
+
});
|
189
|
+
```
|
190
|
+
|
191
|
+
:::danger Caution
|
192
|
+
Projects that need to be compatible with ES5 and below syntax in the production environment need to be careful to turn on the minimize config.
|
193
|
+
:::
|
194
|
+
|
195
|
+
### Not support Babel plugins
|
196
|
+
|
197
|
+
As a compiler, the syntax transformation function of the original Babel plugins such as `babel-plugin-import` is not available after esbuild is turned on. And since the bottom layer of the plugin uses esbuild's `Transform API`, it does not support esbuild plugins to customize the compilation process.
|
198
|
+
|
199
|
+
If you have requirements related to Babel plugins such as `babel-plugin-import`, you can use the SWC plugin.
|
200
|
+
|
201
|
+
### Bundle Size
|
202
|
+
|
203
|
+
Although the compression speed of esbuild is faster, the compression ratio of esbuild is lower than that of terser, so the bundle size will increase, please use it according to the scenes. Generally speaking, esbuild is more suitable for scenes that are not sensitive to bundle size.
|
204
|
+
|
205
|
+
You can refer to [minification-benchmarks](https://github.com/privatenumber/minification-benchmarks) for a detailed comparison between minimizers.
|
@@ -0,0 +1,356 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 2
|
3
|
+
---
|
4
|
+
|
5
|
+
# SWC Plugin
|
6
|
+
|
7
|
+
import SWC from '@modern-js/builder-doc/docs/en/shared/swc.md';
|
8
|
+
|
9
|
+
<SWC />
|
10
|
+
|
11
|
+
## Usage Scenarios
|
12
|
+
|
13
|
+
Before using the SWC plugin, please understand the scenarios and limitations of the SWC plugin to determine whether your project is suitable for using it.
|
14
|
+
|
15
|
+
### Rspack Scenario
|
16
|
+
|
17
|
+
If you are already using Rspack as the bundler in your project, then you do not need to use the SWC plugin, because Rspack uses SWC for transpiler and minifier by default, and the SWC compilation capabilities are available out of the box.
|
18
|
+
|
19
|
+
If you have configured the current SWC plugin when using Rspack, it will not have any effect.
|
20
|
+
|
21
|
+
### Babel Plugins
|
22
|
+
|
23
|
+
If your project requires the registration of some custom Babel plugins, you will not be able to register and use Babel plugins after using SWC, since it replaces Babel as the transpiler.
|
24
|
+
|
25
|
+
For most common Babel plugins, you can find corresponding replacements in SWC, such as:
|
26
|
+
|
27
|
+
- `@babel/preset-env`: use [presetEnv](#presetenv) instead.
|
28
|
+
- `@babel/preset-react`: use [presetReact](#presetreact) instead.
|
29
|
+
- `babel-plugin-import`: use [source.transformImport](/configure/app/source/transform-import) instead.
|
30
|
+
- `babel-plugin-lodash`: use [extensions.lodash](#extensionslodash) instead.
|
31
|
+
- `@emotion/babel-plugin`: use [extensions.emotion](#extensionsemotion) instead.
|
32
|
+
- `babel-plugin-styled-components`: use [extensions.styledComponents](#extensionsstyledcomponents) instead.
|
33
|
+
- `@babel/plugin-react-transform-remove-prop-types`: use [reactUtils.removePropTypes](#extensionsreactutils) instead.
|
34
|
+
|
35
|
+
If you use Babel plugin capabilities that are not yet supported by SWC, you will no longer be able to use them after switching to SWC compilation. You can give feedback via issues under the [swc-plugins](https://github.com/web-infra-dev/swc-plugins) repository and we will evaluate if built-in support is needed.
|
36
|
+
|
37
|
+
### Bundle Size
|
38
|
+
|
39
|
+
When using SWC for code minification instead of [terser](https://github.com/terser/terser) and [cssnano](https://github.com/cssnano/cssnano), there may be a small change in the bundle size. SWC outperforms terser for JavaScript code compression and slightly underperforms cssnano for CSS code compression.
|
40
|
+
|
41
|
+
For a detailed comparison between minifiers, see [minification-benchmarks](https://github.com/privatenumber/minification-benchmarks).
|
42
|
+
|
43
|
+
## Quick Start
|
44
|
+
|
45
|
+
### Used in Modern.js framework
|
46
|
+
|
47
|
+
The Modern.js framework integrates the Builder's SWC plugin, and you can use it in the following ways:
|
48
|
+
|
49
|
+
import EnableSWC from '@modern-js/builder-doc/docs/en/shared/enableSwc.md';
|
50
|
+
|
51
|
+
<EnableSWC />
|
52
|
+
|
53
|
+
That's it! Now you can use SWC transformation and minification in your project.
|
54
|
+
|
55
|
+
## Config
|
56
|
+
|
57
|
+
- **Type:**
|
58
|
+
|
59
|
+
```ts
|
60
|
+
type PluginConfig =
|
61
|
+
| ObjPluginConfig
|
62
|
+
| ((
|
63
|
+
config: ObjPluginConfig,
|
64
|
+
utils: { mergeConfig: typeof lodash.merge; setConfig: typeof lodash.set },
|
65
|
+
) => ObjPluginConfig | void);
|
66
|
+
|
67
|
+
// SwcOptions is configurations of swc https://swc.rs/docs/configuration/compilation
|
68
|
+
interface ObjPluginConfig extends SwcOptions {
|
69
|
+
presetReact?: ReactConfig;
|
70
|
+
presetEnv?: EnvConfig;
|
71
|
+
jsMinify?: boolean | JsMinifyOptions;
|
72
|
+
cssMinify?: boolean | CssMinifyOptions;
|
73
|
+
extensions?: Extensions;
|
74
|
+
overrides?: Overrides;
|
75
|
+
}
|
76
|
+
```
|
77
|
+
|
78
|
+
The plugin configuration is based on the SWC configuration. In order to simplify some deep-level configurations and improve development experience, certain extensions have been made. When using object-based configuration, for example you can use `presetReact` and `presetEnv` to quickly configure React-related features and syntax downgrading. Other configurations will also be directly passed through to SWC.
|
79
|
+
|
80
|
+
When using function-based configuration, the default configuration generated by the plugin will be passed in, allowing you to modify it or return a new configuration.
|
81
|
+
|
82
|
+
### presetReact
|
83
|
+
|
84
|
+
- **Type:** [presetReact](https://swc.rs/docs/configuration/compilation#jsctransformreact) in SWC.
|
85
|
+
|
86
|
+
Ported from `@babel/preset-react`. The value you passed will be merged with default option.
|
87
|
+
|
88
|
+
By default, the plugin will set `runtime` field based on your `react` version, if `react` version is newer than 17.0.0, it will be set to `automatic`, otherwish `classic`.
|
89
|
+
|
90
|
+
### presetEnv
|
91
|
+
|
92
|
+
- **Type:** [presetEnv](https://swc.rs/docs/configuration/supported-browsers#options) in SWC.
|
93
|
+
|
94
|
+
Ported from `@babel/preset-env`. The value you passed will be merged with default option.
|
95
|
+
|
96
|
+
Default option is:
|
97
|
+
|
98
|
+
```ts
|
99
|
+
{
|
100
|
+
targets: '', // automatic get browserslist from your project, so you don't have to set this field
|
101
|
+
mode: 'usage',
|
102
|
+
}
|
103
|
+
```
|
104
|
+
|
105
|
+
### jsMinify
|
106
|
+
|
107
|
+
- **Type:** `boolean` or [compress configuration](https://terser.org/docs/api-reference.html#compress-options).
|
108
|
+
- **Default:** `{ compress: {}, mangle: true }`.
|
109
|
+
|
110
|
+
If set it to `false`, then SWC minification will be disabled, if set it to `true` then will it applies default option. If you pass an object, then this object will be merged with default option.
|
111
|
+
|
112
|
+
### cssMinify
|
113
|
+
|
114
|
+
- **Type:**: `boolean`
|
115
|
+
- **Default:**: `true`
|
116
|
+
|
117
|
+
Whether enable to compress CSS files by SWC. If enabled, it will improve the performance of CSS compression, but the compression ratio will be slightly reduced.
|
118
|
+
|
119
|
+
### overrides
|
120
|
+
|
121
|
+
- **Type:**
|
122
|
+
|
123
|
+
```ts
|
124
|
+
interface Overrides extends SwcOptions {
|
125
|
+
test: RegExp;
|
126
|
+
include: RegExp[];
|
127
|
+
exclude: RegExp[];
|
128
|
+
}
|
129
|
+
```
|
130
|
+
|
131
|
+
- **Default:** `undefined`
|
132
|
+
|
133
|
+
Specify special configuration for specific modules. For example if you want to set ie 11 target for `foo.ts`:
|
134
|
+
|
135
|
+
```ts
|
136
|
+
{
|
137
|
+
test: /foo.ts/,
|
138
|
+
env: { targets: 'ie 11' }
|
139
|
+
}
|
140
|
+
```
|
141
|
+
|
142
|
+
This will merged into the default configuration, and do not affect other modules.
|
143
|
+
|
144
|
+
### extensions
|
145
|
+
|
146
|
+
- **Type:** `Object`
|
147
|
+
|
148
|
+
Some plugins ported from Babel.
|
149
|
+
|
150
|
+
#### extensions.reactUtils
|
151
|
+
|
152
|
+
- **Type:** `Object`
|
153
|
+
|
154
|
+
```ts
|
155
|
+
type ReactUtilsOptions = {
|
156
|
+
autoImportReact?: boolean;
|
157
|
+
removeEffect?: boolean;
|
158
|
+
removePropTypes?: {
|
159
|
+
mode?: 'remove' | 'unwrap' | 'unsafe-wrap';
|
160
|
+
removeImport?: boolean;
|
161
|
+
ignoreFilenames?: string[];
|
162
|
+
additionalLibraries?: string[];
|
163
|
+
classNameMatchers?: string[];
|
164
|
+
};
|
165
|
+
};
|
166
|
+
```
|
167
|
+
|
168
|
+
Some little help utils for `React`.
|
169
|
+
|
170
|
+
**reactUtils.autoImportReact**
|
171
|
+
|
172
|
+
- **Type:** `boolean`
|
173
|
+
|
174
|
+
Automatically import `React` as global variable, eg: `import React from 'react'`.
|
175
|
+
Mostly used for generated `React.createElement`.
|
176
|
+
|
177
|
+
**reactUtils.removeEffect**
|
178
|
+
|
179
|
+
- **Type:** `boolean`
|
180
|
+
|
181
|
+
Remove `useEffect` call.
|
182
|
+
|
183
|
+
**reactUtils.removePropTypes**
|
184
|
+
|
185
|
+
- **Type:**
|
186
|
+
|
187
|
+
```ts
|
188
|
+
type RemovePropTypesOptions = {
|
189
|
+
mode?: 'remove' | 'unwrap' | 'unsafe-wrap';
|
190
|
+
removeImport?: boolean;
|
191
|
+
ignoreFilenames?: string[];
|
192
|
+
additionalLibraries?: string[];
|
193
|
+
classNameMatchers?: string[];
|
194
|
+
};
|
195
|
+
```
|
196
|
+
|
197
|
+
Remove `React` runtime type checking. This is ported from [@babel/plugin-react-transform-remove-prop-types](https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types), All the configurations remain the same.
|
198
|
+
|
199
|
+
#### extensions.lodash
|
200
|
+
|
201
|
+
- **Type:**
|
202
|
+
|
203
|
+
```ts
|
204
|
+
type LodashOptions = {
|
205
|
+
cwd?: string;
|
206
|
+
ids?: string[];
|
207
|
+
};
|
208
|
+
```
|
209
|
+
|
210
|
+
- **Default:**
|
211
|
+
|
212
|
+
```ts
|
213
|
+
const defaultOptions = {
|
214
|
+
cwd: process.cwd(),
|
215
|
+
ids: ['lodash', 'lodash-es'],
|
216
|
+
};
|
217
|
+
```
|
218
|
+
|
219
|
+
Ported from [babel-plugin-lodash](https://github.com/lodash/babel-plugin-lodash), it is used to automatically convert references to Lodash into on-demand imports, thereby reducing the bundle size of Lodash code.
|
220
|
+
|
221
|
+
```ts
|
222
|
+
// Input
|
223
|
+
import { get, throttle } from 'lodash';
|
224
|
+
|
225
|
+
// Output
|
226
|
+
import get from 'lodash/get';
|
227
|
+
import throttle from 'lodash/throttle';
|
228
|
+
```
|
229
|
+
|
230
|
+
#### extensions.styledComponents
|
231
|
+
|
232
|
+
- **Type:**
|
233
|
+
|
234
|
+
```ts
|
235
|
+
boolean | {
|
236
|
+
// Enabled by default in development, disabled in production to reduce file size,
|
237
|
+
// setting this will override the default for all environments.
|
238
|
+
displayName?: boolean,
|
239
|
+
// Enabled by default.
|
240
|
+
ssr?: boolean,
|
241
|
+
// Enabled by default.
|
242
|
+
fileName?: boolean,
|
243
|
+
// Empty by default.
|
244
|
+
topLevelImportPaths?: string[],
|
245
|
+
// Defaults to ["index"].
|
246
|
+
meaninglessFileNames?: string[],
|
247
|
+
// Enabled by default.
|
248
|
+
cssProp?: boolean,
|
249
|
+
// Empty by default.
|
250
|
+
namespace?: string,
|
251
|
+
};
|
252
|
+
```
|
253
|
+
|
254
|
+
This is ported by Next.js team from [babel-plugin-styled-components](https://github.com/styled-components/babel-plugin-styled-components).
|
255
|
+
|
256
|
+
#### extensions.emotion
|
257
|
+
|
258
|
+
- **Type:**
|
259
|
+
|
260
|
+
```ts
|
261
|
+
boolean | {
|
262
|
+
// default is true. It will be disabled when build type is production.
|
263
|
+
sourceMap?: boolean,
|
264
|
+
// default is 'dev-only'.
|
265
|
+
autoLabel?: 'never' | 'dev-only' | 'always',
|
266
|
+
// default is '[local]'.
|
267
|
+
// Allowed values: `[local]` `[filename]` and `[dirname]`
|
268
|
+
// This option only works when autoLabel is set to 'dev-only' or 'always'.
|
269
|
+
// It allows you to define the format of the resulting label.
|
270
|
+
// The format is defined via string where variable parts are enclosed in square brackets [].
|
271
|
+
// For example labelFormat: "my-classname--[local]", where [local] will be replaced with the name of the variable the result is assigned to.
|
272
|
+
labelFormat?: string,
|
273
|
+
// default is undefined.
|
274
|
+
// This option allows you to tell the compiler what imports it should
|
275
|
+
// look at to determine what it should transform so if you re-export
|
276
|
+
// Emotion's exports, you can still use transforms.
|
277
|
+
importMap?: {
|
278
|
+
[packageName: string]: {
|
279
|
+
[exportName: string]: {
|
280
|
+
canonicalImport?: [string, string],
|
281
|
+
styledBaseImport?: [string, string],
|
282
|
+
}
|
283
|
+
}
|
284
|
+
},
|
285
|
+
}
|
286
|
+
```
|
287
|
+
|
288
|
+
This is ported by Next.js team from [@emotion/babel-plugin](https://www.npmjs.com/package/@emotion/babel-plugin)
|
289
|
+
|
290
|
+
#### extensions.pluginImport
|
291
|
+
|
292
|
+
:::tip
|
293
|
+
Builder provides the [source.transformImport](/configure/app/source/transform-import) config, so you don't need to configure `extensions.pluginImport` manually.
|
294
|
+
:::
|
295
|
+
|
296
|
+
Ported from [babel-plugin-import](https://github.com/umijs/babel-plugin-import), configurations are the same.
|
297
|
+
|
298
|
+
Some configurations can be passed in functions, such as `customName`, `customStyleName`. These JavaScript functions will be called by Rust through Node-API, which will cause some performance overhead.
|
299
|
+
|
300
|
+
Some simple function logic can be replaced by template language. Therefore, the configuration of function items such as `customName`, `customStyleName` can also be passed in strings as templates to replace functions and improve performance.
|
301
|
+
|
302
|
+
For example:
|
303
|
+
|
304
|
+
```ts
|
305
|
+
import { MyButton as Btn } from 'foo';
|
306
|
+
```
|
307
|
+
|
308
|
+
Apply following configurations:
|
309
|
+
|
310
|
+
```ts
|
311
|
+
PluginSWC({
|
312
|
+
extensions: {
|
313
|
+
pluginImport: [
|
314
|
+
{
|
315
|
+
libraryName: 'foo',
|
316
|
+
customName: 'foo/es/{{ member }}',
|
317
|
+
},
|
318
|
+
],
|
319
|
+
},
|
320
|
+
});
|
321
|
+
```
|
322
|
+
|
323
|
+
`{{ member }}` will be replaced by the imported specifier:
|
324
|
+
|
325
|
+
```ts
|
326
|
+
import Btn from 'foo/es/MyButton';
|
327
|
+
```
|
328
|
+
|
329
|
+
Template `customName: 'foo/es/{{ member }}'` is the same as `` customName: (member) => `foo/es/${member}` ``, but template value has no performance overhead of Node-API.
|
330
|
+
|
331
|
+
The template used here is [handlebars](https://handlebarsjs.com). There are some useful builtin tools, Take the above import statement as an example:
|
332
|
+
|
333
|
+
```ts
|
334
|
+
PluginSWC({
|
335
|
+
extensions: {
|
336
|
+
pluginImport: [
|
337
|
+
{
|
338
|
+
libraryName: 'foo',
|
339
|
+
customName: 'foo/es/{{ kebabCase member }}',
|
340
|
+
},
|
341
|
+
],
|
342
|
+
},
|
343
|
+
});
|
344
|
+
```
|
345
|
+
|
346
|
+
Transformed to:
|
347
|
+
|
348
|
+
```ts
|
349
|
+
import Btn from 'foo/es/my-button';
|
350
|
+
```
|
351
|
+
|
352
|
+
In addition to `kebabCase`, there are `cameraCase`, `snakeCase`, `upperCase` and `lowerCase` can be used as well.
|
353
|
+
|
354
|
+
## Limitation
|
355
|
+
|
356
|
+
Do not support `@babel/plugin-transform-runtime` and other custom Babel plugins.
|
@@ -91,7 +91,7 @@ import {
|
|
91
91
|
} from '@modern-js/runtime/server';
|
92
92
|
import type { Vars } from '../shared/index';
|
93
93
|
|
94
|
-
const setPayload: UnstableMiddleware = async (
|
94
|
+
const setPayload: UnstableMiddleware<Vars> = async (
|
95
95
|
c: UnstableMiddlewareContext<Vars>,
|
96
96
|
next,
|
97
97
|
) => {
|
@@ -100,7 +100,7 @@ const setPayload: UnstableMiddleware = async (
|
|
100
100
|
await next();
|
101
101
|
};
|
102
102
|
|
103
|
-
export const unstableMiddleware: UnstableMiddleware[] = [setPayload];
|
103
|
+
export const unstableMiddleware: UnstableMiddleware<Vars>[] = [setPayload];
|
104
104
|
```
|
105
105
|
|
106
106
|
```ts title="src/routes/page.data.ts"
|