@modern-js/app-tools 3.9.1 → 3.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/commands/build.js +1 -2
- package/dist/cjs/commands/deploy.js +1 -3
- package/dist/cjs/commands/index.js +4 -0
- package/dist/cjs/compat/hooks.js +1 -1
- package/dist/cjs/index.js +27 -57
- package/dist/cjs/plugins/analyze/getFileSystemEntry.js +1 -1
- package/dist/cjs/plugins/analyze/getServerRoutes.js +1 -1
- package/dist/cjs/plugins/analyze/index.js +2 -2
- package/dist/cjs/plugins/analyze/utils.js +4 -5
- package/dist/cjs/plugins/initialize/index.js +3 -4
- package/dist/esm/commands/build.mjs +1 -2
- package/dist/esm/commands/deploy.mjs +1 -3
- package/dist/esm/commands/index.mjs +4 -0
- package/dist/esm/compat/hooks.mjs +1 -1
- package/dist/esm/index.mjs +2 -12
- package/dist/esm/plugins/analyze/getFileSystemEntry.mjs +1 -1
- package/dist/esm/plugins/analyze/getServerRoutes.mjs +1 -1
- package/dist/esm/plugins/analyze/index.mjs +2 -2
- package/dist/esm/plugins/analyze/utils.mjs +4 -5
- package/dist/esm/plugins/initialize/index.mjs +3 -4
- package/dist/esm-node/commands/build.mjs +1 -2
- package/dist/esm-node/commands/deploy.mjs +1 -3
- package/dist/esm-node/commands/index.mjs +4 -0
- package/dist/esm-node/compat/hooks.mjs +1 -1
- package/dist/esm-node/index.mjs +2 -12
- package/dist/esm-node/plugins/analyze/getFileSystemEntry.mjs +1 -1
- package/dist/esm-node/plugins/analyze/getServerRoutes.mjs +1 -1
- package/dist/esm-node/plugins/analyze/index.mjs +2 -2
- package/dist/esm-node/plugins/analyze/utils.mjs +4 -5
- package/dist/esm-node/plugins/initialize/index.mjs +3 -4
- package/dist/types/index.d.ts +1 -3
- package/dist/types/plugins/analyze/utils.d.ts +1 -1
- package/docs/configure/app/output/disable-svgr.md +4 -0
- package/docs/configure/app/output/svg-default-export.md +4 -0
- package/docs/configure/app/tools/less.md +94 -19
- package/docs/configure/app/tools/sass.md +87 -18
- package/docs/configure/app/tools/svgr.md +89 -0
- package/docs/guides/basic-features/static-assets/svg-assets.md +29 -17
- package/docs/llms.txt +3 -2
- package/docs/plugin/introduction.md +10 -10
- package/package.json +16 -16
- package/dist/cjs/plugins/serverRuntime.js +0 -53
- package/dist/esm/plugins/serverRuntime.mjs +0 -15
- package/dist/esm-node/plugins/serverRuntime.mjs +0 -16
- package/dist/types/plugins/serverRuntime.d.ts +0 -3
|
@@ -24,7 +24,7 @@ const analyze = ()=>({
|
|
|
24
24
|
const resolvedConfig = api.getNormalizedConfig();
|
|
25
25
|
const hooks = api.getHooks();
|
|
26
26
|
try {
|
|
27
|
-
if (checkIsBuildCommands(
|
|
27
|
+
if (checkIsBuildCommands()) fs.emptydirSync(appContext.internalDirectory);
|
|
28
28
|
} catch {}
|
|
29
29
|
const apiOnly = await isApiOnly(appContext.appDirectory, resolvedConfig.source?.entriesDir, appContext.apiDirectory);
|
|
30
30
|
const [{ getProdServerRoutes }] = await Promise.all([
|
|
@@ -118,7 +118,7 @@ const analyze = ()=>({
|
|
|
118
118
|
htmlTemplates
|
|
119
119
|
};
|
|
120
120
|
api.updateAppContext(appContext);
|
|
121
|
-
if (checkIsBuildCommands(
|
|
121
|
+
if (checkIsBuildCommands()) {
|
|
122
122
|
await hooks.generateEntryCode.call({
|
|
123
123
|
entrypoints
|
|
124
124
|
});
|
|
@@ -10,14 +10,14 @@ const walkDirectory = (dir)=>fs.readdirSync(dir).reduce((previous, filename)=>{
|
|
|
10
10
|
...previous,
|
|
11
11
|
...walkDirectory(filePath)
|
|
12
12
|
];
|
|
13
|
-
return [
|
|
13
|
+
else return [
|
|
14
14
|
...previous,
|
|
15
15
|
filePath
|
|
16
16
|
];
|
|
17
17
|
}, []);
|
|
18
18
|
const replaceWithAlias = (base, filePath, alias)=>{
|
|
19
19
|
if (filePath.includes(base)) return normalizeToPosixPath(path.join(alias, path.relative(base, filePath)));
|
|
20
|
-
return filePath;
|
|
20
|
+
else return filePath;
|
|
21
21
|
};
|
|
22
22
|
const parseModule = async ({ source, filename })=>{
|
|
23
23
|
let content = source;
|
|
@@ -53,7 +53,7 @@ const parseModule = async ({ source, filename })=>{
|
|
|
53
53
|
return await parse(content);
|
|
54
54
|
};
|
|
55
55
|
const getServerCombinedModuleFile = (internalDirectory, entryName)=>path.join(internalDirectory, entryName, 'server-loader-combined.js');
|
|
56
|
-
const checkIsBuildCommands = (
|
|
56
|
+
const checkIsBuildCommands = ()=>{
|
|
57
57
|
const buildCommands = [
|
|
58
58
|
'dev',
|
|
59
59
|
'start',
|
|
@@ -63,8 +63,7 @@ const checkIsBuildCommands = (contextCommand)=>{
|
|
|
63
63
|
'dev-worker'
|
|
64
64
|
];
|
|
65
65
|
const command = getCommand();
|
|
66
|
-
|
|
67
|
-
return 'dev' === contextCommand || 'start' === contextCommand || 'build' === contextCommand || 'deploy' === contextCommand;
|
|
66
|
+
return buildCommands.includes(command);
|
|
68
67
|
};
|
|
69
68
|
const checkIsServeCommand = ()=>{
|
|
70
69
|
const command = getCommand();
|
|
@@ -26,7 +26,7 @@ const initialize = ()=>({
|
|
|
26
26
|
api.modifyResolvedConfig(async (resolved)=>{
|
|
27
27
|
let appContext = api.getAppContext();
|
|
28
28
|
const userConfig = api.getConfig();
|
|
29
|
-
const port = await getServerPort(resolved
|
|
29
|
+
const port = await getServerPort(resolved);
|
|
30
30
|
appContext = {
|
|
31
31
|
...appContext,
|
|
32
32
|
port,
|
|
@@ -61,10 +61,9 @@ function stabilizeConfig(resolve, config, keys) {
|
|
|
61
61
|
resolve[key] = config[key] || {};
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
|
-
async function getServerPort(config
|
|
64
|
+
async function getServerPort(config) {
|
|
65
65
|
const prodPort = Number(process.env.PORT) || config.server.port || 8080;
|
|
66
|
-
|
|
67
|
-
if (isDev() && (isDevCommand() || isProgrammaticDev)) return getPort(Number(process.env.PORT) || prodPort);
|
|
66
|
+
if (isDev() && isDevCommand()) return getPort(Number(process.env.PORT) || prodPort);
|
|
68
67
|
return prodPort;
|
|
69
68
|
}
|
|
70
69
|
export default initialize;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,12 +3,10 @@ import { initAppContext } from './utils/initAppContext.js';
|
|
|
3
3
|
export * from './defineConfig.js';
|
|
4
4
|
export declare const appTools: () => CliPlugin<AppTools>;
|
|
5
5
|
export { defineConfig } from './defineConfig.js';
|
|
6
|
-
export { build } from './commands/build.js';
|
|
7
|
-
export { deploy } from './commands/deploy.js';
|
|
8
6
|
export { dev } from './commands/dev.js';
|
|
9
7
|
export { serve } from './commands/serve.js';
|
|
10
8
|
export { closeServer } from './utils/createServer.js';
|
|
11
|
-
export type {
|
|
9
|
+
export type { DeployOptions, DevOptions } from './utils/types.js';
|
|
12
10
|
export { generateWatchFiles } from './utils/generateWatchFiles.js';
|
|
13
11
|
export { resolveModernRsbuildConfig, type ResolveModernRsbuildConfigOptions, } from './rsbuild.js';
|
|
14
12
|
export * from './types/index.js';
|
|
@@ -5,6 +5,6 @@ export declare const parseModule: ({ source, filename, }: {
|
|
|
5
5
|
filename: string;
|
|
6
6
|
}) => Promise<readonly [imports: readonly import("es-module-lexer").ImportSpecifier[], exports: readonly import("es-module-lexer").ExportSpecifier[], facade: boolean, hasModuleSyntax: boolean]>;
|
|
7
7
|
export declare const getServerCombinedModuleFile: (internalDirectory: string, entryName: string) => string;
|
|
8
|
-
export declare const checkIsBuildCommands: (
|
|
8
|
+
export declare const checkIsBuildCommands: () => boolean;
|
|
9
9
|
export declare const checkIsServeCommand: () => boolean;
|
|
10
10
|
export declare const isSubDirOrEqual: (parent: string, child: string) => boolean;
|
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
- **Type:** `boolean`
|
|
4
4
|
- **Default:** `false`
|
|
5
5
|
|
|
6
|
+
:::warning Deprecated
|
|
7
|
+
`output.disableSvgr` is deprecated and will be removed in the next major version. Replace `output.disableSvgr: true` with [tools.svgr](/configure/app/tools/svgr.md) set to `false`; `output.disableSvgr: false` is the default behavior and can simply be removed. When both are set, `tools.svgr` wins.
|
|
8
|
+
:::
|
|
9
|
+
|
|
6
10
|
Whether to transform SVGs into React components. If true, will treat all .svg files as assets.
|
|
7
11
|
|
|
8
12
|
By default, when an SVG resource is referenced in a JS file, Modern.js will call SVGR to convert the SVG into a React component. If you are sure that all SVG resources in your project are not being used as React components, you can turn off this conversion by setting `disableSvgr` to true to improve build performance.
|
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
- **Type:** `'url' | 'component'`
|
|
4
4
|
- **Default:** `'url'`
|
|
5
5
|
|
|
6
|
+
:::warning Deprecated
|
|
7
|
+
`output.svgDefaultExport` is deprecated and will be removed in the next major version. Use `svgrOptions.exportType` of [tools.svgr](/configure/app/tools/svgr.md) instead: `'component'` maps to `exportType: 'default'`, `'url'` maps to `exportType: 'named'`. When both are set, `tools.svgr` wins.
|
|
8
|
+
:::
|
|
9
|
+
|
|
6
10
|
`output.svgDefaultExport` is used to configure the default export type of SVG files.
|
|
7
11
|
|
|
8
12
|
When `output.svgDefaultExport` is set to `url` , the default export of SVG files is the URL of the file. For example:
|
|
@@ -5,49 +5,78 @@
|
|
|
5
5
|
|
|
6
6
|
```js
|
|
7
7
|
const defaultOptions = {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
lessLoaderOptions: {
|
|
9
|
+
lessOptions: {
|
|
10
|
+
javascriptEnabled: true,
|
|
11
|
+
},
|
|
12
|
+
// CSS Source Map enabled by default in development environment
|
|
13
|
+
sourceMap: isDev,
|
|
10
14
|
},
|
|
11
|
-
// CSS Source Map enabled by default in development environment
|
|
12
|
-
sourceMap: isDev,
|
|
13
15
|
};
|
|
14
16
|
```
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
`tools.less` modifies the options of [@rsbuild/plugin-less](https://rsbuild.rs/plugins/list/plugin-less). It accepts the full plugin options:
|
|
19
|
+
|
|
20
|
+
| Option | Description |
|
|
21
|
+
| ------------------- | --------------------------------------------------------------------------------------------------- |
|
|
22
|
+
| `lessLoaderOptions` | Options passed to [less-loader](https://github.com/webpack-contrib/less-loader), object or function |
|
|
23
|
+
| `include` | Files handled by less-loader, defaults to `/\.less$/` |
|
|
24
|
+
| `exclude` | Files that less-loader should skip |
|
|
25
|
+
| `parallel` | Whether to compile Less modules in worker threads, defaults to `false` |
|
|
26
|
+
|
|
27
|
+
### Modifying less-loader options
|
|
17
28
|
|
|
18
|
-
|
|
29
|
+
When `lessLoaderOptions` is an `Object`, it is merged with the default config through Object.assign in a shallow way. It should be noted that `lessOptions` is merged through deepMerge in a deep way. For example:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
export default {
|
|
33
|
+
tools: {
|
|
34
|
+
less: {
|
|
35
|
+
lessLoaderOptions: {
|
|
36
|
+
lessOptions: {
|
|
37
|
+
javascriptEnabled: false,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
```
|
|
19
44
|
|
|
20
|
-
When `
|
|
45
|
+
When `lessLoaderOptions` is a `Function`, the default config is passed as the first parameter, which can be directly modified or returned as the final result. The second parameter provides some utility functions that can be called directly. For example:
|
|
21
46
|
|
|
22
47
|
```js
|
|
23
48
|
export default {
|
|
24
49
|
tools: {
|
|
25
50
|
less: {
|
|
26
|
-
|
|
27
|
-
|
|
51
|
+
lessLoaderOptions(config) {
|
|
52
|
+
// Modify the config of lessOptions
|
|
53
|
+
config.lessOptions = {
|
|
54
|
+
javascriptEnabled: false,
|
|
55
|
+
};
|
|
28
56
|
},
|
|
29
57
|
},
|
|
30
58
|
},
|
|
31
59
|
};
|
|
32
60
|
```
|
|
33
61
|
|
|
34
|
-
###
|
|
62
|
+
### Parallel compilation
|
|
35
63
|
|
|
36
|
-
|
|
64
|
+
Less compilation is pure JavaScript and runs on the Node.js main thread by default. With `parallel` enabled, Less modules are compiled in a pool of worker threads, which shortens the build when a project has many Less files.
|
|
37
65
|
|
|
38
66
|
```js
|
|
39
67
|
export default {
|
|
40
68
|
tools: {
|
|
41
|
-
less
|
|
42
|
-
|
|
43
|
-
config.lessOptions = {
|
|
44
|
-
javascriptEnabled: false,
|
|
45
|
-
};
|
|
69
|
+
less: {
|
|
70
|
+
parallel: true,
|
|
46
71
|
},
|
|
47
72
|
},
|
|
48
73
|
};
|
|
49
74
|
```
|
|
50
75
|
|
|
76
|
+
:::tip
|
|
77
|
+
Options sent to worker threads must satisfy the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). With `parallel` enabled, `lessLoaderOptions` therefore cannot contain functions, such as an `additionalData` function or a custom `implementation`.
|
|
78
|
+
:::
|
|
79
|
+
|
|
51
80
|
### Modifying Less Version
|
|
52
81
|
|
|
53
82
|
In some scenarios, if you need to use a specific version of Less instead of the built-in Less v4 in Modern.js, you can install the desired Less version in your project and set it up using the `implementation` option of the `less-loader`.
|
|
@@ -56,7 +85,9 @@ In some scenarios, if you need to use a specific version of Less instead of the
|
|
|
56
85
|
export default {
|
|
57
86
|
tools: {
|
|
58
87
|
less: {
|
|
59
|
-
|
|
88
|
+
lessLoaderOptions: {
|
|
89
|
+
implementation: require('less'),
|
|
90
|
+
},
|
|
60
91
|
},
|
|
61
92
|
},
|
|
62
93
|
};
|
|
@@ -73,8 +104,52 @@ Used to specify which files `less-loader` does not compile, You can pass in one
|
|
|
73
104
|
```js
|
|
74
105
|
export default {
|
|
75
106
|
tools: {
|
|
76
|
-
less
|
|
77
|
-
addExcludes
|
|
107
|
+
less: {
|
|
108
|
+
lessLoaderOptions(config, { addExcludes }) {
|
|
109
|
+
addExcludes(/node_modules/);
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The plugin's `exclude` option is the recommended equivalent:
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
export default {
|
|
120
|
+
tools: {
|
|
121
|
+
less: {
|
|
122
|
+
exclude: /node_modules/,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Legacy form
|
|
129
|
+
|
|
130
|
+
:::warning Do not mix the two layers
|
|
131
|
+
As soon as an object contains a plugin-level key (`lessLoaderOptions`, `include`, `exclude`, ...), the whole object is parsed as plugin options and any loader option in it has no effect. For example, `lessOptions` in `tools.less: { parallel: true, lessOptions: { ... } }` is ignored and must be moved under `lessLoaderOptions`; a hint is printed in development.
|
|
132
|
+
:::
|
|
133
|
+
|
|
134
|
+
In earlier versions, `tools.less` took the less-loader options directly, e.g. `tools.less: { lessOptions: {} }` or `tools.less(config, { addExcludes }) {}`. This form still works: Modern.js wraps it into `lessLoaderOptions` and produces exactly the same config as before. A migration hint is printed in development, and the legacy form will be removed in the next major version.
|
|
135
|
+
|
|
136
|
+
```js
|
|
137
|
+
// legacy
|
|
138
|
+
export default {
|
|
139
|
+
tools: {
|
|
140
|
+
less: {
|
|
141
|
+
lessOptions: { javascriptEnabled: false },
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// current
|
|
147
|
+
export default {
|
|
148
|
+
tools: {
|
|
149
|
+
less: {
|
|
150
|
+
lessLoaderOptions: {
|
|
151
|
+
lessOptions: { javascriptEnabled: false },
|
|
152
|
+
},
|
|
78
153
|
},
|
|
79
154
|
},
|
|
80
155
|
};
|
|
@@ -5,41 +5,64 @@
|
|
|
5
5
|
|
|
6
6
|
```js
|
|
7
7
|
const defaultOptions = {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
sassLoaderOptions: {
|
|
9
|
+
// CSS Source Map enabled by default in development environment
|
|
10
|
+
sourceMap: isDev,
|
|
11
|
+
},
|
|
10
12
|
};
|
|
11
13
|
```
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
`tools.sass` modifies the options of [@rsbuild/plugin-sass](https://rsbuild.rs/plugins/list/plugin-sass). It accepts the full plugin options:
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
| Option | Description | |
|
|
18
|
+
| ------------------- | --------------------------------------------------------------------------------------------------- | -------- |
|
|
19
|
+
| `sassLoaderOptions` | Options passed to [sass-loader](https://github.com/webpack-contrib/sass-loader), object or function | |
|
|
20
|
+
| `include` | Files handled by sass-loader, defaults to \`/.s(?:a | c)ss$/\` |
|
|
21
|
+
| `exclude` | Files that sass-loader should skip | |
|
|
22
|
+
| `rewriteUrls` | Whether to rewrite relative URLs in Sass files with resolve-url-loader, defaults to `true` | |
|
|
16
23
|
|
|
17
|
-
|
|
24
|
+
### Modifying sass-loader options
|
|
18
25
|
|
|
19
|
-
For example:
|
|
26
|
+
When `sassLoaderOptions` is an `Object`, it is merged with the default config through Object.assign in a shallow way. It should be noted that `sassOptions` is merged through deepMerge in a deep way. For example:
|
|
20
27
|
|
|
21
28
|
```js
|
|
22
29
|
export default {
|
|
23
30
|
tools: {
|
|
24
31
|
sass: {
|
|
25
|
-
|
|
32
|
+
sassLoaderOptions: {
|
|
33
|
+
sourceMap: true,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
When `sassLoaderOptions` is a `Function`, the default config is passed as the first parameter, which can be directly modified or returned as the final result. The second parameter provides some utility functions that can be called directly. For example:
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
export default {
|
|
44
|
+
tools: {
|
|
45
|
+
sass: {
|
|
46
|
+
sassLoaderOptions(config) {
|
|
47
|
+
// Modify the additionalData config
|
|
48
|
+
config.additionalData = async (content, loaderContext) => {
|
|
49
|
+
// ...
|
|
50
|
+
};
|
|
51
|
+
},
|
|
26
52
|
},
|
|
27
53
|
},
|
|
28
54
|
};
|
|
29
55
|
```
|
|
30
56
|
|
|
31
|
-
###
|
|
57
|
+
### Disabling URL rewriting
|
|
32
58
|
|
|
33
|
-
|
|
59
|
+
By default, relative URLs in Sass files are rewritten by resolve-url-loader so that they resolve relative to the source file. If your project does not rely on this, turn it off to drop one loader from the chain:
|
|
34
60
|
|
|
35
61
|
```js
|
|
36
62
|
export default {
|
|
37
63
|
tools: {
|
|
38
|
-
sass
|
|
39
|
-
|
|
40
|
-
config.additionalData = async (content, loaderContext) => {
|
|
41
|
-
// ...
|
|
42
|
-
};
|
|
64
|
+
sass: {
|
|
65
|
+
rewriteUrls: false,
|
|
43
66
|
},
|
|
44
67
|
},
|
|
45
68
|
};
|
|
@@ -53,13 +76,15 @@ In some scenarios, if you need to use a specific version of Sass instead of the
|
|
|
53
76
|
export default {
|
|
54
77
|
tools: {
|
|
55
78
|
sass: {
|
|
56
|
-
|
|
79
|
+
sassLoaderOptions: {
|
|
80
|
+
implementation: require('sass'),
|
|
81
|
+
},
|
|
57
82
|
},
|
|
58
83
|
},
|
|
59
84
|
};
|
|
60
85
|
```
|
|
61
86
|
|
|
62
|
-
###
|
|
87
|
+
### Util Function
|
|
63
88
|
|
|
64
89
|
#### addExcludes
|
|
65
90
|
|
|
@@ -70,8 +95,52 @@ Used to specify which files `sass-loader` does not compile, You can pass in one
|
|
|
70
95
|
```js
|
|
71
96
|
export default {
|
|
72
97
|
tools: {
|
|
73
|
-
sass
|
|
74
|
-
addExcludes
|
|
98
|
+
sass: {
|
|
99
|
+
sassLoaderOptions(config, { addExcludes }) {
|
|
100
|
+
addExcludes(/node_modules/);
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The plugin's `exclude` option is the recommended equivalent:
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
export default {
|
|
111
|
+
tools: {
|
|
112
|
+
sass: {
|
|
113
|
+
exclude: /node_modules/,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Legacy form
|
|
120
|
+
|
|
121
|
+
:::warning Do not mix the two layers
|
|
122
|
+
As soon as an object contains a plugin-level key (`sassLoaderOptions`, `include`, `exclude`, ...), the whole object is parsed as plugin options and any loader option in it has no effect. For example, `sassOptions` in `tools.sass: { rewriteUrls: false, sassOptions: { ... } }` is ignored and must be moved under `sassLoaderOptions`; a hint is printed in development.
|
|
123
|
+
:::
|
|
124
|
+
|
|
125
|
+
In earlier versions, `tools.sass` took the sass-loader options directly, e.g. `tools.sass: { sassOptions: {} }` or `tools.sass(config, { addExcludes }) {}`. This form still works: Modern.js wraps it into `sassLoaderOptions` and produces exactly the same config as before. A migration hint is printed in development, and the legacy form will be removed in the next major version.
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
// legacy
|
|
129
|
+
export default {
|
|
130
|
+
tools: {
|
|
131
|
+
sass: {
|
|
132
|
+
sourceMap: true,
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// current
|
|
138
|
+
export default {
|
|
139
|
+
tools: {
|
|
140
|
+
sass: {
|
|
141
|
+
sassLoaderOptions: {
|
|
142
|
+
sourceMap: true,
|
|
143
|
+
},
|
|
75
144
|
},
|
|
76
145
|
},
|
|
77
146
|
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# tools.svgr
|
|
2
|
+
|
|
3
|
+
- **Type:** `Object | Function | false`
|
|
4
|
+
- **Default:**
|
|
5
|
+
|
|
6
|
+
```js
|
|
7
|
+
const defaultOptions = {
|
|
8
|
+
mixedImport: true,
|
|
9
|
+
svgrOptions: {
|
|
10
|
+
exportType: 'named',
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`tools.svgr` modifies the options of [@rsbuild/plugin-svgr](https://rsbuild.rs/plugins/list/plugin-svgr). It accepts the full plugin options. Set it to `false` to skip registering the SVGR plugin; all `.svg` files are then treated as static assets.
|
|
16
|
+
|
|
17
|
+
| Option | Description |
|
|
18
|
+
| ----------------- | ---------------------------------------------------------------------------------------------------------- |
|
|
19
|
+
| `svgrOptions` | Options passed to [SVGR](https://react-svgr.com/docs/options/), such as `exportType`, `icon`, `svgoConfig` |
|
|
20
|
+
| `parallel` | Whether to transform SVG files in worker threads, defaults to `false` |
|
|
21
|
+
| `exclude` | SVG files that SVGR should skip |
|
|
22
|
+
| `excludeImporter` | Importer files whose SVG imports should skip SVGR |
|
|
23
|
+
| `query` | Query that triggers the SVGR transform, defaults to `/react/` |
|
|
24
|
+
| `mixedImport` | Whether a module may import both the URL and the React component, defaults to `true` |
|
|
25
|
+
|
|
26
|
+
### Merge rules
|
|
27
|
+
|
|
28
|
+
When `tools.svgr` is an `Object`, its top-level fields are merged with the defaults through Object.assign in a shallow way, and `svgrOptions` is merged one level deeper, so setting only `svgrOptions.icon` keeps the default `exportType`.
|
|
29
|
+
|
|
30
|
+
When `tools.svgr` is a `Function`, the default config is passed as the first parameter, which can be directly modified or returned as the final result. An array of objects and functions is also accepted and applied in order.
|
|
31
|
+
|
|
32
|
+
### Parallel transform
|
|
33
|
+
|
|
34
|
+
The SVGR transform is pure JavaScript and runs on the Node.js main thread by default. With many SVG files, enable `parallel` to move the transform into a pool of worker threads:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
export default {
|
|
38
|
+
tools: {
|
|
39
|
+
svgr: {
|
|
40
|
+
parallel: true,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
:::tip
|
|
47
|
+
Options sent to worker threads must satisfy the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). With `parallel` enabled, `svgrOptions` therefore cannot contain functions.
|
|
48
|
+
:::
|
|
49
|
+
|
|
50
|
+
### Changing the default export
|
|
51
|
+
|
|
52
|
+
`svgrOptions.exportType` controls what an SVG file exports by default: `'named'` exports the file URL by default and the React component as the named export `ReactComponent`; `'default'` exports the React component by default.
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
export default {
|
|
56
|
+
tools: {
|
|
57
|
+
svgr: {
|
|
58
|
+
svgrOptions: {
|
|
59
|
+
exportType: 'default',
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Disabling SVGR
|
|
67
|
+
|
|
68
|
+
If you are sure that no SVG asset in your project is used as a React component, disable SVGR to improve build performance:
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
export default {
|
|
72
|
+
tools: {
|
|
73
|
+
svgr: false,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Relation to the legacy options
|
|
79
|
+
|
|
80
|
+
`tools.svgr` replaces the two deprecated options below. They still work today and will be removed in the next major version:
|
|
81
|
+
|
|
82
|
+
| Legacy option | Replacement |
|
|
83
|
+
| -------------------------------------- | -------------------------------------------------------- |
|
|
84
|
+
| `output.svgDefaultExport: 'component'` | `tools.svgr: { svgrOptions: { exportType: 'default' } }` |
|
|
85
|
+
| `output.svgDefaultExport: 'url'` | `tools.svgr: { svgrOptions: { exportType: 'named' } }` |
|
|
86
|
+
| `output.disableSvgr: true` | `tools.svgr: false` |
|
|
87
|
+
| `output.disableSvgr: false` | Default behavior, just remove it |
|
|
88
|
+
|
|
89
|
+
When both are set, `tools.svgr` wins: once `tools.svgr` is set explicitly, `output.disableSvgr` no longer applies, and `tools.svgr.svgrOptions.exportType` overrides the value derived from `output.svgDefaultExport`.
|
|
@@ -38,16 +38,22 @@ export default () => <Logo />;
|
|
|
38
38
|
|
|
39
39
|
## Modify the Default Export
|
|
40
40
|
|
|
41
|
-
You can modify the default export of SVG files through
|
|
41
|
+
You can modify the default export of SVG files through `svgrOptions.exportType` of [tools.svgr](/configure/app/tools/svgr.md). For example, set the default export as a React component:
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
44
|
export default {
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
tools: {
|
|
46
|
+
svgr: {
|
|
47
|
+
svgrOptions: {
|
|
48
|
+
exportType: 'default',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
47
51
|
},
|
|
48
52
|
};
|
|
49
53
|
```
|
|
50
54
|
|
|
55
|
+
The legacy [output.svgDefaultExport](/configure/app/output/svg-default-export.md) config still works but is deprecated.
|
|
56
|
+
|
|
51
57
|
Then import the SVG, you'll get a React component instead of a URL:
|
|
52
58
|
|
|
53
59
|
```tsx title="src/component/Logo.tsx"
|
|
@@ -74,12 +80,12 @@ Please read the [Import Static Assets](/guides/basic-features/static-assets.md)
|
|
|
74
80
|
|
|
75
81
|
## Disable SVGR Processing
|
|
76
82
|
|
|
77
|
-
By default, when an SVG resource is referenced in a JS file, Modern.js will call SVGR to convert the SVG into a React component. If you are sure that all SVG resources in your project are not being used as React components, you can turn off this conversion by setting [
|
|
83
|
+
By default, when an SVG resource is referenced in a JS file, Modern.js will call SVGR to convert the SVG into a React component. If you are sure that all SVG resources in your project are not being used as React components, you can turn off this conversion by setting [tools.svgr](/configure/app/tools/svgr.md) to `false` to improve build performance.
|
|
78
84
|
|
|
79
85
|
```js
|
|
80
86
|
export default {
|
|
81
|
-
|
|
82
|
-
|
|
87
|
+
tools: {
|
|
88
|
+
svgr: false,
|
|
83
89
|
},
|
|
84
90
|
};
|
|
85
91
|
```
|
|
@@ -147,21 +153,27 @@ When SVGR is enabled, its default configuration is as follows:
|
|
|
147
153
|
}
|
|
148
154
|
```
|
|
149
155
|
|
|
150
|
-
|
|
156
|
+
To modify the SVGR configuration, use `svgrOptions` of [tools.svgr](/configure/app/tools/svgr.md). The `svgoConfig.plugins` you pass are deep merged with the defaults by plugin name, so only the changed part needs to be written:
|
|
151
157
|
|
|
152
158
|
```js
|
|
153
159
|
export default {
|
|
154
160
|
tools: {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
161
|
+
svgr: {
|
|
162
|
+
svgrOptions: {
|
|
163
|
+
svgoConfig: {
|
|
164
|
+
plugins: [
|
|
165
|
+
{
|
|
166
|
+
name: 'preset-default',
|
|
167
|
+
params: {
|
|
168
|
+
overrides: {
|
|
169
|
+
// add one more override on top of the default preset-default
|
|
170
|
+
removeUselessDefs: false,
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
},
|
|
176
|
+
},
|
|
165
177
|
},
|
|
166
178
|
},
|
|
167
179
|
};
|
package/docs/llms.txt
CHANGED
|
@@ -128,13 +128,14 @@
|
|
|
128
128
|
- [cssLoader](/configure/app/tools/css-loader.md): Type: Object | FunctionDefault: The config of css-loader can be modified through tools.cssLoader.
|
|
129
129
|
- [devServer](/configure/app/tools/dev-server.md): Type: ObjectDefault: {} The config of DevServer can be modified through tools.devServer. Options compress Type: booleanDefault: true Whether to enable gzip compression for served static assets. If you want to disable the gzip compression, you can set compress to false: headers Type: Record<string, string>Default: undefined Adds headers to all responses. historyApiFallback Type: boolean | ConnectHistoryApiFallbackOptionsDefault: false The index.html page will likely have to be served in place of any 404 responses. Enable devServer.historyApiFallback by setting it to true: For more options and information, see the connect-history-api-fallback documentation. proxy Type: ProxyOptions[] | Record<string, string | ProxyOptions>Default: undefined Configure proxy rules for the dev server, and forward requests to the specified service. watch Type: booleanDefault: true Whether to watch files change in directories such as mock/, server/, api/.
|
|
130
130
|
- [htmlPlugin](/configure/app/tools/html-plugin.md): Type: boolean | Object | FunctionDefault: The configs of html-rspack-plugin can be modified through tools.htmlPlugin.
|
|
131
|
-
- [less](/configure/app/tools/less.md): Type: Object | FunctionDefault:
|
|
131
|
+
- [less](/configure/app/tools/less.md): Type: Object | FunctionDefault: tools.less modifies the options of @rsbuild/plugin-less. It accepts the full plugin options: Modifying less-loader options When lessLoaderOptions is an Object, it is merged with the default config through Object.assign in a shallow way. It should be noted that lessOptions is merged through deepMerge in a deep way. For example: When lessLoaderOptions is a Function, the default config is passed as the first parameter, which can be directly modified or returned as the final result. The second parameter provides some utility functions that can be called directly. For example: Parallel compilation Less compilation is pure JavaScript and runs on the Node.js main thread by default. With parallel enabled, Less modules are compiled in a pool of worker threads, which shortens the build when a project has many Less files. Modifying Less Version In some scenarios, if you need to use a specific version of Less instead of the built-in Less v4 in Modern.js, you can install the desired Less version in your project and set it up using the implementation option of the less-loader. Util Function addExcludes Type: (excludes: RegExp | RegExp[]) => void Used to specify which files less-loader does not compile, You can pass in one or more regular expressions to match the path of less files, for example: The plugin's exclude option is the recommended equivalent: Legacy form In earlier versions, tools.less took the less-loader options directly, e.g. tools.less: { lessOptions: {} } or tools.less(config, { addExcludes }) {}. This form still works: Modern.js wraps it into lessLoaderOptions and produces exactly the same config as before. A migration hint is printed in development, and the legacy form will be removed in the next major version.
|
|
132
132
|
- [lightningcssLoader](/configure/app/tools/lightningcss-loader.md): Type: Rspack.LightningcssLoaderOptions | Function | booleanDefault: Rspack.LightningcssLoaderOptions | Function | boolean You can configure builtin:lightningcss-loader through tools.lightningcssLoader.
|
|
133
133
|
- [minifyCss](/configure/app/tools/minify-css.md): Type: Object | Function | undefinedDefault: When building for production, Modern.js will minimize the CSS code through css-minimizer-webpack-plugin. The config of css-minimizer-webpack-plugin can be modified via tools.minifyCss. Object Type When tools.minifyCss is Object type, it will be merged with the default config via Object.assign. For example, modify the preset config of cssnano: Function Type When tools.minifyCss is Function type, the default config is passed in as the first parameter, the config object can be modified directly, or a value can be returned as the final result.
|
|
134
134
|
- [postcss](/configure/app/tools/postcss.md): Type: Object | FunctionDefault: Modern.js integrates PostCSS by default, you can configure postcss-loader through tools.postcss. It should be noted that when you enable the tools.lightningcss configuration, PostCSS will be disabled by default, including postcss-loader and its default plugins.
|
|
135
135
|
- [rspack](/configure/app/tools/rspack.md): Type: Rspack.Configuration | Function | undefinedDefault: undefined tools.rspack is used to configure Rspack.
|
|
136
|
-
- [sass](/configure/app/tools/sass.md): Type: Object | FunctionDefault:
|
|
136
|
+
- [sass](/configure/app/tools/sass.md): Type: Object | FunctionDefault: tools.sass modifies the options of @rsbuild/plugin-sass. It accepts the full plugin options: Modifying sass-loader options When sassLoaderOptions is an Object, it is merged with the default config through Object.assign in a shallow way. It should be noted that sassOptions is merged through deepMerge in a deep way. For example: When sassLoaderOptions is a Function, the default config is passed as the first parameter, which can be directly modified or returned as the final result. The second parameter provides some utility functions that can be called directly. For example: Disabling URL rewriting By default, relative URLs in Sass files are rewritten by resolve-url-loader so that they resolve relative to the source file. If your project does not rely on this, turn it off to drop one loader from the chain: Modifying Sass Version In some scenarios, if you need to use a specific version of Sass instead of the built-in Dart Sass v1 in Modern.js, you can install the desired Sass version in your project and set it up using the implementation option of the sass-loader. Util Function addExcludes Type: (excludes: RegExp | RegExp[]) => void Used to specify which files sass-loader does not compile, You can pass in one or more regular expressions to match the path of sass files, for example: The plugin's exclude option is the recommended equivalent: Legacy form In earlier versions, tools.sass took the sass-loader options directly, e.g. tools.sass: { sassOptions: {} } or tools.sass(config, { addExcludes }) {}. This form still works: Modern.js wraps it into sassLoaderOptions and produces exactly the same config as before. A migration hint is printed in development, and the legacy form will be removed in the next major version.
|
|
137
137
|
- [styleLoader](/configure/app/tools/style-loader.md): Type: Object | FunctionDefault: {} The config of style-loader can be set through tools.styleLoader.
|
|
138
|
+
- [svgr](/configure/app/tools/svgr.md): Type: Object | Function | falseDefault: tools.svgr modifies the options of @rsbuild/plugin-svgr. It accepts the full plugin options. Set it to false to skip registering the SVGR plugin; all .svg files are then treated as static assets. Merge rules When tools.svgr is an Object, its top-level fields are merged with the defaults through Object.assign in a shallow way, and svgrOptions is merged one level deeper, so setting only svgrOptions.icon keeps the default exportType. When tools.svgr is a Function, the default config is passed as the first parameter, which can be directly modified or returned as the final result. An array of objects and functions is also accepted and applied in order. Parallel transform The SVGR transform is pure JavaScript and runs on the Node.js main thread by default. With many SVG files, enable parallel to move the transform into a pool of worker threads: Changing the default export svgrOptions.exportType controls what an SVG file exports by default: 'named' exports the file URL by default and the React component as the named export ReactComponent; 'default' exports the React component by default. Disabling SVGR If you are sure that no SVG asset in your project is used as a React component, disable SVGR to improve build performance: Relation to the legacy options tools.svgr replaces the two deprecated options below. They still work today and will be removed in the next major version: When both are set, tools.svgr wins: once tools.svgr is set explicitly, output.disableSvgr no longer applies, and tools.svgr.svgrOptions.exportType overrides the value derived from output.svgDefaultExport.
|
|
138
139
|
- [swc](/configure/app/tools/swc.md): Type: Object | FunctionDefault: undefined
|
|
139
140
|
- [tsChecker](/configure/app/tools/ts-checker.md): Type: Object | FunctionDefault: By default, the @rsbuild/plugin-type-check is enabled for type checking. You can use output.disableTsChecker config to disable it.
|
|
140
141
|
- [aliasStrategy](/configure/app/source/alias-strategy.md): Type: 'prefer-tsconfig' | 'prefer-alias'Default: 'prefer-tsconfig' source.aliasStrategy is used to control the priority between the paths option in tsconfig.json and the alias option in the bundler.
|