@modern-js/module-tools-docs 2.21.1 → 2.22.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,22 @@
1
1
  # @modern-js/module-tools-docs
2
2
 
3
+ ## 2.22.0
4
+
5
+ ### Minor Changes
6
+
7
+ - c890980: feat: add swc transform content and fix build logic when disable swc-transform
8
+ feat: 添加 swc transform 相关文档内容,并且修复禁用 swc-transform 时候的构建逻辑
9
+
10
+ ### Patch Changes
11
+
12
+ - 3c3d1e2: docs: update project creation and new command text.
13
+
14
+ docs: 更新生成器创建项目和 new 命令文案。
15
+
16
+ - Updated dependencies [b8553a3]
17
+ - @modern-js/doc-tools@2.22.0
18
+ - @modern-js/doc-plugin-auto-sidebar@2.22.0
19
+
3
20
  ## 2.21.1
4
21
 
5
22
  ### Patch Changes
@@ -322,7 +322,7 @@ We have done many extensions based on the original esbuild build. Therefore, whe
322
322
 
323
323
  :::
324
324
 
325
- <!-- ## externalHelpers
325
+ ## externalHelpers
326
326
 
327
327
  By default, the output JS code may depend on helper functions to support the target environment or output format, and these helper functions will be inlined in the file that requires it.
328
328
 
@@ -364,7 +364,7 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
364
364
  export var yourCode = function() {
365
365
  // use _async_to_generator
366
366
  }
367
- ``` -->
367
+ ```
368
368
 
369
369
  ## externals
370
370
 
@@ -521,12 +521,12 @@ Whether to generate sourceMap or not
521
521
  - Type: `boolean | 'inline' | 'external'`
522
522
  - Default: `false`
523
523
 
524
- <!-- ## sourceType
524
+ ## sourceType
525
525
 
526
526
  Sets the format of the source code. By default, the source code will be treated as EsModule. When the source code is using CommonJS, you need to set `commonjs`.
527
527
 
528
528
  - **Type**: `commonjs` | `module`
529
- - **Default**: `module` -->
529
+ - **Default**: `module`
530
530
 
531
531
  ## splitting
532
532
 
@@ -21,7 +21,7 @@ export default defineConfig({
21
21
  });
22
22
  ```
23
23
 
24
- ### ``npm-library'`
24
+ ### `'npm-library'`
25
25
 
26
26
  Library generic schema used under class [NPM](https://www.npmjs.com/) package manager, contains `esm` and `cjs` Bundle products, and includes a type file.
27
27
 
@@ -68,7 +68,7 @@ export const buildConfig = [
68
68
  ];
69
69
  ```
70
70
 
71
- ### `npm-library-with-umd'`
71
+ ### `'npm-library-with-umd'`
72
72
 
73
73
  Used under class [NPM](https://www.npmjs.com/) package manager, and Library supports a similar pattern to [unpkg](https://unpkg.com/). Additional `umd` products are provided on top of the pre-defined `npm-library`.
74
74
 
@@ -1,5 +1,6 @@
1
1
  ---
2
2
  overview: true
3
3
  sidebar_label: Overview
4
+ sidebar_position: 1
4
5
  ---
5
6
  # Overview
@@ -9,10 +9,10 @@ This chapter will describe how to develop component projects using the module en
9
9
  ```bash Interactive questions
10
10
  npx @modern-js/create@latest components-project
11
11
 
12
- ? Please select the solution you want to create: Npm Module
13
- ? Package Name: components-demo
14
- ? Development Language: TS
15
- ? Package Manager: pnpm
12
+ ? Please select the type of project you want to create: Npm Module
13
+ ? Please fill in the project name: components-demo
14
+ ? Please select the programming language: TS
15
+ ? Please select the package manager: pnpm
16
16
  ```
17
17
 
18
18
  ---
@@ -22,10 +22,10 @@ It is recommended to use the `@modern-js/create` command to initialize an npm pr
22
22
  ```bash Interactive questions
23
23
  npx @modern-js/create@latest components-project
24
24
 
25
- ? Please select the solution you want to create: Npm Module
26
- ? Package Name: components-demo
27
- ? Development Language: TS
28
- ? Package Manager: pnpm
25
+ ? Please select the type of project you want to create: Npm Module
26
+ ? Please fill in the project name: components-demo
27
+ ? Please select the programming language: TS
28
+ ? Please select the package manager: pnpm
29
29
  ```
30
30
 
31
31
  ---
@@ -67,8 +67,8 @@ export default () => {
67
67
  ```bash Terminal
68
68
  pnpm run new
69
69
 
70
- ? Action Enable features
71
- ? Enable features Enable Visual Testing (Storybook)
70
+ ? Please select the operation you want: Enable features
71
+ ? Please select the feature name: Enable Visual Testing (Storybook)
72
72
  ```
73
73
 
74
74
  ---
@@ -78,8 +78,8 @@ Execute the `new` command in the project root directory to enable the Storybook
78
78
  ```bash Terminal
79
79
  pnpm run new
80
80
 
81
- ? Action Enable features
82
- ? Enable features Enable Visual Testing (Storybook)
81
+ ? Please select the operation you want: Enable features
82
+ ? Please select the feature name: Enable Visual Testing (Storybook)
83
83
  ```
84
84
 
85
85
  ---
@@ -356,8 +356,8 @@ By default, this feature is not enabled in the module project, you need to enabl
356
356
  ```bash Terminal
357
357
  pnpm run new
358
358
 
359
- ? Action Enable features
360
- ? Enable features Enable Visual Testing (Storybook)
359
+ ? Please select the operation you want: Enable features
360
+ ? Please select the feature name: Enable Visual Testing (Storybook)
361
361
  ```
362
362
 
363
363
  ---
@@ -367,8 +367,8 @@ The Tailwind CSS feature can be enabled by executing the `new` command in the pr
367
367
  ```bash Terminal
368
368
  pnpm run new
369
369
 
370
- ? Action Enable features
371
- ? Enable features Enable Tailwind CSS
370
+ ? Please select the operation you want: Enable features
371
+ ? Please select the feature name: Enable Tailwind CSS
372
372
  ```
373
373
 
374
374
  ---
@@ -26,7 +26,7 @@ Next, in the issue interaction, follow the options below.
26
26
  ```bash
27
27
  ? Please select the type of project you want to create: Npm Module
28
28
  ? Please fill in the project name: library
29
- ? Please select the development language: TS
29
+ ? Please select the programming language: TS
30
30
  ? Please select the package manager: pnpm
31
31
  ```
32
32
 
@@ -5,3 +5,5 @@
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
7
  * [@modern-js/plugin-module-node-polyfill](./plugin-node-polyfill.mdx): Inject Polyfills of Node core modules in the browser side.
8
+ * [@modern-js/plugin-module-polyfill](./plugin-polyfill.md):Inject polyfill for unsupported features used in your code.
9
+ * [@modern-js/plugin-module-babel](./plugin-babel.md):Use babel to transform your code.
@@ -0,0 +1,56 @@
1
+ # Babel Plugin
2
+
3
+ :::tip
4
+ Normally, we don't need to use Babel to transform our code, this plugin is only used as a downgrade.
5
+ :::
6
+
7
+ ## Quick start
8
+
9
+ ### Install
10
+
11
+ ```bash
12
+ # npm
13
+ npm install @modern-js/plugin-module-babel -D
14
+
15
+ # yarn
16
+ yarn add @modern-js/plugin-module-babel -D
17
+
18
+ # pnpm
19
+ pnpm add @modern-js/plugin-module-babel -D
20
+ ```
21
+
22
+ ### Register
23
+
24
+ You can install the plugin with the following command:
25
+
26
+ ```ts
27
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
28
+ import { modulePluginBabel } from '@modern-js/plugin-module-babel';
29
+
30
+ export default defineConfig({
31
+ plugins: [
32
+ moduleTools(),
33
+ modulePluginBabel(),
34
+ ],
35
+ });
36
+ ```
37
+
38
+ ## Config
39
+
40
+ See [babel options](https://babeljs.io/docs/options).
41
+
42
+ Here is an example with `@babel/preset-env` configured
43
+
44
+ ```ts
45
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
46
+ import { modulePluginBabel } from '@modern-js/plugin-module-babel';
47
+
48
+ export default defineConfig({
49
+ plugins: [
50
+ moduleTools(),
51
+ modulePluginBabel({
52
+ presets: [['@babel/preset-env']],
53
+ }),
54
+ ],
55
+ });
56
+ ```
@@ -17,19 +17,18 @@ yarn add @modern-js/plugin-module-banner -D
17
17
  pnpm add @modern-js/plugin-module-banner -D
18
18
  ```
19
19
 
20
-
21
20
  ### Register
22
21
 
23
22
  You can install the plugin with the following command:
24
23
 
25
24
  ```ts
26
- import moduleTools, defineConfig from '@modern-js/module-tools';
27
- import pluginBanner from '@modern-js/plugin-module-banner';
25
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
26
+ import { modulePluginBanner } from '@modern-js/plugin-module-banner';
28
27
 
29
28
  export default defineConfig({
30
29
  plugins: [
31
30
  moduleTools(),
32
- pluginBanner({
31
+ modulePluginBanner({
33
32
  banner: {
34
33
  js: '//comment',
35
34
  css: '/*comment*/',
@@ -48,7 +47,9 @@ Note: CSS comments do not support the `//comment` syntax. Refer to [【CSS Comme
48
47
  ### Add copyright information at the top of a JS file
49
48
 
50
49
  ```ts
51
- import pluginBanner from '@modern-js/plugin-module-banner';
50
+ import { modulePluginBanner } from '@modern-js/plugin-module-banner';
51
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
52
+
52
53
 
53
54
  const copyRight = `/*
54
55
  © Copyright 2020 xxx.com or one of its affiliates.
@@ -62,7 +63,8 @@ const copyRight = `/*
62
63
 
63
64
  export default defineConfig({
64
65
  plugins: [
65
- pluginBanner({
66
+ moduleTools(),
67
+ modulePluginBanner({
66
68
  banner: {
67
69
  js: copyRight,
68
70
  },
@@ -73,7 +75,7 @@ export default defineConfig({
73
75
 
74
76
  ## Configuration
75
77
 
76
- * **Type:**
78
+ * **Type**
77
79
 
78
80
  ```ts
79
81
  type BannerOptions = {
@@ -2,11 +2,9 @@
2
2
 
3
3
  Using [SWC](https://swc.rs/) provides the same ability and configuration as [`babel-plugin-import`](https://github.com/umijs/babel-plugin-import).
4
4
 
5
- {/*
6
5
  :::tip
7
- [As of @modern-js/module-tools version 2.16.0, this plugin functionality is built into module-tools and is provided by [`transformImport`](api/config/build-config.html#transformimport).]: #
6
+ Since @modern-js/module-tools version >= 2.16.0, this plugin functionality is built into module-tools and is provided by [`transformImport`](api/config/build-config.html#transformimport).
8
7
  :::
9
- */}
10
8
 
11
9
  ## Quick Start
12
10
 
@@ -28,13 +26,13 @@ pnpm add @modern-js/plugin-module-import -D
28
26
  In Module Tools, you can register plugins in the following way:
29
27
 
30
28
  ```ts
31
- import moduleTools, defineConfig from '@modern-js/module-tools';
32
- import moduleImport from '@modern-js/plugin-module-import';
29
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
30
+ import { modulePluginImport } from '@modern-js/plugin-module-import';
33
31
 
34
32
  export default defineConfig({
35
33
  plugins: [
36
34
  moduleTools(),
37
- moduleImport({
35
+ modulePluginImport({
38
36
  pluginImport: [{
39
37
  libraryName: 'antd',
40
38
  style: true,
@@ -65,11 +63,13 @@ The elements of the array are configuration objects for `babel-plugin-import`, w
65
63
  **Example:**
66
64
 
67
65
  ```ts
68
- import moduleImport from '@modern-js/plugin-module-import';
66
+ import { modulePluginImport } from '@modern-js/plugin-module-import';
67
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
69
68
 
70
69
  export default defineConfig({
71
70
  plugins: [
72
- moduleImport({
71
+ moduleTools(),
72
+ modulePluginImport({
73
73
  pluginImport: [
74
74
  // babel-plugin-import`s options config
75
75
  {
@@ -108,13 +108,14 @@ import { MyButton as Btn } from 'foo';
108
108
 
109
109
  Add the following configuration on the right-hand side:
110
110
 
111
-
112
111
  ```ts
113
- import moduleImport from '@modern-js/plugin-module-import';
112
+ import { modulePluginImport } from '@modern-js/plugin-module-import';
113
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
114
114
 
115
115
  export default defineConfig({
116
116
  plugins: [
117
- moduleImport({
117
+ moduleTools(),
118
+ modulePluginImport({
118
119
  pluginImport: [{
119
120
  libraryName: 'foo',
120
121
  customName: 'foo/es/{{ member }}'
@@ -129,11 +130,11 @@ export default defineConfig({
129
130
  The `{{ member }}` in it will be replaced with the corresponding import member.
130
131
 
131
132
  ```ts focus=8:8
132
- import moduleImport from '@modern-js/plugin-module-import';
133
+ import { modulePluginImport } from '@modern-js/plugin-module-import';
133
134
 
134
135
  export default defineConfig({
135
136
  plugins: [
136
- moduleImport({
137
+ modulePluginImport({
137
138
  pluginImport: [{
138
139
  libraryName: 'foo',
139
140
  customName: 'foo/es/{{ member }}'
@@ -143,7 +144,6 @@ export default defineConfig({
143
144
  });
144
145
  ```
145
146
 
146
-
147
147
  ---
148
148
  After transformation:
149
149
 
@@ -153,18 +153,18 @@ import Btn from 'foo/es/MyButton';
153
153
 
154
154
  </CH.Spotlight>
155
155
 
156
-
157
156
  Template `customName: 'foo/es/{{ member }}'` is the same as `` customName: (member) => `foo/es/${member}` ``, but template value has no performance overhead of Node-API.
158
157
 
159
158
  The template used here is [handlebars](https://handlebarsjs.com). There are some useful builtin tools, Take the above import statement as an example:
160
159
 
161
-
162
160
  ```ts focus=8:8
163
- import moduleImport from '@modern-js/plugin-module-import';
161
+ import { modulePluginImport } from '@modern-js/plugin-module-import';
162
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
164
163
 
165
164
  export default defineConfig({
166
165
  plugins: [
167
- moduleImport({
166
+ moduleTools(),
167
+ modulePluginImport({
168
168
  pluginImport: [{
169
169
  libraryName: 'foo',
170
170
  customName: 'foo/es/{{ kebabCase member }}',
@@ -86,14 +86,13 @@ export default defineConfig({
86
86
  });
87
87
  ```
88
88
 
89
-
90
89
  ## Node Polyfills
91
90
 
92
91
  ### Globals
93
92
 
94
- - `Buffer`
95
- - `process`
96
- - `console`
93
+ * `Buffer`
94
+ * `process`
95
+ * `console`
97
96
 
98
97
  When the above global variables are used directly in code, the corresponding polyfill will be injected.
99
98
 
@@ -103,34 +102,34 @@ const bufferData = Buffer.from('xxxx');
103
102
 
104
103
  ### Modules
105
104
 
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`
105
+ * `assert`
106
+ * `buffer`
107
+ * `console`
108
+ * `constants`
109
+ * `crypto`
110
+ * `domain`
111
+ * `events`
112
+ * `http`
113
+ * `https`
114
+ * `os`
115
+ * `path`
116
+ * `punycode`
117
+ * `process`
118
+ * `querystring`
119
+ * `stream`
120
+ * `_stream_duplex`
121
+ * `_stream_passthrough`
122
+ * `_stream_readable`
123
+ * `_stream_transform`
124
+ * `_stream_writable`
125
+ * `string_decoder`
126
+ * `sys`
127
+ * `timers`
128
+ * `tty`
129
+ * `url`
130
+ * `util`
131
+ * `vm`
132
+ * `zlib`
134
133
 
135
134
  When the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.
136
135
 
@@ -142,16 +141,16 @@ const bufferData = Buffer.from('xxxx');
142
141
 
143
142
  ### Fallbacks
144
143
 
145
- - `child_process`
146
- - `cluster`
147
- - `dgram`
148
- - `dns`
149
- - `fs`
150
- - `module`
151
- - `net`
152
- - `readline`
153
- - `repl`
154
- - `tls`
144
+ * `child_process`
145
+ * `cluster`
146
+ * `dgram`
147
+ * `dns`
148
+ * `fs`
149
+ * `module`
150
+ * `net`
151
+ * `readline`
152
+ * `repl`
153
+ * `tls`
155
154
 
156
155
  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
156
 
@@ -0,0 +1,69 @@
1
+ # Polyfill Plugin
2
+
3
+ :::tip
4
+ Normally, we don't need to inject polyfill for npm packages, this step should be done on the web application framework side, but in some scenarios we need to inject polyfill in order to make our library run directly in low version browsers.
5
+
6
+ Note that this plugin does not transform your code syntax, it only injects polyfill for unsupported functions used in your code, importing them as normal functions instead of polluting the global. You need to install the `core-js-pure` dependency.
7
+
8
+ :::
9
+
10
+ ## Quick start
11
+
12
+ ### Install
13
+
14
+ ```bash
15
+ # npm
16
+ npm install @modern-js/plugin-module-polyfill -D
17
+
18
+ # yarn
19
+ yarn add @modern-js/plugin-module-polyfill -D
20
+
21
+ # pnpm
22
+ pnpm add @modern-js/plugin-module-polyfill -D
23
+ ```
24
+
25
+ ### Register
26
+
27
+ In Module Tools, you can register plugins in the following way:
28
+
29
+ ```ts
30
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
31
+ import { modulePluginPolyfill } from '@modern-js/plugin-module-polyfill';
32
+
33
+ export default defineConfig({
34
+ plugins: [
35
+ moduleTools(),
36
+ modulePluginPolyfill(),
37
+ ],
38
+ });
39
+ ```
40
+
41
+ ## Config
42
+
43
+ * **Type**
44
+
45
+ ```ts
46
+ type options = {
47
+ targets?: Record<string, string> | string;
48
+ }
49
+ ```
50
+
51
+ ### targets
52
+
53
+ See [babel target](https://babeljs.io/docs/options#targets).
54
+
55
+ This is a example.
56
+
57
+ ```ts
58
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
59
+ import { modulePluginPolyfill } from '@modern-js/plugin-module-polyfill';
60
+
61
+ export default defineConfig({
62
+ plugins: [
63
+ moduleTools(),
64
+ modulePluginPolyfill({
65
+ targets: "> 0.25%, not dead"
66
+ }),
67
+ ],
68
+ });
69
+ ```
@@ -363,7 +363,7 @@ export default defineConfig({
363
363
 
364
364
  :::
365
365
 
366
- <!-- ## externalHelpers
366
+ ## externalHelpers
367
367
 
368
368
  默认情况下,输出的 JS 代码可能会依赖一些辅助函数来支持目标环境或者输出格式,这些辅助函数会被内联在需要它的文件中。
369
369
 
@@ -406,7 +406,7 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
406
406
  export var yourCode = function() {
407
407
  // use _async_to_generator
408
408
  }
409
- ``` -->
409
+ ```
410
410
 
411
411
 
412
412
  ## externals
@@ -569,12 +569,12 @@ export default defineConfig({
569
569
  - 类型: `boolean | 'inline' | 'external'`
570
570
  - 默认值: `false`
571
571
 
572
- <!-- ## sourceType
572
+ ## sourceType
573
573
 
574
574
  设置源码的格式。默认情况下,会将源码作为 EsModule 进行处理。当源码使用的是 CommonJS 的时候,需要设置 `commonjs`。
575
575
 
576
576
  - 类型:`commonjs` | `module`
577
- - 默认值:`module` -->
577
+ - 默认值:`module`
578
578
 
579
579
  ## splitting
580
580
 
@@ -861,7 +861,7 @@ const tailwind = {
861
861
  - 类型: `'es5' | 'es6' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'es2021' | 'es2022' | 'esnext'`
862
862
  - 默认值: `'es6'`
863
863
 
864
- <!-- ## transformImport
864
+ ## transformImport
865
865
 
866
866
  提供与 babel-plugin-import 等价的能力和配置,基于 SWC 实现。
867
867
 
@@ -893,7 +893,7 @@ export default defineConfig({
893
893
 
894
894
  ### 注意事项
895
895
 
896
- 参考[【Import 插件——注意事项】](plugins/official-list/plugin-import.html#注意事项) -->
896
+ 参考[【Import 插件——注意事项】](plugins/official-list/plugin-import.html#注意事项)
897
897
 
898
898
 
899
899
  ## umdGlobals
@@ -4,4 +4,4 @@ sidebar_label: 概览
4
4
  sidebar_position: 1
5
5
  ---
6
6
 
7
- # index
7
+ # 概览
@@ -72,7 +72,7 @@ export default () => {
72
72
  pnpm run new
73
73
 
74
74
  ? 请选择你想要的操作 启用可选功能
75
- ? 启用可选功能 启用「Storybook」
75
+ ? 请选择功能名称 启用「Storybook」
76
76
  ```
77
77
 
78
78
  ---
@@ -83,7 +83,7 @@ pnpm run new
83
83
  pnpm run new
84
84
 
85
85
  ? 请选择你想要的操作 启用可选功能
86
- ? 启用可选功能 启用「Storybook」
86
+ ? 请选择功能名称 启用「Storybook」
87
87
  ```
88
88
 
89
89
  ---
@@ -361,7 +361,7 @@ body {
361
361
  pnpm run new
362
362
 
363
363
  ? 请选择你想要的操作 启用可选功能
364
- ? 启用可选功能 启用「Storybook」
364
+ ? 请选择功能名称 启用「Storybook」
365
365
  ```
366
366
 
367
367
  ---
@@ -372,7 +372,7 @@ pnpm run new
372
372
  pnpm run new
373
373
 
374
374
  ? 请选择你想要的操作 启用可选功能
375
- ? 启用可选功能 启用 Tailwind CSS 支持
375
+ ? 请选择功能名称 启用 Tailwind CSS 支持
376
376
  ```
377
377
 
378
378
  ---
@@ -5,3 +5,5 @@
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
7
  * [@modern-js/plugin-module-node-polyfill](./plugin-node-polyfill.mdx):会自动注入 Node 核心模块在浏览器端的 polyfills。
8
+ * [@modern-js/plugin-module-polyfill](./plugin-polyfill.md):为你的代码中使用到的不支持的功能注入 polyfill。
9
+ * [@modern-js/plugin-module-babel](./plugin-babel.md):使用 babel 转换你的代码。
@@ -0,0 +1,56 @@
1
+ # Babel 插件
2
+
3
+ :::tip
4
+ 通常情况下,我们无需使用 Babel 转换我们的代码,此插件仅作为一种降级方式。
5
+ :::
6
+
7
+ ## 快速开始
8
+
9
+ ### 安装
10
+
11
+ ```bash
12
+ # npm
13
+ npm install @modern-js/plugin-module-babel -D
14
+
15
+ # yarn
16
+ yarn add @modern-js/plugin-module-babel -D
17
+
18
+ # pnpm
19
+ pnpm add @modern-js/plugin-module-babel -D
20
+ ```
21
+
22
+ ### 注册插件
23
+
24
+ 在 Module Tools 中,你可以按照如下方式注册插件:
25
+
26
+ ```ts
27
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
28
+ import { modulePluginBabel } from '@modern-js/plugin-module-babel';
29
+
30
+ export default defineConfig({
31
+ plugins: [
32
+ moduleTools(),
33
+ modulePluginBabel(),
34
+ ],
35
+ });
36
+ ```
37
+
38
+ ## 配置
39
+
40
+ See [babel options](https://babeljs.io/docs/options)
41
+
42
+ 下面是一个配置了`@babel/preset-env`的例子:
43
+
44
+ ```ts
45
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
46
+ import { modulePluginBabel } from '@modern-js/plugin-module-babel';
47
+
48
+ export default defineConfig({
49
+ plugins: [
50
+ moduleTools(),
51
+ modulePluginBabel({
52
+ presets: [['@babel/preset-env']],
53
+ }),
54
+ ],
55
+ });
56
+ ```
@@ -17,19 +17,18 @@ yarn add @modern-js/plugin-module-banner -D
17
17
  pnpm add @modern-js/plugin-module-banner -D
18
18
  ```
19
19
 
20
-
21
20
  ### 注册插件
22
21
 
23
22
  在 Module Tools 中,你可以按照如下方式注册插件:
24
23
 
25
24
  ```ts
26
- import moduleTools, defineConfig from '@modern-js/module-tools';
27
- import pluginBanner from '@modern-js/plugin-module-banner';
25
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
26
+ import { modulePluginBanner } from '@modern-js/plugin-module-banner';
28
27
 
29
28
  export default defineConfig({
30
29
  plugins: [
31
30
  moduleTools(),
32
- pluginBanner({
31
+ modulePluginBanner({
33
32
  banner: {
34
33
  js: '//comment',
35
34
  css: '/*comment*/',
@@ -48,7 +47,8 @@ export default defineConfig({
48
47
  ### 在 JS 文件顶部增加版权信息
49
48
 
50
49
  ```ts
51
- import pluginBanner from '@modern-js/plugin-module-banner';
50
+ import { modulePluginBanner } from '@modern-js/plugin-module-banner';
51
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
52
52
 
53
53
  const copyRight = `/*
54
54
  © Copyright 2020 xxx.com or one of its affiliates.
@@ -62,7 +62,8 @@ const copyRight = `/*
62
62
 
63
63
  export default defineConfig({
64
64
  plugins: [
65
- pluginBanner({
65
+ moduleTools(),
66
+ modulePluginBanner({
66
67
  banner: {
67
68
  js: copyRight,
68
69
  },
@@ -73,7 +74,7 @@ export default defineConfig({
73
74
 
74
75
  ## 配置
75
76
 
76
- * 类型:
77
+ * **类型:**
77
78
 
78
79
  ```ts
79
80
  type BannerOptions = {
@@ -2,12 +2,10 @@
2
2
 
3
3
  提供与 [babel-plugin-import](https://github.com/umijs/babel-plugin-import) 等价的能力和配置,基于 [SWC](https://swc.rs/) 实现。
4
4
 
5
- {/*
6
5
  :::tip
7
6
  从 @modern-js/module-tools 2.16.0 版本开始,该插件功能内置在 module-tools 中,由 [`transformImport`](api/config/build-config.html#transformimport)
8
7
  配置提供。
9
8
  :::
10
- */}
11
9
 
12
10
  ## 快速开始
13
11
 
@@ -29,13 +27,13 @@ pnpm add @modern-js/plugin-module-import -D
29
27
  在 Module Tools 中,你可以按照如下方式注册插件:
30
28
 
31
29
  ```ts
32
- import moduleTools, defineConfig from '@modern-js/module-tools';
33
- import moduleImport from '@modern-js/plugin-module-import';
30
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
31
+ import { modulePluginImport } from '@modern-js/plugin-module-import';
34
32
 
35
33
  export default defineConfig({
36
34
  plugins: [
37
35
  moduleTools(),
38
- moduleImport({
36
+ modulePluginImport({
39
37
  pluginImport: [{
40
38
  libraryName: 'antd',
41
39
  style: true,
@@ -49,7 +47,7 @@ export default defineConfig({
49
47
 
50
48
  ## 配置
51
49
 
52
- * 类型:
50
+ * **类型:**
53
51
 
54
52
  ```ts
55
53
  type Options = {
@@ -66,11 +64,13 @@ type Options = {
66
64
  使用示例:
67
65
 
68
66
  ```ts
69
- import moduleImport from '@modern-js/plugin-module-import';
67
+ import { modulePluginImport } from '@modern-js/plugin-module-import';
68
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
70
69
 
71
70
  export default defineConfig({
72
71
  plugins: [
73
- moduleImport({
72
+ moduleTools(),
73
+ modulePluginImport({
74
74
  pluginImport: [
75
75
  // babel-plugin-import 的 options 配置
76
76
  {
@@ -109,13 +109,14 @@ import { MyButton as Btn } from 'foo';
109
109
 
110
110
  添加右侧的配置:
111
111
 
112
-
113
112
  ```ts
114
- import moduleImport from '@modern-js/plugin-module-import';
113
+ import { modulePluginImport } from '@modern-js/plugin-module-import';
114
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
115
115
 
116
116
  export default defineConfig({
117
117
  plugins: [
118
- moduleImport({
118
+ moduleTools(),
119
+ modulePluginImport({
119
120
  pluginImport: [{
120
121
  libraryName: 'foo',
121
122
  customName: 'foo/es/{{ member }}'
@@ -130,11 +131,13 @@ export default defineConfig({
130
131
  其中的 `{{ member }}` 会被替换为相应的引入成员
131
132
 
132
133
  ```ts focus=8:8
133
- import moduleImport from '@modern-js/plugin-module-import';
134
+ import { modulePluginImport } from '@modern-js/plugin-module-import';
135
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
134
136
 
135
137
  export default defineConfig({
136
138
  plugins: [
137
- moduleImport({
139
+ moduleTools(),
140
+ modulePluginImport({
138
141
  pluginImport: [{
139
142
  libraryName: 'foo',
140
143
  customName: 'foo/es/{{ member }}'
@@ -144,7 +147,6 @@ export default defineConfig({
144
147
  });
145
148
  ```
146
149
 
147
-
148
150
  ---
149
151
  转换后:
150
152
 
@@ -154,17 +156,18 @@ import Btn from 'foo/es/MyButton';
154
156
 
155
157
  </CH.Spotlight>
156
158
 
157
-
158
159
  可以看出配置 `customName: "foo/es/{{ member }}"` 的效果等同于配置 `` customName: (member) => `foo/es/${member}` `` ,但是不会有 Node-API 的调用开销。
159
160
 
160
161
  这里使用到的模版是 [handlebars](https://handlebarsjs.com),模版配置中还内置了一些有用的辅助工具,还是以上面的导入语句为例,配置成:
161
162
 
162
163
  ```ts focus=8:8
163
- import moduleImport from '@modern-js/plugin-module-import';
164
+ import { modulePluginImport } from '@modern-js/plugin-module-import';
165
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
164
166
 
165
167
  export default defineConfig({
166
168
  plugins: [
167
- moduleImport({
169
+ moduleTools(),
170
+ modulePluginImport({
168
171
  pluginImport: [{
169
172
  libraryName: 'foo',
170
173
  customName: 'foo/es/{{ kebabCase member }}',
@@ -39,7 +39,7 @@ export default defineConfig({
39
39
 
40
40
  ## 配置
41
41
 
42
- * 类型:
42
+ * **类型:**
43
43
 
44
44
  ```ts
45
45
  type NodePolyfillOptions = {
@@ -86,14 +86,13 @@ export default defineConfig({
86
86
  });
87
87
  ```
88
88
 
89
-
90
89
  ## Node Polyfills
91
90
 
92
91
  ### Globals
93
92
 
94
- - `Buffer`
95
- - `process`
96
- - `console`
93
+ * `Buffer`
94
+ * `process`
95
+ * `console`
97
96
 
98
97
  当你在代码中使用以上全局变量时,对应 polyfill 会被自动注入。
99
98
 
@@ -103,34 +102,34 @@ const bufferData = Buffer.from('xxxx');
103
102
 
104
103
  ### Modules
105
104
 
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`
105
+ * `assert`
106
+ * `buffer`
107
+ * `console`
108
+ * `constants`
109
+ * `crypto`
110
+ * `domain`
111
+ * `events`
112
+ * `http`
113
+ * `https`
114
+ * `os`
115
+ * `path`
116
+ * `punycode`
117
+ * `process`
118
+ * `querystring`
119
+ * `stream`
120
+ * `_stream_duplex`
121
+ * `_stream_passthrough`
122
+ * `_stream_readable`
123
+ * `_stream_transform`
124
+ * `_stream_writable`
125
+ * `string_decoder`
126
+ * `sys`
127
+ * `timers`
128
+ * `tty`
129
+ * `url`
130
+ * `util`
131
+ * `vm`
132
+ * `zlib`
134
133
 
135
134
  当你通过 `require` 或 `import` 等语法在代码中引用以上模块时,对应 polyfill 会被注入。
136
135
 
@@ -142,16 +141,16 @@ const bufferData = Buffer.from('xxxx');
142
141
 
143
142
  ### Fallbacks
144
143
 
145
- - `child_process`
146
- - `cluster`
147
- - `dgram`
148
- - `dns`
149
- - `fs`
150
- - `module`
151
- - `net`
152
- - `readline`
153
- - `repl`
154
- - `tls`
144
+ * `child_process`
145
+ * `cluster`
146
+ * `dgram`
147
+ * `dns`
148
+ * `fs`
149
+ * `module`
150
+ * `net`
151
+ * `readline`
152
+ * `repl`
153
+ * `tls`
155
154
 
156
155
  目前浏览器端没有以上模块的 polyfill,因此当你引用以上模块时,会自动 fallback 为一个空对象。
157
156
 
@@ -0,0 +1,69 @@
1
+ # Polyfill 插件
2
+
3
+ :::tip
4
+ 通常情况下,我们不需要为 npm 包注入 polyfill,这一步应该在 Web 应用的框架侧完成,但是在某些场景,为了让我们的库能够直接运行在低版本浏览器里,我们需要注入 polyfill。
5
+
6
+ 请注意,此插件并不会转化你的代码语法,只会为你的代码中使用到的不支持的功能注入 polyfill,把它们作为普通函数导入而不是污染全局。你需要安装 `core-js-pure` 依赖
7
+
8
+ :::
9
+
10
+ ## 快速开始
11
+
12
+ ### 安装
13
+
14
+ ```bash
15
+ # npm
16
+ npm install @modern-js/plugin-module-polyfill -D
17
+
18
+ # yarn
19
+ yarn add @modern-js/plugin-module-polyfill -D
20
+
21
+ # pnpm
22
+ pnpm add @modern-js/plugin-module-polyfill -D
23
+ ```
24
+
25
+ ### 注册插件
26
+
27
+ 在 Module Tools 中,你可以按照如下方式注册插件:
28
+
29
+ ```ts
30
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
31
+ import { modulePluginPolyfill } from '@modern-js/plugin-module-polyfill';
32
+
33
+ export default defineConfig({
34
+ plugins: [
35
+ moduleTools(),
36
+ modulePluginPolyfill(),
37
+ ],
38
+ });
39
+ ```
40
+
41
+ ## 配置
42
+
43
+ * **类型:**
44
+
45
+ ```ts
46
+ type options = {
47
+ targets?: Record<string, string> | string;
48
+ }
49
+ ```
50
+
51
+ ### targets
52
+
53
+ See [babel target](https://babeljs.io/docs/options#targets).
54
+
55
+ This is a example.
56
+
57
+ ```ts
58
+ import moduleTools, { defineConfig } from '@modern-js/module-tools';
59
+ import { modulePluginPolyfill } from '@modern-js/plugin-module-polyfill';
60
+
61
+ export default defineConfig({
62
+ plugins: [
63
+ moduleTools(),
64
+ modulePluginPolyfill({
65
+ targets: "> 0.25%, not dead"
66
+ }),
67
+ ],
68
+ });
69
+ ```
package/package.json CHANGED
@@ -9,15 +9,15 @@
9
9
  "directory": "packages/document/module-doc"
10
10
  },
11
11
  "license": "MIT",
12
- "version": "2.21.1",
12
+ "version": "2.22.0",
13
13
  "main": "index.js",
14
14
  "dependencies": {
15
15
  "@code-hike/mdx": "^0.7.4",
16
16
  "react": "^18.2.0",
17
17
  "react-dom": "^18.2.0",
18
18
  "shiki": "^0.11.1",
19
- "@modern-js/doc-plugin-auto-sidebar": "2.21.1",
20
- "@modern-js/doc-tools": "2.21.1"
19
+ "@modern-js/doc-plugin-auto-sidebar": "2.22.0",
20
+ "@modern-js/doc-tools": "2.22.0"
21
21
  },
22
22
  "scripts": {
23
23
  "dev": "modern dev",