@modern-js/module-tools-docs 2.11.0 → 2.13.0
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/CHANGELOG.md +19 -0
- package/docs/en/api/config/build-config.md +56 -4
- package/docs/en/api/config/dev.md +85 -0
- package/docs/en/guide/advance/asset.mdx +1 -1
- package/docs/en/guide/advance/external-dependency.mdx +6 -84
- package/docs/en/guide/advance/in-depth-about-build.md +4 -0
- package/docs/en/guide/basic/before-getting-started.md +2 -4
- package/docs/en/guide/basic/modify-output-product.md +3 -3
- package/docs/en/guide/basic/test-your-project.mdx +3 -3
- package/docs/en/guide/basic/using-storybook.mdx +1 -1
- package/docs/en/plugins/official-list/overview.md +1 -2
- package/docs/en/plugins/official-list/plugin-node-polyfill.mdx +162 -0
- package/docs/zh/api/config/build-config.md +58 -7
- package/docs/zh/api/config/dev.md +85 -0
- package/docs/zh/guide/advance/asset.mdx +1 -1
- package/docs/zh/guide/advance/external-dependency.mdx +6 -85
- package/docs/zh/guide/advance/in-depth-about-build.md +4 -0
- package/docs/zh/guide/basic/before-getting-started.md +2 -4
- package/docs/zh/guide/basic/modify-output-product.md +3 -3
- package/docs/zh/guide/basic/test-your-project.mdx +3 -3
- package/docs/zh/guide/basic/using-storybook.mdx +1 -1
- package/docs/zh/plugins/official-list/overview.md +1 -0
- package/docs/zh/plugins/official-list/plugin-node-polyfill.mdx +162 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @modern-js/module-tools-docs
|
|
2
2
|
|
|
3
|
+
## 2.13.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 7fa815b: doc: add content about dev config
|
|
8
|
+
doc: 添加关于 dev 配置的内容
|
|
9
|
+
- @modern-js/doc-tools@2.13.0
|
|
10
|
+
- @modern-js/doc-plugin-auto-sidebar@2.13.0
|
|
11
|
+
|
|
12
|
+
## 2.12.0
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- a90a6a2: feat(plugin-module-node-polyfill, module-docs): add Node Polyfill feature
|
|
17
|
+
feat(plugin-module-node-polyfill, module-docs): 添加 Node Polyfill 功能。
|
|
18
|
+
- Updated dependencies [d50aaf7]
|
|
19
|
+
- @modern-js/doc-tools@2.12.0
|
|
20
|
+
- @modern-js/doc-plugin-auto-sidebar@2.12.0
|
|
21
|
+
|
|
3
22
|
## 2.11.0
|
|
4
23
|
|
|
5
24
|
### Minor Changes
|
|
@@ -110,11 +110,12 @@ The following usage is not currently supported:
|
|
|
110
110
|
```js index.ts
|
|
111
111
|
import { ReactComponent } from './logo.svg';
|
|
112
112
|
```
|
|
113
|
+
|
|
113
114
|
:::
|
|
114
115
|
|
|
115
116
|
When enabled, the type of svg used can be modified by adding a type definition to the `modern-app-env.d.ts` file:
|
|
116
117
|
|
|
117
|
-
```
|
|
118
|
+
```ts modern-app-env.d.ts focus=1:3
|
|
118
119
|
declare module '*.svg' {
|
|
119
120
|
const src: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
|
|
120
121
|
export default src;
|
|
@@ -144,6 +145,30 @@ Automatically externalize project dependencies and peerDependencies and not pack
|
|
|
144
145
|
- type: `boolean | Object`
|
|
145
146
|
- default: `true`
|
|
146
147
|
|
|
148
|
+
When we want to turn off the default handling behavior for third-party dependencies, we can do so by:
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
export default defineConfig({
|
|
152
|
+
buildConfig: {
|
|
153
|
+
autoExternal: false,
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
This way the dependencies under `"dependencies"` and `"peerDependencies"` will be packaged. If you want to turn off the processing of only one of these dependencies, you can use the
|
|
159
|
+
`buildConfig.autoExternal` in the form of an object.
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
export default defineConfig({
|
|
163
|
+
buildConfig: {
|
|
164
|
+
autoExternal: {
|
|
165
|
+
dependencies: false,
|
|
166
|
+
peerDependencies: false,
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
147
172
|
### dependencies
|
|
148
173
|
|
|
149
174
|
Whether or not the dep dependencies of the external project are needed
|
|
@@ -216,7 +241,7 @@ The dts file generates the relevant configuration, by default it generates.
|
|
|
216
241
|
- type: `false | Object`
|
|
217
242
|
- default:
|
|
218
243
|
|
|
219
|
-
```
|
|
244
|
+
```js
|
|
220
245
|
{
|
|
221
246
|
abortOnError: true,
|
|
222
247
|
distPath: './',
|
|
@@ -333,6 +358,27 @@ Generates code for the node environment by default, you can also specify `browse
|
|
|
333
358
|
- type: `'browser' | 'node'`
|
|
334
359
|
- default: `node`
|
|
335
360
|
|
|
361
|
+
## redirect
|
|
362
|
+
|
|
363
|
+
In `buildType: 'bundleless'` build mode, the reference path is redirected to ensure that it points to the correct product, e.g:
|
|
364
|
+
|
|
365
|
+
- `import '. /index.less'` will be rewritten to `import '. /index.css'`
|
|
366
|
+
- `import icon from '. /close.svg'` would be rewritten as `import icon from '... /asset/close.svg'` to `import icon from '. /asset/close.svg'` (depending on the situation)
|
|
367
|
+
|
|
368
|
+
In some scenarios, you may not need these functions, then you can turn it off with this configuration, and its reference path will not be changed after turning it off.
|
|
369
|
+
|
|
370
|
+
```ts
|
|
371
|
+
export default {
|
|
372
|
+
buildConfig: {
|
|
373
|
+
redirect: {
|
|
374
|
+
alias: false, // Turn off modifying alias paths
|
|
375
|
+
style: false, // Turn off modifying the path to the style file
|
|
376
|
+
asset: false, // Turn off modifying the path to the resource file
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
};
|
|
380
|
+
```
|
|
381
|
+
|
|
336
382
|
## sideEffects
|
|
337
383
|
|
|
338
384
|
Module sideEffects
|
|
@@ -340,7 +386,7 @@ Module sideEffects
|
|
|
340
386
|
- Type: `RegExg[] | (filePath: string, isExternal: boolean) => boolean | boolean`
|
|
341
387
|
- Default value: `undefined`
|
|
342
388
|
|
|
343
|
-
Normally, we configure the module's side effects via the sideEffects field in package.json, but in some cases,
|
|
389
|
+
Normally, we configure the module's side effects via the sideEffects field in package.json, but in some cases, The package.json of a three-party package is unreliable.Such as when we reference a three-party package style file
|
|
344
390
|
|
|
345
391
|
```js
|
|
346
392
|
import 'other-package/dist/index.css';
|
|
@@ -354,7 +400,13 @@ But the package.json of this three-party package does not have the style file co
|
|
|
354
400
|
}
|
|
355
401
|
```
|
|
356
402
|
|
|
357
|
-
|
|
403
|
+
At the same time you set [style.object](#inject) to `true` and you will see a warning message like this in the console
|
|
404
|
+
|
|
405
|
+
```bash
|
|
406
|
+
[LIBUILD:ESBUILD_WARN] Ignoring this import because "other-package/dist/index.css" was marked as having no side effects
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
At this point, you can use this configuration item to manually configure the module's `"sideEffects"` to support regular and functional forms.
|
|
358
410
|
|
|
359
411
|
```js modern.config.ts
|
|
360
412
|
import { defineConfig } from '@modern-js/module-tools';
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_position: 3
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Dev Config
|
|
6
|
+
|
|
7
|
+
This section describes all configuration of Module Tools related to debugging tools.
|
|
8
|
+
|
|
9
|
+
``` ts
|
|
10
|
+
export default {
|
|
11
|
+
dev: {
|
|
12
|
+
storybook: {/* Storybook Config */},
|
|
13
|
+
},
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## storybook
|
|
18
|
+
|
|
19
|
+
**Requirements**:
|
|
20
|
+
|
|
21
|
+
- Turn on Storybook debugging or install the `@modern-js/plugin-storybook` plugin.
|
|
22
|
+
- Register the `@modern-js/plugin-storybook` plugin。
|
|
23
|
+
|
|
24
|
+
> For more information on how to enable Storybook debugging, please refer to:[【Storybook】](guide/basic/use-micro-generator#storybook)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### storybook.webpack
|
|
29
|
+
|
|
30
|
+
- **Type**: `Object` | `Function` | `undefined`
|
|
31
|
+
|
|
32
|
+
- **Default**: `undefined`
|
|
33
|
+
|
|
34
|
+
``` ts
|
|
35
|
+
export default {
|
|
36
|
+
dev: {
|
|
37
|
+
storybook: {
|
|
38
|
+
webpack(config) {
|
|
39
|
+
return config;
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
You can modify the webpack configuration of the Storybook Preview-iframe via `dev.storybook.webpack`. The usage can be found in the [`tools.webpack`](https://modernjs.dev/builder/api/config-tools.html#tools.webpack) configuration of Modern.js Builder.
|
|
47
|
+
|
|
48
|
+

|
|
49
|
+
|
|
50
|
+
:::tip
|
|
51
|
+
For the webpack configuration of the Storybook Manager app section, you can configure it by adding the `./config/storybook/main.js` file to configure it.
|
|
52
|
+
|
|
53
|
+
``` js
|
|
54
|
+
// ./config/storybook/main.js
|
|
55
|
+
|
|
56
|
+
module.exports = {
|
|
57
|
+
// it controls the Storybook manager app
|
|
58
|
+
managerWebpack: async (config, options) => {
|
|
59
|
+
// update config here
|
|
60
|
+
return config;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
```
|
|
64
|
+
:::
|
|
65
|
+
|
|
66
|
+
### storybook.webpackChain
|
|
67
|
+
|
|
68
|
+
- **Type**: `Function` | `undefined`
|
|
69
|
+
|
|
70
|
+
- **Default**: `undefined`
|
|
71
|
+
|
|
72
|
+
``` ts
|
|
73
|
+
export default {
|
|
74
|
+
dev: {
|
|
75
|
+
storybook: {
|
|
76
|
+
webpackChain(chain) {
|
|
77
|
+
return chain;
|
|
78
|
+
},
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
You can modify the webpack configuration of the Storybook Preview-iframe via `dev.storybook.webpackChain`. You can refer to Modern.js Builder's [`tools.webpackChain`](https://modernjs.dev/builder/api/config-tools.html#tools.webpackchain) configuration for how to use it.
|
|
85
|
+
|
|
@@ -17,7 +17,7 @@ By default, module projects are processed for the following static resources:
|
|
|
17
17
|
For the handling of static files, the module project currently supports the following functions.
|
|
18
18
|
|
|
19
19
|
- Set the static resource path to `. /assets`.
|
|
20
|
-
-
|
|
20
|
+
- Files less than **10kb** will be inlined into the code.
|
|
21
21
|
|
|
22
22
|
Let us look at the following example:
|
|
23
23
|
|
|
@@ -19,11 +19,11 @@ In addition to `"dependencies"`, [`"peerDependencies"`](/en/guide/basic/before-g
|
|
|
19
19
|
|
|
20
20
|
## Default handling of third-party dependencies
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
By default, third-party dependencies under `"dependencies"` and `"peerDependencies"` are not packaged in the module project\*\*.
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
- If you need to package, move `"dependencies"` to `"peerDependencies"`, which is equivalent to a **prebundle** of dependencies to reduce the size of the dependency installation.
|
|
24
|
+
This is because when the npm package is installed, its `"dependencies"` will also be installed. By not packaging `"dependencies"`, you can reduce the size of the package product.
|
|
26
25
|
|
|
26
|
+
If you need to package some dependencies, it is recommended to move them from `"dependencies"` to `"devDependencies"`, which is equivalent to **prebundle** the dependencies and reduces the size of the dependency installation.
|
|
27
27
|
|
|
28
28
|
<CH.Spotlight>
|
|
29
29
|
|
|
@@ -79,88 +79,10 @@ If you want to modify the default processing, you can use the following API.
|
|
|
79
79
|
|
|
80
80
|
- [`buildConfig.autoExternal`](/api/config/build-config#autoexternal)
|
|
81
81
|
|
|
82
|
-
|
|
82
|
+
## Exclude specified third-party dependencies
|
|
83
83
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
```ts
|
|
87
|
-
export default defineConfig({
|
|
88
|
-
buildConfig: {
|
|
89
|
-
autoExternal: false,
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
This way the dependencies under `"dependencies"` and `"peerDependencies"` will be packaged. If you want to turn off the processing of only one of these dependencies, you can use the
|
|
95
|
-
`buildConfig.autoExternal` in the form of an object.
|
|
96
|
-
|
|
97
|
-
```ts
|
|
98
|
-
export default defineConfig({
|
|
99
|
-
buildConfig: {
|
|
100
|
-
autoExternal: {
|
|
101
|
-
dependencies: false,
|
|
102
|
-
peerDependencies: false,
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
## Packaging third-party dependencies
|
|
109
|
-
|
|
110
|
-
However there are cases where you want to package these third-party dependencies into the product. The benefits of packaging third-party dependencies into the product are:**Less time spent downloading third-party dependencies**. This way of handling third-party dependencies is generally more common in developing command-line tools, which can improve the loading speed of command-line tools and give users a better experience.
|
|
111
|
-
|
|
112
|
-
So how do you enable the packaging of third-party dependencies in the module project?
|
|
113
|
-
|
|
114
|
-
**When we want to package certain dependencies, we generally declare them in `"devDependencies"` in package.json**. In this case, the dependencies declared in `"devDependencies"` are **allowed** to be packaged in the product during the build process.
|
|
115
|
-
|
|
116
|
-
<CH.Spotlight>
|
|
117
|
-
|
|
118
|
-
```json ./v1/package.json
|
|
119
|
-
{
|
|
120
|
-
"devDependencies": {
|
|
121
|
-
"react": "^17"
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
---
|
|
127
|
-
|
|
128
|
-
If the project needs to package `react` dependencies.
|
|
129
|
-
|
|
130
|
-
```json ./v1/package.json
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
---
|
|
134
|
-
|
|
135
|
-
When a `react` dependency is used in the source code.
|
|
136
|
-
|
|
137
|
-
```tsx src/index.ts
|
|
138
|
-
import React from 'react';
|
|
139
|
-
console.info(React);
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
---
|
|
143
|
-
|
|
144
|
-
The `react` code will be packaged into the product at this point.
|
|
145
|
-
|
|
146
|
-
```js dist/index.js
|
|
147
|
-
// ...
|
|
148
|
-
var import_react = __toESM(require_react());
|
|
149
|
-
function src_default() {
|
|
150
|
-
console.info(import_react.default);
|
|
151
|
-
}
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
</CH.Spotlight>
|
|
155
|
-
|
|
156
|
-
However, in a real build scenario, you may encounter a third-party dependency that cannot be handled, so you can use the following API to **external** them.
|
|
157
|
-
|
|
158
|
-
- [`buildConfig.externals`](/en/api/config/build-config#externals)
|
|
159
|
-
|
|
160
|
-
### Exclude specified third-party dependencies
|
|
161
|
-
|
|
162
|
-
We mentioned above the use of the `buildConfig.autoExternal` API, while `buildConfig.externals` can also control which third-party dependencies to handle. So we can combine these two
|
|
163
|
-
APIs to handle the project's dependencies in a more granular way.
|
|
84
|
+
We mentioned above the use of the `buildConfig.autoExternal` API, and [`buildConfig.externals`](/en/api/config/build-config#externals) can control which third-party dependencies to handle
|
|
85
|
+
the project's dependencies in a more granular way.
|
|
164
86
|
|
|
165
87
|
For example, when we need to leave only certain dependencies unpacked, we can configure it as follows.
|
|
166
88
|
|
|
@@ -30,6 +30,10 @@ They have their own benefits.
|
|
|
30
30
|
- Bundle can reduce the size of build products and also pre-package dependencies to reduce the size of installed dependencies. Packaging libraries in advance can speed up application project builds.
|
|
31
31
|
- Bundleless maintains the original file structure and is more conducive to debugging and tree shaking.
|
|
32
32
|
|
|
33
|
+
:::warning
|
|
34
|
+
Bundleless is a single file compilation mode, so for type references and exports you need to add the `type` field, e.g. `import type { A } from '. /types`
|
|
35
|
+
:::
|
|
36
|
+
|
|
33
37
|
In `buildConfig` you can specify whether the current build task is Bundle or Bundleless by using [`buildConfig.buildType`](/en/api/config/build-config#buildtype).
|
|
34
38
|
|
|
35
39
|
### Relationship between `input` and `sourceDir`
|
|
@@ -166,8 +166,7 @@ The Module Tools configuration file - `modern.config.(j|t)s` - is provided in th
|
|
|
166
166
|
|
|
167
167
|
By default, the contents of the generated configuration file are as follows.
|
|
168
168
|
|
|
169
|
-
```
|
|
170
|
-
// modern.config.ts
|
|
169
|
+
```ts title="modern.config.ts"
|
|
171
170
|
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
172
171
|
|
|
173
172
|
export default defineConfig({
|
|
@@ -178,8 +177,7 @@ export default defineConfig({
|
|
|
178
177
|
|
|
179
178
|
**We recommend using the `defineConfig` function**, but it is not mandatory to use it. So you can also return an object directly in the config file: the
|
|
180
179
|
|
|
181
|
-
``` ts
|
|
182
|
-
// modern.config.ts
|
|
180
|
+
``` ts title="modern.config.ts"
|
|
183
181
|
import moduleTools from '@modern-js/module-tools';
|
|
184
182
|
|
|
185
183
|
export default {
|
|
@@ -9,7 +9,7 @@ sidebar_position: 3
|
|
|
9
9
|
|
|
10
10
|
When the `modern build` command is used in an initialized project, the products are generated according to the default configuration supported by Module Tools. The default supported configurations are specified as follows.
|
|
11
11
|
|
|
12
|
-
```
|
|
12
|
+
```ts title="modern.config.ts"
|
|
13
13
|
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
14
14
|
|
|
15
15
|
export default defineConfig({
|
|
@@ -58,7 +58,7 @@ For example, if the output product is based on the preset string `"npm-library"`
|
|
|
58
58
|
|
|
59
59
|
For example, to achieve the same effect as the preset string ``npm-library-es5"` using the form of a preset function, you can do the following.
|
|
60
60
|
|
|
61
|
-
```
|
|
61
|
+
```ts title="modern.config.ts"
|
|
62
62
|
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
63
63
|
|
|
64
64
|
export default defineConfig({
|
|
@@ -131,7 +131,7 @@ In addition to the above categories, frequently asked questions and best practic
|
|
|
131
131
|
|
|
132
132
|
`buildConfig` is one of the methods used to modify the product, **only `buildConfig` will take effect when configured in conjunction with `buildPreset`**. So if configured as follows.
|
|
133
133
|
|
|
134
|
-
```
|
|
134
|
+
```ts title="modern.config.ts"
|
|
135
135
|
import { defineConfig } from '@modern-js/module-tools';
|
|
136
136
|
|
|
137
137
|
export default defineConfig({
|
|
@@ -47,7 +47,7 @@ For common modules, we can use the test function as follows:
|
|
|
47
47
|
|
|
48
48
|
<CH.Spotlight>
|
|
49
49
|
|
|
50
|
-
```
|
|
50
|
+
```ts ./src/index.ts
|
|
51
51
|
export default function () {
|
|
52
52
|
return 'hello world';
|
|
53
53
|
}
|
|
@@ -57,7 +57,7 @@ export default function () {
|
|
|
57
57
|
|
|
58
58
|
First is the code of the module.
|
|
59
59
|
|
|
60
|
-
```
|
|
60
|
+
```ts ./src/index.ts
|
|
61
61
|
export default function () {
|
|
62
62
|
return 'hello world';
|
|
63
63
|
}
|
|
@@ -69,7 +69,7 @@ Then in the test file, we can do this.
|
|
|
69
69
|
|
|
70
70
|
Where `@` points to the source directory, defined in `tests/tsconfig.json` in the initialization project.
|
|
71
71
|
|
|
72
|
-
```
|
|
72
|
+
```ts ./tests/index.test.ts
|
|
73
73
|
import main from '@/index';
|
|
74
74
|
|
|
75
75
|
describe('default cases', () => {
|
|
@@ -4,5 +4,4 @@
|
|
|
4
4
|
|
|
5
5
|
* [@modern-js/plugin-module-import](./plugin-import.md):Use SWC to provide the same ability as [`babel-plugin-import`](https://github.com/umijs/babel-plugin-import).
|
|
6
6
|
* [@modern-js/plugin-module-banner](./plugin-banner.md):Add custom content, such as copyright information, to the top and bottom of each JS and CSS file.
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
* [@modern-js/plugin-module-node-polyfill](./plugin-node-polyfill.mdx): Inject Polyfills of Node core modules in the browser side.
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Node Polyfill Plugin
|
|
2
|
+
|
|
3
|
+
:::tip About Node Polyfill
|
|
4
|
+
Normally, we don't need to use Node libs on the browser side. However, it is possible to use some Node libs when the code will run on both the Node side and the browser side, and Node Polyfill provides browser versions of polyfills for these Node libs.
|
|
5
|
+
:::
|
|
6
|
+
|
|
7
|
+
By using the Node Polyfill plugin, Node core libs polyfills are automatically injected into the browser-side, allowing you to use these modules on the browser side with confidence.
|
|
8
|
+
|
|
9
|
+
## Quick Start
|
|
10
|
+
|
|
11
|
+
### Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# npm
|
|
15
|
+
npm install @modern-js/plugin-module-node-polyfill -D
|
|
16
|
+
|
|
17
|
+
# yarn
|
|
18
|
+
yarn add @modern-js/plugin-module-node-polyfill -D
|
|
19
|
+
|
|
20
|
+
# pnpm
|
|
21
|
+
pnpm add @modern-js/plugin-module-node-polyfill -D
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Register
|
|
25
|
+
|
|
26
|
+
In Module Tools, you can register plugins in the following way:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
30
|
+
import { modulePluginNodePolyfill } from '@modern-js/plugin-module-node-polyfill';
|
|
31
|
+
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
plugins: [
|
|
34
|
+
moduleTools(),
|
|
35
|
+
modulePluginNodePolyfill(),
|
|
36
|
+
],
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Configurations
|
|
41
|
+
|
|
42
|
+
* **Type**:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
type NodePolyfillOptions = {
|
|
46
|
+
exclude?: string[];
|
|
47
|
+
overrides?: Record<NodePolyfillKey, string>;
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### exclude
|
|
52
|
+
|
|
53
|
+
Exclude the Node Polyfill to be injected.
|
|
54
|
+
|
|
55
|
+
``` ts focus=7:9
|
|
56
|
+
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
57
|
+
import { modulePluginNodePolyfill } from '@modern-js/plugin-module-node-polyfill';
|
|
58
|
+
|
|
59
|
+
export default defineConfig({
|
|
60
|
+
plugins: [
|
|
61
|
+
moduleTools(),
|
|
62
|
+
modulePluginNodePolyfill({
|
|
63
|
+
exclude: ['console'],
|
|
64
|
+
}),
|
|
65
|
+
],
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### overrides
|
|
70
|
+
|
|
71
|
+
Override the built-in Node Polyfill.
|
|
72
|
+
|
|
73
|
+
``` ts focus=7:9
|
|
74
|
+
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
75
|
+
import { modulePluginNodePolyfill } from '@modern-js/plugin-module-node-polyfill';
|
|
76
|
+
|
|
77
|
+
export default defineConfig({
|
|
78
|
+
plugins: [
|
|
79
|
+
moduleTools(),
|
|
80
|
+
modulePluginNodePolyfill({
|
|
81
|
+
overrides: {
|
|
82
|
+
fs: path.join(__dirname, './custom-fs.js'),
|
|
83
|
+
}
|
|
84
|
+
}),
|
|
85
|
+
],
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
## Node Polyfills
|
|
91
|
+
|
|
92
|
+
### Globals
|
|
93
|
+
|
|
94
|
+
- `Buffer`
|
|
95
|
+
- `process`
|
|
96
|
+
- `console`
|
|
97
|
+
|
|
98
|
+
When the above global variables are used directly in code, the corresponding polyfill will be injected.
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const bufferData = Buffer.from('xxxx');
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Modules
|
|
105
|
+
|
|
106
|
+
- `assert`
|
|
107
|
+
- `buffer`
|
|
108
|
+
- `console`
|
|
109
|
+
- `constants`
|
|
110
|
+
- `crypto`
|
|
111
|
+
- `domain`
|
|
112
|
+
- `events`
|
|
113
|
+
- `http`
|
|
114
|
+
- `https`
|
|
115
|
+
- `os`
|
|
116
|
+
- `path`
|
|
117
|
+
- `punycode`
|
|
118
|
+
- `process`
|
|
119
|
+
- `querystring`
|
|
120
|
+
- `stream`
|
|
121
|
+
- `_stream_duplex`
|
|
122
|
+
- `_stream_passthrough`
|
|
123
|
+
- `_stream_readable`
|
|
124
|
+
- `_stream_transform`
|
|
125
|
+
- `_stream_writable`
|
|
126
|
+
- `string_decoder`
|
|
127
|
+
- `sys`
|
|
128
|
+
- `timers`
|
|
129
|
+
- `tty`
|
|
130
|
+
- `url`
|
|
131
|
+
- `util`
|
|
132
|
+
- `vm`
|
|
133
|
+
- `zlib`
|
|
134
|
+
|
|
135
|
+
When the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { Buffer } from 'buffer';
|
|
139
|
+
|
|
140
|
+
const bufferData = Buffer.from('xxxx');
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Fallbacks
|
|
144
|
+
|
|
145
|
+
- `child_process`
|
|
146
|
+
- `cluster`
|
|
147
|
+
- `dgram`
|
|
148
|
+
- `dns`
|
|
149
|
+
- `fs`
|
|
150
|
+
- `module`
|
|
151
|
+
- `net`
|
|
152
|
+
- `readline`
|
|
153
|
+
- `repl`
|
|
154
|
+
- `tls`
|
|
155
|
+
|
|
156
|
+
Currently there is no polyfill for the above modules on the browser side, so when you import the above modules, it will automatically fallback to an empty object.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import fs from 'fs';
|
|
160
|
+
|
|
161
|
+
console.log(fs); // -> {}
|
|
162
|
+
```
|
|
@@ -124,7 +124,7 @@ import { ReactComponent } from './logo.svg';
|
|
|
124
124
|
|
|
125
125
|
当开启功能后,可以通过在 `modern-app-env.d.ts` 文件中增加类型定义,修改使用 SVG 的类型:
|
|
126
126
|
|
|
127
|
-
```
|
|
127
|
+
```ts modern-app-env.d.ts focus=1:3
|
|
128
128
|
declare module '*.svg' {
|
|
129
129
|
const src: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
|
|
130
130
|
export default src;
|
|
@@ -154,6 +154,30 @@ declare module '*.svg' {
|
|
|
154
154
|
- 类型: `boolean | Object`
|
|
155
155
|
- 默认值: `true`
|
|
156
156
|
|
|
157
|
+
当我们希望关闭对于第三方依赖的默认处理行为时候,可以通过以下方式来实现:
|
|
158
|
+
|
|
159
|
+
```ts modern.config.ts
|
|
160
|
+
export default defineConfig({
|
|
161
|
+
buildConfig: {
|
|
162
|
+
autoExternal: false,
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
这样对于 `"dependencies"` 和 `"peerDependencies"` 下面的依赖都会进行打包处理。如果只想要关闭其中某个下面的依赖处理,则可以使用
|
|
168
|
+
`buildConfig.autoExternal` 的对象形式:
|
|
169
|
+
|
|
170
|
+
```ts modern.config.ts
|
|
171
|
+
export default defineConfig({
|
|
172
|
+
buildConfig: {
|
|
173
|
+
autoExternal: {
|
|
174
|
+
dependencies: false,
|
|
175
|
+
peerDependencies: false,
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
157
181
|
### dependencies
|
|
158
182
|
|
|
159
183
|
是否需要外置项目的 `"dependencies"` 依赖。
|
|
@@ -258,7 +282,7 @@ export default defineConfig({
|
|
|
258
282
|
- 类型: `false | Object`
|
|
259
283
|
- 默认值:
|
|
260
284
|
|
|
261
|
-
```
|
|
285
|
+
```js
|
|
262
286
|
{
|
|
263
287
|
abortOnError: true,
|
|
264
288
|
distPath: './',
|
|
@@ -272,7 +296,7 @@ export default defineConfig({
|
|
|
272
296
|
在出现类型错误的时候,是否允许构建成功。**默认情况下,在出现类型错误的时候会导致构建失败**。
|
|
273
297
|
|
|
274
298
|
:::warning
|
|
275
|
-
当关闭该配置后,无法保证类型文件能正常生成,且不保证内容正确。在 `buildType: 'bundle'
|
|
299
|
+
当关闭该配置后,无法保证类型文件能正常生成,且不保证内容正确。在 `buildType: 'bundle'`,即打包模式下,类型文件一定不会生成。
|
|
276
300
|
:::
|
|
277
301
|
|
|
278
302
|
- 类型:`boolean`
|
|
@@ -299,7 +323,6 @@ TypeScript 配置文件的路径。
|
|
|
299
323
|
- 类型: `string`
|
|
300
324
|
- 默认值: `./tsconfig.json`
|
|
301
325
|
|
|
302
|
-
|
|
303
326
|
## externals
|
|
304
327
|
|
|
305
328
|
配置外部依赖,不会被打包到最终的 bundle 中。
|
|
@@ -380,6 +403,27 @@ export default defineConfig({
|
|
|
380
403
|
- 类型: `'browser' | 'node'`
|
|
381
404
|
- 默认值: `node`
|
|
382
405
|
|
|
406
|
+
## redirect
|
|
407
|
+
|
|
408
|
+
在 `buildType: 'bundleless'` 构建模式下,会对引用路径进行重定向,确保指向了正确的产物,例如:
|
|
409
|
+
|
|
410
|
+
- `import './index.less'` 会被改写成 `import './index.css'`
|
|
411
|
+
- `import icon from './close.svg'` 会被改写成 `import icon from '../asset/close.svg'`(依实际情况而定)
|
|
412
|
+
|
|
413
|
+
在某些场景下,你可能不需要这些功能,那么可以通过此配置关闭它,关闭后,其引用路径将不会发生改变。
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
export default {
|
|
417
|
+
buildConfig: {
|
|
418
|
+
redirect: {
|
|
419
|
+
alias: false, // 关闭对别名路径的修改
|
|
420
|
+
style: false, // 关闭对样式文件路径的修改
|
|
421
|
+
asset: false, // 关闭对资源文件路径的修改
|
|
422
|
+
},
|
|
423
|
+
},
|
|
424
|
+
};
|
|
425
|
+
```
|
|
426
|
+
|
|
383
427
|
## sideEffects
|
|
384
428
|
|
|
385
429
|
配置模块的副作用
|
|
@@ -387,7 +431,8 @@ export default defineConfig({
|
|
|
387
431
|
- 类型: `RegExg[] | (filePath: string, isExternal: boolean) => boolean | boolean`
|
|
388
432
|
- 默认值: `undefined`
|
|
389
433
|
|
|
390
|
-
通常情况下,我们通过 package.json 的 `"sideEffects"`
|
|
434
|
+
通常情况下,我们通过 package.json 的 `"sideEffects"` 字段来配置模块的副作用,但是在某些情况下,三方包的 package.json 是不可靠的。
|
|
435
|
+
例如我们引用了一个三方包的样式文件。
|
|
391
436
|
|
|
392
437
|
```js
|
|
393
438
|
import 'other-package/dist/index.css';
|
|
@@ -401,7 +446,13 @@ import 'other-package/dist/index.css';
|
|
|
401
446
|
}
|
|
402
447
|
```
|
|
403
448
|
|
|
404
|
-
|
|
449
|
+
同时你又设置了[style.inject](#inject)为 `true`,在控制台可以看到类似的警告信息:
|
|
450
|
+
|
|
451
|
+
```bash
|
|
452
|
+
[LIBUILD:ESBUILD_WARN] Ignoring this import because "other-package/dist/index.css" was marked as having no side effects
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
这时候就可以使用这个配置项,手动配置模块的`"sideEffects"`,配置支持正则和函数形式。
|
|
405
456
|
|
|
406
457
|
```js modern.config.ts
|
|
407
458
|
import { defineConfig } from '@modern-js/module-tools';
|
|
@@ -610,7 +661,7 @@ function styleInject(css, ref) {
|
|
|
610
661
|
var style_inject_es_default = styleInject;
|
|
611
662
|
|
|
612
663
|
// src/index.scss
|
|
613
|
-
var css_248z =
|
|
664
|
+
var css_248z = '.body {\n color: black;\n}';
|
|
614
665
|
style_inject_es_default(css_248z);
|
|
615
666
|
```
|
|
616
667
|
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_position: 3
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Dev Config
|
|
6
|
+
|
|
7
|
+
本章节描述了 Module Tools 关于调试工具相关的所有配置。
|
|
8
|
+
|
|
9
|
+
``` ts
|
|
10
|
+
export default {
|
|
11
|
+
dev: {
|
|
12
|
+
storybook: {/* Storybook Config */},
|
|
13
|
+
},
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## storybook
|
|
18
|
+
|
|
19
|
+
**首先需要确保**:
|
|
20
|
+
|
|
21
|
+
- 开启 Storybook 调试功能或者安装 `@modern-js/plugin-storybook` 插件。
|
|
22
|
+
- 注册 `@modern-js/plugin-storybook` 插件。
|
|
23
|
+
|
|
24
|
+
> 关于如何开启 Storybook 调试功能,可以参考:[【Storybook 调试】](guide/basic/use-micro-generator#storybook-调试)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### storybook.webpack
|
|
29
|
+
|
|
30
|
+
- 类型:`Object` | `Function` | `undefined`
|
|
31
|
+
|
|
32
|
+
- 默认值:`undefined`
|
|
33
|
+
|
|
34
|
+
``` ts
|
|
35
|
+
export default {
|
|
36
|
+
dev: {
|
|
37
|
+
storybook: {
|
|
38
|
+
webpack(config) {
|
|
39
|
+
return config;
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
你可以通过 `dev.storybook.webpack` 来修改 Storybook Preview-iframe 的 webpack 配置。使用方式可以参考 Modern.js Builder 的 [`tools.webpack`](https://modernjs.dev/builder/api/config-tools.html#tools.webpack) 配置。
|
|
47
|
+
|
|
48
|
+

|
|
49
|
+
|
|
50
|
+
:::tip
|
|
51
|
+
对于 Storybook Manager app 部分的 webpack 配置,可以通过增加 `./config/storybook/main.js` 文件进行配置。
|
|
52
|
+
|
|
53
|
+
``` js
|
|
54
|
+
// ./config/storybook/main.js
|
|
55
|
+
|
|
56
|
+
module.exports = {
|
|
57
|
+
// it controls the Storybook manager app
|
|
58
|
+
managerWebpack: async (config, options) => {
|
|
59
|
+
// update config here
|
|
60
|
+
return config;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
```
|
|
64
|
+
:::
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
### storybook.webpackChain
|
|
68
|
+
|
|
69
|
+
- 类型:`Function` | `undefined`
|
|
70
|
+
|
|
71
|
+
- 默认值:`undefined`
|
|
72
|
+
|
|
73
|
+
``` ts
|
|
74
|
+
export default {
|
|
75
|
+
dev: {
|
|
76
|
+
storybook: {
|
|
77
|
+
webpackChain(chain) {
|
|
78
|
+
return chain;
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
你可以通过 `dev.storybook.webpackChain` 来修改 Storybook Preview-iframe 的 webpack 配置。使用方式可以参考 Modern.js Builder 的 [`tools.webpackChain`](https://modernjs.dev/builder/api/config-tools.html#tools.webpackchain) 配置。
|
|
@@ -19,10 +19,11 @@ sidebar_position: 4
|
|
|
19
19
|
|
|
20
20
|
## 第三方依赖的默认处理
|
|
21
21
|
|
|
22
|
-
在模块工程里,**默认情况下不会对 `"dependencies"` 以及 `"peerDependencies"`
|
|
22
|
+
在模块工程里,**默认情况下不会对 `"dependencies"` 以及 `"peerDependencies"` 下的第三方依赖进行打包处理**。
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
这是因为在安装 npm 包时,其 `"dependencies"` 也会被安装。不打包 `"dependencies"`,可以减小包产物的体积。
|
|
25
|
+
|
|
26
|
+
如果需要打包某些依赖,建议将它们从 `"dependencies"` 挪到 `"devDependencies"` ,这相当于对依赖进行 **prebundle** ,可以减小依赖安装的体积。
|
|
26
27
|
|
|
27
28
|
<CH.Spotlight>
|
|
28
29
|
|
|
@@ -74,93 +75,13 @@ console.info(React);
|
|
|
74
75
|
|
|
75
76
|
</CH.Spotlight>
|
|
76
77
|
|
|
77
|
-
|
|
78
78
|
如果想要修改默认的处理方式,可以通过下面的 API 进行修改:
|
|
79
79
|
|
|
80
80
|
- [`buildConfig.autoExternal`](/api/config/build-config#autoexternal)
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
当我们希望关闭对于第三方依赖的默认处理行为时候,可以通过以下方式来实现:
|
|
85
|
-
|
|
86
|
-
```ts
|
|
87
|
-
export default defineConfig({
|
|
88
|
-
buildConfig: {
|
|
89
|
-
autoExternal: false,
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
这样对于 `"dependencies"` 和 `"peerDependencies"` 下面的依赖都会进行打包处理。如果只想要关闭其中某个下面的依赖处理,则可以使用
|
|
95
|
-
`buildConfig.autoExternal` 的对象形式:
|
|
96
|
-
|
|
97
|
-
```ts
|
|
98
|
-
export default defineConfig({
|
|
99
|
-
buildConfig: {
|
|
100
|
-
autoExternal: {
|
|
101
|
-
dependencies: false,
|
|
102
|
-
peerDependencies: false,
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
## 打包第三方依赖
|
|
109
|
-
|
|
110
|
-
不过也有一些情况希望将这些第三方依赖打包到产物。将第三方依赖打包到产物的好处是:**减少下载第三方依赖所花费的时间**。这种处理第三方依赖的方式一般在开发命令行工具中比较常见,这可以提升命令行工具的加载速度,给使用者带来更好的使用体验。
|
|
111
|
-
|
|
112
|
-
那么如何在模块工程中开启对于第三方依赖的打包处理呢?
|
|
113
|
-
|
|
114
|
-
**当希望打包某些依赖的时候,一般来说会将这些依赖声明在 package.json 的 `"devDependencies"` 中**。此时在构建过程中,会**允许**声明在 `"devDependencies"` 中的依赖打包到产物中。
|
|
115
|
-
|
|
116
|
-
<CH.Spotlight>
|
|
117
|
-
|
|
118
|
-
```json ./v1/package.json
|
|
119
|
-
{
|
|
120
|
-
"devDependencies": {
|
|
121
|
-
"react": "^17"
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
---
|
|
127
|
-
|
|
128
|
-
如果项目需要打包 `react` 依赖。
|
|
129
|
-
|
|
130
|
-
```json ./v1/package.json
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
---
|
|
134
|
-
|
|
135
|
-
当源码中使用了 `react` 依赖。
|
|
136
|
-
|
|
137
|
-
```tsx src/index.ts
|
|
138
|
-
import React from 'react';
|
|
139
|
-
console.info(React);
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
---
|
|
143
|
-
|
|
144
|
-
此时产物中会将 `react` 代码打包到产物中。
|
|
145
|
-
|
|
146
|
-
```js dist/index.js
|
|
147
|
-
// ...
|
|
148
|
-
var import_react = __toESM(require_react());
|
|
149
|
-
function src_default() {
|
|
150
|
-
console.info(import_react.default);
|
|
151
|
-
}
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
</CH.Spotlight>
|
|
155
|
-
|
|
156
|
-
不过在实际的开发场景中,可能会遇到某个第三方依赖无法处理的情况,此时可以使用以下 API 对它们进行 **external** 处理。
|
|
157
|
-
|
|
158
|
-
- [`buildConfig.externals`](/api/config/build-config#externals)
|
|
159
|
-
|
|
160
|
-
### 排除指定第三方依赖
|
|
82
|
+
## 排除指定第三方依赖
|
|
161
83
|
|
|
162
|
-
在上面我们提到了 `buildConfig.autoExternal` API 的用途,同时 `buildConfig.externals`
|
|
163
|
-
API 对项目的依赖进行更细微的处理。
|
|
84
|
+
在上面我们提到了 `buildConfig.autoExternal` API 的用途,同时 [`buildConfig.externals`](/api/config/build-config#externals) 可以实现对三方依赖更细微的处理。
|
|
164
85
|
|
|
165
86
|
例如当我们需要仅对某些依赖不进行打包处理的时候,可以按照如下方式进行配置:
|
|
166
87
|
|
|
@@ -30,6 +30,10 @@ sidebar_position: 1
|
|
|
30
30
|
- Bundle 可以减少构建产物的体积,也可以对依赖预打包,减小安装依赖的体积。提前对库进行打包,可以加快应用项目构建的速度。
|
|
31
31
|
- Bundleless 则是可以保持原有的文件结构,更有利于调试和 tree shaking。
|
|
32
32
|
|
|
33
|
+
:::warning
|
|
34
|
+
Bundleless 是单文件编译模式,因此对于类型的引用和导出你需要加上 `type` 字段, 例如 `import type { A } from './types`
|
|
35
|
+
:::
|
|
36
|
+
|
|
33
37
|
在 `buildConfig` 中可以通过 [`buildConfig.buildType`](/api/config/build-config#buildtype) 来指定当前构建任务是 Bundle 还是 Bundleless。
|
|
34
38
|
|
|
35
39
|
### `input` 与 `sourceDir` 的关系
|
|
@@ -166,8 +166,7 @@ npm install -g pnpm
|
|
|
166
166
|
|
|
167
167
|
默认情况下,生成的配置文件的内容如下:
|
|
168
168
|
|
|
169
|
-
```
|
|
170
|
-
// modern.config.ts
|
|
169
|
+
```ts title="modern.config.ts"
|
|
171
170
|
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
172
171
|
|
|
173
172
|
export default defineConfig({
|
|
@@ -178,8 +177,7 @@ export default defineConfig({
|
|
|
178
177
|
|
|
179
178
|
**我们推荐使用 `defineConfig` 函数**,不过并不强制使用它。因此你也可以在配置文件中直接返回一个对象:
|
|
180
179
|
|
|
181
|
-
```ts
|
|
182
|
-
// modern.config.ts
|
|
180
|
+
```ts title="modern.config.ts"
|
|
183
181
|
import moduleTools from '@modern-js/module-tools';
|
|
184
182
|
|
|
185
183
|
export default {
|
|
@@ -8,7 +8,7 @@ sidebar_position: 3
|
|
|
8
8
|
|
|
9
9
|
当在初始化的项目里使用 `modern build` 命令的时候,会根据 Module Tools 默认支持的配置生成相应的产物。默认支持的配置具体如下:
|
|
10
10
|
|
|
11
|
-
```
|
|
11
|
+
```ts title="modern.config.ts"
|
|
12
12
|
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
13
13
|
|
|
14
14
|
export default defineConfig({
|
|
@@ -57,7 +57,7 @@ export default defineConfig({
|
|
|
57
57
|
|
|
58
58
|
例如,如果使用预设函数的形式达到预设字符串 `"npm-library-es5"` 同样的效果,可以按照如下的方式:
|
|
59
59
|
|
|
60
|
-
```
|
|
60
|
+
```ts title="modern.config.ts"
|
|
61
61
|
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
62
62
|
|
|
63
63
|
export default defineConfig({
|
|
@@ -129,7 +129,7 @@ export default defineConfig({
|
|
|
129
129
|
|
|
130
130
|
`buildConfig` 是用于修改产物的方式之一,**当与 `buildPreset` 配置同时存在的时候,只有 `buildConfig` 才会生效**。因此如果按照如下方式配置:
|
|
131
131
|
|
|
132
|
-
```
|
|
132
|
+
```ts title="modern.config.ts"
|
|
133
133
|
import { defineConfig } from '@modern-js/module-tools';
|
|
134
134
|
|
|
135
135
|
export default defineConfig({
|
|
@@ -61,7 +61,7 @@ modern test --updateSnapshot
|
|
|
61
61
|
|
|
62
62
|
<CH.Spotlight>
|
|
63
63
|
|
|
64
|
-
```
|
|
64
|
+
```ts ./src/index.ts
|
|
65
65
|
export default function () {
|
|
66
66
|
return 'hello world';
|
|
67
67
|
}
|
|
@@ -71,7 +71,7 @@ export default function () {
|
|
|
71
71
|
|
|
72
72
|
首先是模块的代码。
|
|
73
73
|
|
|
74
|
-
```
|
|
74
|
+
```ts ./src/index.ts
|
|
75
75
|
export default function () {
|
|
76
76
|
return 'hello world';
|
|
77
77
|
}
|
|
@@ -83,7 +83,7 @@ export default function () {
|
|
|
83
83
|
|
|
84
84
|
其中 `@` 指向了源码目录,在初始化项目的 `tests/tsconfig.json` 的 `paths` 中定义了。
|
|
85
85
|
|
|
86
|
-
```
|
|
86
|
+
```ts ./tests/index.test.ts
|
|
87
87
|
import main from '@/index';
|
|
88
88
|
|
|
89
89
|
describe('默认值 cases', () => {
|
|
@@ -4,3 +4,4 @@
|
|
|
4
4
|
|
|
5
5
|
* [@modern-js/plugin-module-import](./plugin-import.md):使用 SWC 提供与 `babel-plugin-import` 一样的能力。
|
|
6
6
|
* [@modern-js/plugin-module-banner](./plugin-banner.md):为每个 JS 和 CSS 文件的顶部和底部添加自定义内容,例如版权信息。
|
|
7
|
+
* [@modern-js/plugin-module-node-polyfill](./plugin-node-polyfill.mdx):会自动注入 Node 核心模块在浏览器端的 polyfills。
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Node Polyfill 插件
|
|
2
|
+
|
|
3
|
+
:::tip Node Polyfill 介绍
|
|
4
|
+
通常情况下,我们不会在浏览器端使用 Node 模块。但在当前代码需要同时在 Node 端和浏览器端运行时,用到一些 Node 模块是有可能的。Node Polyfill 为这些 Node 模块提供了浏览器版本的 polyfills。
|
|
5
|
+
:::
|
|
6
|
+
|
|
7
|
+
通过使用 Node Polyfill 插件,会自动注入 Node 核心模块在浏览器端的 polyfills,让你可以在浏览器端放心使用这些模块。
|
|
8
|
+
|
|
9
|
+
## 快速开始
|
|
10
|
+
|
|
11
|
+
### 安装
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# npm
|
|
15
|
+
npm install @modern-js/plugin-module-node-polyfill -D
|
|
16
|
+
|
|
17
|
+
# yarn
|
|
18
|
+
yarn add @modern-js/plugin-module-node-polyfill -D
|
|
19
|
+
|
|
20
|
+
# pnpm
|
|
21
|
+
pnpm add @modern-js/plugin-module-node-polyfill -D
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### 注册插件
|
|
25
|
+
|
|
26
|
+
在 Module Tools 中,你可以按照如下方式注册插件:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
30
|
+
import { modulePluginNodePolyfill } from '@modern-js/plugin-module-node-polyfill';
|
|
31
|
+
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
plugins: [
|
|
34
|
+
moduleTools(),
|
|
35
|
+
modulePluginNodePolyfill(),
|
|
36
|
+
],
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## 配置
|
|
41
|
+
|
|
42
|
+
* 类型:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
type NodePolyfillOptions = {
|
|
46
|
+
exclude?: string[];
|
|
47
|
+
overrides?: Record<NodePolyfillKey, string>;
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### exclude
|
|
52
|
+
|
|
53
|
+
排除要注入的 Node Polyfill。
|
|
54
|
+
|
|
55
|
+
``` ts focus=7:9
|
|
56
|
+
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
57
|
+
import { modulePluginNodePolyfill } from '@modern-js/plugin-module-node-polyfill';
|
|
58
|
+
|
|
59
|
+
export default defineConfig({
|
|
60
|
+
plugins: [
|
|
61
|
+
moduleTools(),
|
|
62
|
+
modulePluginNodePolyfill({
|
|
63
|
+
exclude: ['console'],
|
|
64
|
+
}),
|
|
65
|
+
],
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### overrides
|
|
70
|
+
|
|
71
|
+
覆盖内置的 Node Polyfill。
|
|
72
|
+
|
|
73
|
+
``` ts focus=7:9
|
|
74
|
+
import moduleTools, { defineConfig } from '@modern-js/module-tools';
|
|
75
|
+
import { modulePluginNodePolyfill } from '@modern-js/plugin-module-node-polyfill';
|
|
76
|
+
|
|
77
|
+
export default defineConfig({
|
|
78
|
+
plugins: [
|
|
79
|
+
moduleTools(),
|
|
80
|
+
modulePluginNodePolyfill({
|
|
81
|
+
overrides: {
|
|
82
|
+
fs: path.join(__dirname, './custom-fs.js'),
|
|
83
|
+
}
|
|
84
|
+
}),
|
|
85
|
+
],
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
## Node Polyfills
|
|
91
|
+
|
|
92
|
+
### Globals
|
|
93
|
+
|
|
94
|
+
- `Buffer`
|
|
95
|
+
- `process`
|
|
96
|
+
- `console`
|
|
97
|
+
|
|
98
|
+
当你在代码中使用以上全局变量时,对应 polyfill 会被自动注入。
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const bufferData = Buffer.from('xxxx');
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Modules
|
|
105
|
+
|
|
106
|
+
- `assert`
|
|
107
|
+
- `buffer`
|
|
108
|
+
- `console`
|
|
109
|
+
- `constants`
|
|
110
|
+
- `crypto`
|
|
111
|
+
- `domain`
|
|
112
|
+
- `events`
|
|
113
|
+
- `http`
|
|
114
|
+
- `https`
|
|
115
|
+
- `os`
|
|
116
|
+
- `path`
|
|
117
|
+
- `punycode`
|
|
118
|
+
- `process`
|
|
119
|
+
- `querystring`
|
|
120
|
+
- `stream`
|
|
121
|
+
- `_stream_duplex`
|
|
122
|
+
- `_stream_passthrough`
|
|
123
|
+
- `_stream_readable`
|
|
124
|
+
- `_stream_transform`
|
|
125
|
+
- `_stream_writable`
|
|
126
|
+
- `string_decoder`
|
|
127
|
+
- `sys`
|
|
128
|
+
- `timers`
|
|
129
|
+
- `tty`
|
|
130
|
+
- `url`
|
|
131
|
+
- `util`
|
|
132
|
+
- `vm`
|
|
133
|
+
- `zlib`
|
|
134
|
+
|
|
135
|
+
当你通过 `require` 或 `import` 等语法在代码中引用以上模块时,对应 polyfill 会被注入。
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { Buffer } from 'buffer';
|
|
139
|
+
|
|
140
|
+
const bufferData = Buffer.from('xxxx');
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Fallbacks
|
|
144
|
+
|
|
145
|
+
- `child_process`
|
|
146
|
+
- `cluster`
|
|
147
|
+
- `dgram`
|
|
148
|
+
- `dns`
|
|
149
|
+
- `fs`
|
|
150
|
+
- `module`
|
|
151
|
+
- `net`
|
|
152
|
+
- `readline`
|
|
153
|
+
- `repl`
|
|
154
|
+
- `tls`
|
|
155
|
+
|
|
156
|
+
目前浏览器端没有以上模块的 polyfill,因此当你引用以上模块时,会自动 fallback 为一个空对象。
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import fs from 'fs';
|
|
160
|
+
|
|
161
|
+
console.log(fs); // -> {}
|
|
162
|
+
```
|
package/package.json
CHANGED
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
"bugs": "https://github.com/web-infra-dev/modern.js/issues",
|
|
6
6
|
"repository": "web-infra-dev/modern.js",
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"version": "2.
|
|
8
|
+
"version": "2.13.0",
|
|
9
9
|
"main": "index.js",
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@code-hike/mdx": "^0.7.4",
|
|
12
12
|
"react": "^18.2.0",
|
|
13
13
|
"react-dom": "^18.2.0",
|
|
14
14
|
"shiki": "^0.11.1",
|
|
15
|
-
"@modern-js/doc-plugin-auto-sidebar": "2.
|
|
16
|
-
"@modern-js/doc-tools": "2.
|
|
15
|
+
"@modern-js/doc-plugin-auto-sidebar": "2.13.0",
|
|
16
|
+
"@modern-js/doc-tools": "2.13.0"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"dev": "modern dev",
|