@modern-js/main-doc 2.41.0 → 2.42.1
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/apis/app/commands.mdx +3 -1
- package/docs/en/configure/app/output/enable-inline-route-manifests.mdx +14 -4
- package/docs/en/configure/app/tools/bundler-chain.mdx +0 -4
- package/docs/en/guides/advanced-features/build-performance.mdx +156 -0
- package/docs/en/guides/advanced-features/optimize-bundle.mdx +115 -0
- package/docs/en/guides/advanced-features/rspack-start.mdx +1 -1
- package/docs/en/guides/advanced-features/source-build.mdx +161 -0
- package/docs/en/guides/advanced-features/testing.mdx +2 -2
- package/docs/en/guides/advanced-features/using-storybook.mdx +1 -1
- package/docs/en/guides/advanced-features/web-server.mdx +1 -1
- package/docs/en/guides/get-started/tech-stack.mdx +1 -1
- package/docs/en/guides/troubleshooting/builder.mdx +550 -21
- package/docs/en/guides/troubleshooting/hmr.mdx +148 -0
- package/docs/zh/apis/app/commands.mdx +3 -1
- package/docs/zh/configure/app/output/enable-inline-route-manifests.mdx +6 -6
- package/docs/zh/configure/app/tools/bundler-chain.mdx +0 -4
- package/docs/zh/guides/advanced-features/build-performance.mdx +156 -0
- package/docs/zh/guides/advanced-features/optimize-bundle.mdx +115 -0
- package/docs/zh/guides/advanced-features/rspack-start.mdx +1 -1
- package/docs/zh/guides/advanced-features/source-build.mdx +161 -0
- package/docs/zh/guides/advanced-features/testing.mdx +1 -1
- package/docs/zh/guides/advanced-features/using-storybook.mdx +1 -1
- package/docs/zh/guides/advanced-features/web-server.mdx +1 -1
- package/docs/zh/guides/get-started/tech-stack.mdx +1 -1
- package/docs/zh/guides/topic-detail/generator/plugin/structure.md +1 -1
- package/docs/zh/guides/troubleshooting/builder.mdx +553 -20
- package/docs/zh/guides/troubleshooting/hmr.mdx +148 -0
- package/package.json +7 -7
@@ -4,36 +4,565 @@ sidebar_position: 3
|
|
4
4
|
|
5
5
|
# Build FAQ
|
6
6
|
|
7
|
-
If you encounter build-related problems, you can refer to the following documents
|
7
|
+
If you encounter build-related problems, you can refer to the following documents.
|
8
8
|
|
9
|
-
|
9
|
+
### How to import UI Component library on demand?
|
10
10
|
|
11
|
-
|
11
|
+
If you need to configure the on-demand import of the component library, you can configure it through [source.transformImport](/configure/app/source/transform-import.html), which is equivalent to [babel-plugin-import](https://www.npmjs.com/package/babel-plugin-import).
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
```ts
|
14
|
+
export default {
|
15
|
+
source: {
|
16
|
+
transformImport: [
|
17
|
+
{
|
18
|
+
libraryName: 'xxx-components',
|
19
|
+
libraryDirectory: 'es',
|
20
|
+
style: true,
|
21
|
+
},
|
22
|
+
],
|
23
|
+
},
|
24
|
+
};
|
25
|
+
```
|
16
26
|
|
17
|
-
|
27
|
+
---
|
28
|
+
|
29
|
+
### How to run ESLint during compilation?
|
30
|
+
|
31
|
+
For the sake of compilation performance, Modern.js will not perform ESLint verification during the compilation process by default. If you need this function, you can manually install and register the [eslint-webpack-plugin](https://github.com/webpack) in the community -contrib/eslint-webpack-plugin).
|
32
|
+
|
33
|
+
The sample code to register the plugin is as follows:
|
34
|
+
|
35
|
+
```js
|
36
|
+
import ESLintPlugin from 'eslint-webpack-plugin';
|
37
|
+
|
38
|
+
export default {
|
39
|
+
tools: {
|
40
|
+
bundlerChain(chain) {
|
41
|
+
chain.plugin('eslint-plugin').use(ESLintPlugin, [
|
42
|
+
{
|
43
|
+
extensions: ['.js', '.ts', '.jsx', 'tsx', '.mjs'],
|
44
|
+
},
|
45
|
+
]);
|
46
|
+
},
|
47
|
+
},
|
48
|
+
};
|
49
|
+
```
|
50
|
+
|
51
|
+
> For more detailed usage, please refer to the [eslint-webpack-plugin](https://github.com/webpack-contrib/eslint-webpack-plugin) documentation.
|
52
|
+
|
53
|
+
---
|
54
|
+
|
55
|
+
### How to configure CDN path for static assets?
|
56
|
+
|
57
|
+
If you need to upload static assets such as JS and CSS to CDN for use, you can set the URL prefix of static assets through the [output.assetPrefix](/configure/app/output/asset-prefix.html) configuration.
|
58
|
+
|
59
|
+
```js
|
60
|
+
export default {
|
61
|
+
output: {
|
62
|
+
assetPrefix: 'https://cdn.example.com/assets/',
|
63
|
+
},
|
64
|
+
};
|
65
|
+
```
|
66
|
+
|
67
|
+
---
|
68
|
+
|
69
|
+
### How to clear the webpack cache?
|
70
|
+
|
71
|
+
By default, Modern.js's webpack cache is generated in the `./node_modules/.cache/webpack` directory.
|
72
|
+
|
73
|
+
If you need to clear the local webpack cache, you can execute the following command:
|
74
|
+
|
75
|
+
```bash
|
76
|
+
rm -rf ./node_modules/.cache
|
77
|
+
```
|
78
|
+
|
79
|
+
---
|
80
|
+
|
81
|
+
### How to remove console after production build?
|
82
|
+
|
83
|
+
When the production environment is built, we can remove the `console` from the code, so as to avoid the log of the development environment being output to the production environment.
|
84
|
+
|
85
|
+
Modern.js provides a configuration option to remove console by default, please see [performance.removeConsole](/configure/app/performance/remove-console.html).
|
86
|
+
|
87
|
+
---
|
88
|
+
|
89
|
+
### How to view the final generated webpack / Rspack configuration?
|
90
|
+
|
91
|
+
Modern.js provides [inspect command](https://modernjs.dev/en/apis/app/commands.html) to view the final Modern.js configuration and webpack / Rspack configuration generated by the project.
|
92
|
+
|
93
|
+
```bash
|
94
|
+
➜ npx modern inspect
|
95
|
+
|
96
|
+
Inspect config succeed, open following files to view the content:
|
97
|
+
|
98
|
+
- Builder Config: /root/my-project/dist/builder.config.js
|
99
|
+
- Rspack Config (web): /root/my-project/dist/rspack.config.web.js
|
100
|
+
```
|
101
|
+
|
102
|
+
---
|
103
|
+
|
104
|
+
### How to ignore specific warnings?
|
105
|
+
|
106
|
+
By default, Modern.js will print all error and warning logs generated by the build process.
|
107
|
+
|
108
|
+
If a large number of warning logs are generated due to the three-party package, it cannot be processed temporarily, and you want to ignore it. Specific warning logs can be ignored through the build configuration provided by webpack / Rspack.
|
109
|
+
|
110
|
+
```ts
|
111
|
+
export default {
|
112
|
+
tools: {
|
113
|
+
bundlerChain: chain => {
|
114
|
+
chain.ignoreWarnings([/Using \/ for division outside of calc()/]);
|
115
|
+
},
|
116
|
+
},
|
117
|
+
};
|
118
|
+
```
|
119
|
+
|
120
|
+
For details, please refer to: [ignoreWarnings](https://webpack.js.org/configuration/other-options/#ignorewarnings).
|
121
|
+
|
122
|
+
---
|
123
|
+
|
124
|
+
### 'compilation' argument error when webpack compiling?
|
125
|
+
|
126
|
+
If the following error occurs when compiling, it is usually caused by installing the wrong version of webpack in the project, or installing multiple versions of webpack:
|
127
|
+
|
128
|
+
```bash
|
129
|
+
TypeError: The 'compilation' argument must be an instance of Compilation
|
130
|
+
```
|
131
|
+
|
132
|
+
The webpack version problem has the following situations:
|
133
|
+
|
134
|
+
1. The webpack dependency is directly declared in the project's package.json, and the version range of the webpack that the Modern.js depends on is different and cannot match the same version.
|
135
|
+
2. Multiple npm packages installed in the project all depend on webpack, and the webpack version ranges they depend on are different and cannot match the same version.
|
136
|
+
3. Due to the lock mechanism of the package manager, multiple webpack versions are generated in the lock file.
|
137
|
+
|
138
|
+
In the first case, it is recommended to remove the webpack dependency from the project's package.json. Because Modern.js encapsulates webpack-related capabilities by default, and will pass in the webpack object in the [tools.webpack](/en/configure/app/tools/webpack.html) configuration option. Therefore, in most cases, it is not recommended to install additional webpack dependencies in the project.
|
139
|
+
|
140
|
+
In the second case, it is recommended to see if you can upgrade an npm package so that its dependent webpack version range is consistent with the Modern.js. It is also possible to manually unify versions through the ability of the package manager, e.g. using [yarn resolutions](https://classic.yarnpkg.com/lang/en/docs/selective-version-resolutions/) or [pnpm overrides](https ://pnpm.io/package_json#pnpmoverrides).
|
141
|
+
|
142
|
+
If it is the third case, you can use the two methods mentioned in the second case, or you can try to delete the lock file and reinstall it to solve it.
|
143
|
+
|
144
|
+
:::tip
|
145
|
+
Deleting the lock file will automatically upgrade the dependency version in the project to the latest version under the specified scope, please test it thoroughly.
|
146
|
+
:::
|
147
|
+
|
148
|
+
---
|
149
|
+
|
150
|
+
### Find ES6+ code in the compiled files?
|
151
|
+
|
152
|
+
By default, Modern.js will not compile files under `node_modules` through `babel-loader` or `ts-loader`. If the npm package introduced by the project contains ES6+ syntax, it will be bundled into the output files.
|
153
|
+
|
154
|
+
When this happens, you can specify directories or modules that need to be compiled additionally through the [source.include](/en/configure/app/source/include.html) configuration option.
|
155
|
+
|
156
|
+
---
|
157
|
+
|
158
|
+
### Failed import other modules in Monorepo?
|
159
|
+
|
160
|
+
Due to considerations of compilation performance, by default, the Modern.js does not compile files under `node_modules` or files outside the current project directory.
|
161
|
+
|
162
|
+
Therefore, when you reference the source code of other sub-projects, you may encounter an error similar to `You may need an additional loader to handle the result of these loaders.`
|
163
|
+
|
164
|
+
There are several solutions to this problem:
|
165
|
+
|
166
|
+
1. You can enable the source code build mode to compile other sub-projects within the monorepo. Please refer to [Source Code Build Mode](/guides/advanced-features/source-build.html) for more information.
|
167
|
+
2. You can add the `source.include` configuration option to specify the directories or modules that need to be additionally compiled. Please refer to [Usage of source.include](/configure/app/source/include.html) for more information.
|
168
|
+
3. You can pre-build the sub-projects that need to be referenced, generate the corresponding build artifacts, and then reference the build artifacts in the current project instead of referencing the source code.
|
169
|
+
|
170
|
+
---
|
171
|
+
|
172
|
+
### Compile error `Error: [object Object] is not a PostCSS plugin` ?
|
173
|
+
|
174
|
+
Currently, Modern.js is using PostCSS v8. If you encounter the `Error: [object Object] is not a PostCSS plugin` error during the compilation process, it is usually caused by referencing the wrong version of PostCSS, for example, the version of `postcss` (peerDependencies) in `cssnano` does not meet expectations.
|
175
|
+
|
176
|
+
You can find the dependencies of `UNMET PEER DEPENDENCY` through `npm ls postcss`, and then install the correct version of dependencies by specifying the PostCSS version in package.json.
|
177
|
+
|
178
|
+
```
|
179
|
+
npm ls postcss
|
180
|
+
|
181
|
+
├─┬ css-loader@6.3.0
|
182
|
+
│ └── UNMET PEER DEPENDENCY postcss@8.3.9
|
183
|
+
├─┬ css-minimizer-webpack-plugin@3.0.0
|
184
|
+
│ └── UNMET PEER DEPENDENCY postcss@8.3.9
|
185
|
+
```
|
186
|
+
|
187
|
+
---
|
188
|
+
|
189
|
+
### Compile error `You may need additional loader`?
|
190
|
+
|
191
|
+
If the following error message is encountered during the compilation process, it means that there are individual files that cannot be compiled correctly.
|
192
|
+
|
193
|
+
```bash
|
194
|
+
Module parse failed: Unexpected token
|
195
|
+
File was processed with these loaders:
|
196
|
+
* some-loader/index.js
|
197
|
+
|
198
|
+
You may need an additional loader to handle the result of these loaders.
|
199
|
+
```
|
200
|
+
|
201
|
+
Solution:
|
202
|
+
|
203
|
+
- If the `.ts` file outside the current project is referenced, or the `.ts` file under node_modules, please add the [source.include](/en/configure/app/source/include.html) configuration Items that specify files that require additional compilation.
|
204
|
+
- If you refer to a file format that is not supported by Modern.js, please configure the corresponding webpack loader for compilation.
|
205
|
+
|
206
|
+
---
|
207
|
+
|
208
|
+
### Find `exports is not defined` runtime error?
|
209
|
+
|
210
|
+
If the compilation is succeed, but the `exports is not defined` error appears after opening the page, it is usually because a CommonJS module is compiled by Babel.
|
211
|
+
|
212
|
+
Under normal circumstances, Modern.js will not use Babel to compile CommonJS modules. If the [source.include](/en/configure/app/source/include.html) configuration option is used in the project, some CommonJS modules may be added to the Babel compilation.
|
213
|
+
|
214
|
+
There are two workarounds for this problem:
|
215
|
+
|
216
|
+
1. Avoid adding CommonJS modules to Babel compilation.
|
217
|
+
2. Set Babel's `sourceType` configuration option to `unambiguous`, for example:
|
218
|
+
|
219
|
+
```js
|
220
|
+
export default {
|
221
|
+
tools: {
|
222
|
+
babel(config) {
|
223
|
+
config.sourceType = 'unambiguous';
|
224
|
+
},
|
225
|
+
},
|
226
|
+
};
|
227
|
+
```
|
228
|
+
|
229
|
+
Setting `sourceType` to `unambiguous` may have some other effects, please refer to [Babel official documentation](https://babeljs.io/docs/en/options#sourcetype).
|
230
|
+
|
231
|
+
---
|
232
|
+
|
233
|
+
### Compile error "Error: ES Modules may not assign module.exports or exports.\*, Use ESM export syntax"?
|
234
|
+
|
235
|
+
If the following error occurs during compilation, it is usually because a CommonJS module is compiled with Babel in the project, and the solution is same as the above `exports is not defined` problem.
|
236
|
+
|
237
|
+
```bash
|
238
|
+
Error: ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: 581
|
239
|
+
```
|
240
|
+
|
241
|
+
For more information, please refer to issue: [babel#12731](https://github.com/babel/babel/issues/12731).
|
242
|
+
|
243
|
+
---
|
244
|
+
|
245
|
+
### Compilation error "export 'foo' (imported as 'foo') was not found in './utils'"?
|
246
|
+
|
247
|
+
If you encounter this error during the compilation process, it means that your code is referencing an export that does not exist.
|
248
|
+
|
249
|
+
For example, in the following code, `index.ts` is importing the `foo` variable from `utils.ts`, but `utils.ts` only exports the `bar` variable.
|
250
|
+
|
251
|
+
```ts
|
252
|
+
// utils.ts
|
253
|
+
export const bar = 'bar';
|
254
|
+
|
255
|
+
// index.ts
|
256
|
+
import { foo } from './utils';
|
257
|
+
```
|
258
|
+
|
259
|
+
In this case, Modern.js will throw the following error:
|
260
|
+
|
261
|
+
```bash
|
262
|
+
Compile Error:
|
263
|
+
File: ./src/index.ts
|
264
|
+
export 'foo' (imported as 'foo') was not found in './utils' (possible exports: bar)
|
265
|
+
```
|
266
|
+
|
267
|
+
If you encounter this issue, the first step is to check the import/export statements in your code and correct any invalid code.
|
268
|
+
|
269
|
+
There are some common mistakes:
|
270
|
+
|
271
|
+
- Importing a non-existent variable:
|
272
|
+
|
273
|
+
```ts
|
274
|
+
// utils.ts
|
275
|
+
export const bar = 'bar';
|
276
|
+
|
277
|
+
// index.ts
|
278
|
+
import { foo } from './utils';
|
279
|
+
```
|
280
|
+
|
281
|
+
- Re-exporting a type without adding the `type` modifier, causing compilers like Babel to fail in recognizing the type export, resulting in compilation errors.
|
282
|
+
|
283
|
+
```ts
|
284
|
+
// utils.ts
|
285
|
+
export type Foo = 'bar';
|
286
|
+
|
287
|
+
// index.ts
|
288
|
+
export { Foo } from './utils'; // Incorrect
|
289
|
+
export type { Foo } from './utils'; // Correct
|
290
|
+
```
|
291
|
+
|
292
|
+
In some cases, the error may be caused by a third-party dependency that you cannot modify directly. In this situation, if you are sure that the issue does not affect your application, you can add the following configuration to change the log level from `error` to `warn`:
|
293
|
+
|
294
|
+
```ts
|
295
|
+
export default {
|
296
|
+
tools: {
|
297
|
+
webpackChain(chain) {
|
298
|
+
chain.module.parser.merge({
|
299
|
+
javascript: {
|
300
|
+
exportsPresence: 'warn',
|
301
|
+
},
|
302
|
+
});
|
303
|
+
},
|
304
|
+
},
|
305
|
+
};
|
306
|
+
```
|
307
|
+
|
308
|
+
However, it is important to contact the developer of the third-party dependency immediately to fix the issue.
|
309
|
+
|
310
|
+
> You can refer to the webpack documentation for more details on [module.parser.javascript.exportsPresence](https://webpack.js.org/configuration/module/#moduleparserjavascriptexportspresence).
|
311
|
+
|
312
|
+
---
|
313
|
+
|
314
|
+
### The compilation progress bar is stuck, but there is no Error log in the terminal?
|
315
|
+
|
316
|
+
When the compilation progress bar is stuck, but there is no Error log on the terminal, it is usually because an exception occurred during the compilation. In some cases, when Error is caught by webpack or other modules, the error log can not be output correctly. The most common scenario is that there is an exception in the Babel config, which is caught by webpack, and webpack swallows the Error in some cases.
|
317
|
+
|
318
|
+
**Solution:**
|
319
|
+
|
320
|
+
If this problem occurs after you modify the Babel config, it is recommended to check for the following incorrect usages:
|
321
|
+
|
322
|
+
1. You have configured a plugin or preset that does not exist, maybe the name is misspelled, or it is not installed correctly.
|
323
|
+
|
324
|
+
```ts
|
325
|
+
// wrong example
|
326
|
+
export default {
|
327
|
+
tools: {
|
328
|
+
babel(config, { addPlugins }) {
|
329
|
+
// The plugin has the wrong name or is not installed
|
330
|
+
addPlugins('babel-plugin-not-exists');
|
331
|
+
},
|
332
|
+
},
|
333
|
+
};
|
334
|
+
```
|
335
|
+
|
336
|
+
2. Whether multiple babel-plugin-imports are configured, but the name of each babel-plugin-import is not declared in the third item of the array.
|
337
|
+
|
338
|
+
```ts
|
339
|
+
// wrong example
|
340
|
+
export default {
|
341
|
+
tools: {
|
342
|
+
babel(config, { addPlugins }) {
|
343
|
+
addPlugins([
|
344
|
+
[
|
345
|
+
'babel-plugin-import',
|
346
|
+
{ libraryName: 'antd', libraryDirectory: 'es' },
|
347
|
+
],
|
348
|
+
[
|
349
|
+
'babel-plugin-import',
|
350
|
+
{ libraryName: 'antd-mobile', libraryDirectory: 'es' },
|
351
|
+
],
|
352
|
+
]);
|
353
|
+
},
|
354
|
+
},
|
355
|
+
};
|
356
|
+
```
|
357
|
+
|
358
|
+
```ts
|
359
|
+
// correct example
|
360
|
+
export default {
|
361
|
+
tools: {
|
362
|
+
babel(config, { addPlugins }) {
|
363
|
+
addPlugins([
|
364
|
+
[
|
365
|
+
'babel-plugin-import',
|
366
|
+
{ libraryName: 'antd', libraryDirectory: 'es' },
|
367
|
+
'antd',
|
368
|
+
],
|
369
|
+
[
|
370
|
+
'babel-plugin-import',
|
371
|
+
{ libraryName: 'antd-mobile', libraryDirectory: 'es' },
|
372
|
+
'antd-mobile',
|
373
|
+
],
|
374
|
+
]);
|
375
|
+
},
|
376
|
+
},
|
377
|
+
};
|
378
|
+
```
|
379
|
+
|
380
|
+
In addition to the reasons mentioned above, there is another possibility that can cause Babel compilation to hang, which is when Babel compiles a large JS file exceeding 10,000 lines (usually a large file in the node_modules directory that is compiled using `source.include`).
|
381
|
+
|
382
|
+
When Babel compiles large files, the built-in babel-plugin-styled-components in Modern.js can cause the compilation to hang. There is already a [relevant issue](https://github.com/styled-components/babel-plugin-styled-components/issues/374) in the community .
|
383
|
+
|
384
|
+
In the future, Modern.js will consider removing the built-in babel-plugin-styled-components. In the current version, you can set [tools.styledComponents](/configure/app/tools/styled-components.html) to `false` to remove this plugin.
|
385
|
+
|
386
|
+
```ts title="modern.config.ts"
|
387
|
+
export default {
|
388
|
+
tools: {
|
389
|
+
styledComponents: false,
|
390
|
+
},
|
391
|
+
};
|
392
|
+
```
|
393
|
+
|
394
|
+
---
|
395
|
+
|
396
|
+
### The webpack cache does not work?
|
397
|
+
|
398
|
+
Modern.js enables webpack's persistent cache by default.
|
399
|
+
|
400
|
+
After the first compilation is completed, the cache file will be automatically generated and output to the `./node_modules/.cache/webpack` directory. When the second compilation is performed, the cache is hit and the compilation speed is greatly improved.
|
401
|
+
|
402
|
+
When configuration files such as `package.json` are modified, the cache is automatically invalidated.
|
403
|
+
|
404
|
+
If the webpack compilation cache in the project has not taken effect, you can add the following configuration for troubleshooting:
|
405
|
+
|
406
|
+
```ts
|
407
|
+
export default {
|
408
|
+
tools: {
|
409
|
+
webpack(config) {
|
410
|
+
config.infrastructureLogging = {
|
411
|
+
...config.infrastructureLogging,
|
412
|
+
debug: /webpack\.cache/,
|
413
|
+
};
|
414
|
+
},
|
415
|
+
},
|
416
|
+
};
|
417
|
+
```
|
418
|
+
|
419
|
+
After adding the above configuration, webpack will output logs for debugging. Please refer to the logs related to `PackFileCacheStrategy` to understand the cause of cache invalidation.
|
420
|
+
|
421
|
+
---
|
18
422
|
|
19
|
-
|
423
|
+
### Tree shaking does not take effect?
|
20
424
|
|
21
|
-
|
22
|
-
- How to run ESLint during compilation?
|
23
|
-
- How to configure CDN path for static assets?
|
425
|
+
Modern.js will enable the tree shaking function of webpack by default during production construction. Whether tree shaking can take effect depends on whether the business code can meet the tree shaking conditions of webpack.
|
24
426
|
|
25
|
-
|
427
|
+
If you encounter the problem that tree shaking does not take effect, you can check whether the `sideEffects` configuration of the relevant npm package is correct. If you don't know what `sideEffects` is, or are interested in the principles behind tree shaking, you can read the following two documents:
|
26
428
|
|
27
|
-
|
429
|
+
- [webpack official documentation - Tree Shaking](https://webpack.docschina.org/guides/tree-shaking/)
|
430
|
+
- [Tree Shaking Troubleshooting Guide](https://bytedance.feishu.cn/docs/doccn8E1ldDct5uv1EEDQs8Ycwe)
|
431
|
+
|
432
|
+
---
|
433
|
+
|
434
|
+
### JavaScript heap out of memory when compiling?
|
435
|
+
|
436
|
+
This error indicates that there is a memory overflow problem during the packaging process. In most cases, it is because the bundled content exceeds the default memory limit of Node.js.
|
437
|
+
|
438
|
+
In case of OOM issues, the easiest way to fix this is by increasing the memory cap, Node.js provides the `--max-old-space-size` option to set this. You can set this parameter by adding [NODE_OPTIONS](https://nodejs.org/api/cli.html#node_optionsoptions) before the CLI command.
|
439
|
+
|
440
|
+
For example, add parameters before the `modern build` command:
|
441
|
+
|
442
|
+
```diff title="package.json"
|
443
|
+
{
|
444
|
+
"scripts": {
|
445
|
+
- "build": "modern build"
|
446
|
+
+ "build": "NODE_OPTIONS=--max_old_space_size=16384 modern build"
|
447
|
+
}
|
448
|
+
}
|
449
|
+
```
|
450
|
+
|
451
|
+
If you are executing other commands, such as `modern deploy`, please add parameters before the corresponding command.
|
452
|
+
|
453
|
+
The value of the `max_old_space_size` parameter represents the upper limit of the memory size (MB). Generally, it can be set to `16384` (16GB).
|
454
|
+
|
455
|
+
The following parameters are explained in more detail in the official Node.js documentation:
|
456
|
+
|
457
|
+
- [NODE_OPTIONS](https://nodejs.org/api/cli.html#node_optionsoptions)
|
458
|
+
- [--max-old-space-size](https://nodejs.org/api/cli.html#--max-old-space-sizesize-in-megabytes)
|
459
|
+
|
460
|
+
In addition to increasing the memory limit, it is also a solution to improve efficiency by enabling some compilation strategies, please refer to [Improve Build Performance](/guides/advanced-features/build-performance.html).
|
461
|
+
|
462
|
+
If the above methods cannot solve your problem, it may be that some abnormal logic in the project has caused memory overflow. You can debug recent code changes and locate the root cause of problems. If it cannot be located, please contact us.
|
463
|
+
|
464
|
+
---
|
465
|
+
|
466
|
+
### Can't resolve 'core-js/modules/xxx.js' when compiling?
|
467
|
+
|
468
|
+
If you get an error similar to the following when compiling, it means that [core-js](https://github.com/zloirock/core-js) cannot be resolved properly in the project.
|
469
|
+
|
470
|
+
```text
|
471
|
+
Module not found: Can't resolve 'core-js/modules/es.error.cause.js'
|
472
|
+
```
|
473
|
+
|
474
|
+
Usually, you don't need to install `core-js` in the project, because the Modern.js already has a built-in `core-js` v3.
|
475
|
+
|
476
|
+
If there is an error that `core-js` cannot be found, there may be several reasons:
|
477
|
+
|
478
|
+
1. The `plugins` configured by Babel is overwritten in the project, causing the built-in `babelPluginLockCorejsVersion` does not work. In this case, just change `tools.babel` to a function:
|
479
|
+
|
480
|
+
```ts
|
481
|
+
// Wrong usage, will override Modern.js's default Babel plugins
|
482
|
+
export default {
|
483
|
+
tools: {
|
484
|
+
babel: {
|
485
|
+
plugins: ['babel-plugin-xxx'],
|
486
|
+
},
|
487
|
+
},
|
488
|
+
};
|
489
|
+
|
490
|
+
// Correct usage, add a new plugin in the default configuration instead of overriding the plugin
|
491
|
+
export default {
|
492
|
+
tools: {
|
493
|
+
babel(config) {
|
494
|
+
config.plugins.push('babel-plugin-xxx');
|
495
|
+
},
|
496
|
+
},
|
497
|
+
};
|
498
|
+
```
|
499
|
+
|
500
|
+
2. Some code in the project depends on `core-js` v2. In this case, you usually need to find out the corresponding code and upgrade `core-js` to the v3.
|
501
|
+
3. An npm package in `node_modules` imported `core-js`, but does not declare the `core-js` dependency in `dependencies`. In this case, you need to declare the `core-js` dependency in the corresponding npm package, or install a copy of `core-js` in the project root directory.
|
502
|
+
|
503
|
+
---
|
504
|
+
|
505
|
+
### Compilation error after referencing a type from lodash
|
506
|
+
|
507
|
+
If the `@types/lodash` package is installed in your project, you may import some types from `lodash`, such as the `DebouncedFunc` type:
|
508
|
+
|
509
|
+
```ts
|
510
|
+
import { debounce, DebouncedFunc } from 'lodash';
|
511
|
+
```
|
512
|
+
|
513
|
+
Modern.js will throw an error after compiling the above code:
|
514
|
+
|
515
|
+
```bash
|
516
|
+
Syntax error: /project/src/index.ts: The lodash method `DebouncedFunc` is not a known module.
|
517
|
+
Please report bugs to https://github.com/lodash/babel-plugin-lodash/issues.
|
518
|
+
```
|
519
|
+
|
520
|
+
The reason is that Modern.js has enabled the [babel-plugin-lodash](https://github.com/lodash/babel-plugin-lodash) plugin by default to optimize the bundle size of lodash, but Babel cannot distinguish between "value" and "type", which resulting in an exception in the compiled code.
|
521
|
+
|
522
|
+
The solution is to use TypeScript's `import type` syntax to explicitly declare the `DebouncedFunc` type:
|
523
|
+
|
524
|
+
```ts
|
525
|
+
import { debounce } from 'lodash';
|
526
|
+
import type { DebouncedFunc } from 'lodash';
|
527
|
+
```
|
528
|
+
|
529
|
+
:::tip
|
530
|
+
In any case, it is recommended to use `import type` to import types, this will help the compiler to identify the type.
|
531
|
+
:::
|
532
|
+
|
533
|
+
---
|
534
|
+
|
535
|
+
### Division in Less file doesn't work?
|
536
|
+
|
537
|
+
Compared with the v3 version, the Less v4 version has some differences in the way of writing division:
|
538
|
+
|
539
|
+
```less
|
540
|
+
// Less v3
|
541
|
+
.math {
|
542
|
+
width: 2px / 2; // 1px
|
543
|
+
width: 2px ./ 2; // 1px
|
544
|
+
width: (2px / 2); // 1px
|
545
|
+
}
|
546
|
+
|
547
|
+
// Less v4
|
548
|
+
.math {
|
549
|
+
width: 2px / 2; // 2px / 2
|
550
|
+
width: 2px ./ 2; // 1px
|
551
|
+
width: (2px / 2); // 1px
|
552
|
+
}
|
553
|
+
```
|
554
|
+
|
555
|
+
The built-in Less version of Modern.js is v4, and the writing method of the lower version will not take effect. Please pay attention to the distinction.
|
556
|
+
|
557
|
+
The writing of division in Less can also be modified through configuration options, see [Less - Math](https://lesscss.org/usage/#less-options-math).
|
558
|
+
|
559
|
+
---
|
28
560
|
|
29
|
-
|
30
|
-
- Compile error `You may need additional loader`?
|
31
|
-
- Find `exports is not defined` runtime error?
|
561
|
+
### Compile error ‘TypeError: Cannot delete property 'xxx' of #\<Object\>’
|
32
562
|
|
33
|
-
|
563
|
+
This error indicates that a read-only configuration option was deleted during the compilation process. Normally, we do not want any operation to directly modify the incoming configuration when compiling, but it is difficult to restrict the behavior of underlying plugins (such as postcss-loader, etc). If this error occurs, please contact the builder developer and we will need to do something special with that configuration.
|
34
564
|
|
35
|
-
|
565
|
+
The same type of error is also reported:
|
36
566
|
|
37
|
-
-
|
38
|
-
-
|
39
|
-
- HMR not working when updating React components?
|
567
|
+
- 'TypeError: Cannot add property xxx, object is not extensible'
|
568
|
+
- 'TypeError: Cannot assign to read only property 'xxx' of object '#\<Object\>'
|