@modern-js/module-tools-docs 2.10.0 → 2.12.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 CHANGED
@@ -1,5 +1,31 @@
1
1
  # @modern-js/module-tools-docs
2
2
 
3
+ ## 2.12.0
4
+
5
+ ### Patch Changes
6
+
7
+ - a90a6a2: feat(plugin-module-node-polyfill, module-docs): add Node Polyfill feature
8
+ feat(plugin-module-node-polyfill, module-docs): 添加 Node Polyfill 功能。
9
+ - Updated dependencies [d50aaf7]
10
+ - @modern-js/doc-tools@2.12.0
11
+ - @modern-js/doc-plugin-auto-sidebar@2.12.0
12
+
13
+ ## 2.11.0
14
+
15
+ ### Minor Changes
16
+
17
+ - f1b2629: feat: add `dts.abortOnError` config
18
+ feat: 添加 `dts.abortOnError` 配置
19
+
20
+ ### Patch Changes
21
+
22
+ - 6118636: fix(module-tools, module-tools-docs): fix svgr usage
23
+ fix(module-tools, module-tools-docs): 修复 svgr 的使用
24
+ - Updated dependencies [210e9d6]
25
+ - Updated dependencies [95dd73e]
26
+ - @modern-js/doc-tools@2.11.0
27
+ - @modern-js/doc-plugin-auto-sidebar@2.11.0
28
+
3
29
  ## 2.10.0
4
30
 
5
31
  ### Patch Changes
@@ -95,6 +95,34 @@ Packaged to handle svg as a React component, options reference [svgr](https://re
95
95
  - type: `boolean | Object`
96
96
  - default: `false`
97
97
 
98
+ When svgr feature is enabled, you can use svg as a component using the default export.
99
+
100
+ ```js index.ts
101
+ // true
102
+ import Logo from './logo.svg';
103
+
104
+ export default () => <Logo />;
105
+ ```
106
+
107
+ :::warning
108
+ The following usage is not currently supported:
109
+
110
+ ```js index.ts
111
+ import { ReactComponent } from './logo.svg';
112
+ ```
113
+ :::
114
+
115
+ 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
+ ``` ts modern-app-env.d.ts focus=1:3
118
+ declare module '*.svg' {
119
+ const src: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
120
+ export default src;
121
+ }
122
+
123
+ /// <reference types='@modern-js/module-tools/types' />
124
+ ```
125
+
98
126
  #### include
99
127
 
100
128
  Set the matching svg file
@@ -183,17 +211,30 @@ To prevent excessive global replacement substitution, it is recommended that the
183
211
 
184
212
  ## dts
185
213
 
186
- The dts file generates the relevant configuration, by default it generates
214
+ The dts file generates the relevant configuration, by default it generates.
187
215
 
188
216
  - type: `false | Object`
189
- - default: `{}`
217
+ - default:
190
218
 
191
- ### tsconfigPath
219
+ ``` js
220
+ {
221
+ abortOnError: true,
222
+ distPath: './',
223
+ only: false,
224
+ tsconfigPath: './tsconfig.json',
225
+ }
226
+ ```
192
227
 
193
- Path to the tsconfig file
228
+ ### abortOnError
194
229
 
195
- - type: `string`
196
- - default: `. /tsconfig.json`
230
+ Whether to allow the build to succeed in case of a type error. By default, this will cause the build to fail in case of a type error.
231
+
232
+ :::warning
233
+ When this configuration is turned on, there is no guarantee that the type files will be generated properly and accurately. In `buildType: 'bundle'` or Bundle build mode, the type file must not be generated.
234
+ :::
235
+
236
+ - type: `boolean`
237
+ - default: `true`
197
238
 
198
239
  ### distPath
199
240
 
@@ -209,6 +250,13 @@ Generate only dts files, not js files
209
250
  - type: `boolean`
210
251
  - default: `false`
211
252
 
253
+ ### tsconfigPath
254
+
255
+ Path to the tsconfig file
256
+
257
+ - type: `string`
258
+ - default: `. /tsconfig.json`
259
+
212
260
  ## externals
213
261
 
214
262
  Configure external dependencies that will not be packaged into the final bundle
@@ -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
- - For files over **10kb** they are inlined into the code.
20
+ - Files less than **10kb** will be inlined into the code.
21
21
 
22
22
  Let us look at the following example:
23
23
 
@@ -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
+ ```
@@ -98,34 +98,51 @@ export default defineConfig({
98
98
 
99
99
  ### svgr
100
100
 
101
- 打包时将 svg 作为一个 React 组件处理,options 参考 [svgr](https://react-svgr.com/docs/options/),另外还支持了 `include` 和 `exclude` 两个配置项,用于匹配需要处理的 svg 文件。
101
+ 打包时将 SVG 作为一个 React 组件处理,options 参考 [svgr](https://react-svgr.com/docs/options/),另外还支持了 `include` 和 `exclude` 两个配置项,用于匹配需要处理的 SVG 文件。
102
102
 
103
103
  - 类型: `boolean | Object`
104
104
  - 默认值: `false`
105
105
 
106
- :::tip
107
- 开启 svgr 后,仍然使用 default export 导出 svg,所以你只能导入默认值。
106
+ 开启 svgr 功能后,可以使用默认导出的方式将 SVG 当做组件使用。
108
107
 
109
108
  ```js index.ts
110
109
  // true
111
- import logo from './logo.svg';
110
+ import Logo from './logo.svg';
111
+
112
+ export default () => <Logo />;
113
+ ```
114
+
115
+ :::warning
112
116
 
113
- // false
117
+ 目前不支持下面的用法:
118
+
119
+ ```js index.ts
114
120
  import { ReactComponent } from './logo.svg';
115
121
  ```
116
122
 
117
123
  :::
118
124
 
125
+ 当开启功能后,可以通过在 `modern-app-env.d.ts` 文件中增加类型定义,修改使用 SVG 的类型:
126
+
127
+ ``` ts modern-app-env.d.ts focus=1:3
128
+ declare module '*.svg' {
129
+ const src: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
130
+ export default src;
131
+ }
132
+
133
+ /// <reference types='@modern-js/module-tools/types' />
134
+ ```
135
+
119
136
  #### include
120
137
 
121
- 设定匹配的 svg 文件
138
+ 设定匹配的 SVG 文件
122
139
 
123
140
  - 类型: `string | RegExp | (string | RegExp)[]`
124
141
  - 默认值: `/\.svg$/`
125
142
 
126
143
  #### exclude
127
144
 
128
- 设定不匹配的 svg 文件
145
+ 设定不匹配的 SVG 文件
129
146
 
130
147
  - 类型: `string | RegExp | (string | RegExp)[]`
131
148
  - 默认值: `undefined`
@@ -236,17 +253,30 @@ export default defineConfig({
236
253
 
237
254
  ## dts
238
255
 
239
- 类型文件生成的相关配置,默认会生成。
256
+ 类型文件生成的相关配置,默认情况会生成。
240
257
 
241
258
  - 类型: `false | Object`
242
- - 默认值: `{}`
259
+ - 默认值:
243
260
 
244
- ### tsconfigPath
261
+ ``` js
262
+ {
263
+ abortOnError: true,
264
+ distPath: './',
265
+ only: false,
266
+ tsconfigPath: './tsconfig.json',
267
+ }
268
+ ```
245
269
 
246
- TypeScript 配置文件的路径。
270
+ ### abortOnError
247
271
 
248
- - 类型: `string`
249
- - 默认值: `./tsconfig.json`
272
+ 在出现类型错误的时候,是否允许构建成功。**默认情况下,在出现类型错误的时候会导致构建失败**。
273
+
274
+ :::warning
275
+ 当关闭该配置后,无法保证类型文件能正常生成,且不保证内容正确。在 `buildType: 'bundle'` 或者 Bundle 构建模式下,类型文件一定不会生成。
276
+ :::
277
+
278
+ - 类型:`boolean`
279
+ - 默认值:`true`
250
280
 
251
281
  ### distPath
252
282
 
@@ -262,6 +292,14 @@ TypeScript 配置文件的路径。
262
292
  - 类型: `boolean`
263
293
  - 默认值: `false`
264
294
 
295
+ ### tsconfigPath
296
+
297
+ TypeScript 配置文件的路径。
298
+
299
+ - 类型: `string`
300
+ - 默认值: `./tsconfig.json`
301
+
302
+
265
303
  ## externals
266
304
 
267
305
  配置外部依赖,不会被打包到最终的 bundle 中。
@@ -17,7 +17,7 @@ sidebar_position: 7
17
17
  对于静态文件的处理,模块工程目前默认支持的功能有:
18
18
 
19
19
  - 设置静态资源路径为 `./assets`。
20
- - 对于超过 **10kb** 的文件会内联到代码中。
20
+ - 对于不超过 **10kb** 的文件会内联到代码中。
21
21
 
22
22
  让我们看下面的例子:
23
23
 
@@ -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.10.0",
8
+ "version": "2.12.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.10.0",
16
- "@modern-js/doc-tools": "2.10.0"
15
+ "@modern-js/doc-plugin-auto-sidebar": "2.12.0",
16
+ "@modern-js/doc-tools": "2.12.0"
17
17
  },
18
18
  "scripts": {
19
19
  "dev": "modern dev",