@modern-js/module-tools-docs 2.11.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,15 @@
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
+
3
13
  ## 2.11.0
4
14
 
5
15
  ### Minor Changes
@@ -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
+ ```
@@ -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.11.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.11.0",
16
- "@modern-js/doc-tools": "2.11.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",