@modern-js/module-tools-docs 2.7.0 → 2.8.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 +10 -0
- package/README.md +2 -2
- package/docs/en/guide/basic/command-preview.md +1 -1
- package/docs/en/guide/intro/getting-started.md +1 -1
- package/docs/en/plugins/official-list/overview.md +3 -1
- package/docs/en/plugins/official-list/plugin-banner.md +103 -0
- package/docs/en/plugins/official-list/plugin-import.mdx +177 -0
- package/docs/zh/guide/basic/command-preview.md +1 -1
- package/docs/zh/guide/basic/use-micro-generator.md +2 -2
- package/docs/zh/guide/intro/getting-started.md +1 -1
- package/docs/zh/plugins/official-list/overview.md +2 -1
- package/docs/zh/plugins/official-list/plugin-banner.md +103 -0
- package/docs/zh/plugins/official-list/plugin-import.mdx +176 -0
- package/modern.config.ts +3 -3
- package/package.json +5 -5
package/CHANGELOG.md
ADDED
package/README.md
CHANGED
|
@@ -19,8 +19,8 @@ Please follow [Quick Start](https://modernjs.dev/module-tools/en/guide/intro/get
|
|
|
19
19
|
|
|
20
20
|
## Contributing
|
|
21
21
|
|
|
22
|
-
Please read the [Contributing Guide](https://github.com/
|
|
22
|
+
Please read the [Contributing Guide](https://github.com/web-infra-dev/modern.js/blob/main/CONTRIBUTING.md).
|
|
23
23
|
|
|
24
24
|
## License
|
|
25
25
|
|
|
26
|
-
Modern.js is [MIT licensed](https://github.com/
|
|
26
|
+
Modern.js is [MIT licensed](https://github.com/web-infra-dev/modern.js/blob/main/LICENSE).
|
|
@@ -178,7 +178,7 @@ Usage: modern gen-release-note [options]
|
|
|
178
178
|
Generate Release Note based on current repository changeset information
|
|
179
179
|
|
|
180
180
|
Options:
|
|
181
|
-
--repo <repo> The name of the repository to generate the Pull Request link, e.g.:
|
|
181
|
+
--repo <repo> The name of the repository to generate the Pull Request link, e.g.: web-infra-dev/modern.js
|
|
182
182
|
--custom <cumtom> Custom Release Note generation function
|
|
183
183
|
-h, --help display help for command
|
|
184
184
|
```
|
|
@@ -46,7 +46,7 @@ If your project has a `src/index.(js|jsx)` file or both `src/index.(ts|tsx)` and
|
|
|
46
46
|
**If you want to see the complete project using the modular engineering scheme, you can execute the following command**.
|
|
47
47
|
|
|
48
48
|
```bash
|
|
49
|
-
git clone https://github.com/
|
|
49
|
+
git clone https://github.com/web-infra-dev/module-tools-examples
|
|
50
50
|
cd module-tools-example/base
|
|
51
51
|
|
|
52
52
|
## Execute the build.
|
|
@@ -2,5 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Official Plugin
|
|
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
|
+
* [@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
|
+
|
|
6
8
|
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Banner Plugin
|
|
2
|
+
|
|
3
|
+
Provide the ability to inject content at the top and bottom of each JS and CSS file.
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
### Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# npm
|
|
11
|
+
npm install @modern-js/plugin-module-banner -D
|
|
12
|
+
|
|
13
|
+
# yarn
|
|
14
|
+
yarn add @modern-js/plugin-module-banner -D
|
|
15
|
+
|
|
16
|
+
# pnpm
|
|
17
|
+
pnpm add @modern-js/plugin-module-banner -D
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Register
|
|
22
|
+
|
|
23
|
+
You can install the plugin with the following command:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import moduleTools, defineConfig from '@modern-js/module-tools';
|
|
27
|
+
import pluginBanner from '@modern-js/plugin-module-banner';
|
|
28
|
+
|
|
29
|
+
export default defineConfig({
|
|
30
|
+
plugins: [
|
|
31
|
+
moduleTools(),
|
|
32
|
+
pluginBanner({
|
|
33
|
+
banner: {
|
|
34
|
+
js: '//comment',
|
|
35
|
+
css: '/*comment*/',
|
|
36
|
+
},
|
|
37
|
+
}),
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
:::tip
|
|
43
|
+
Note: CSS comments do not support the `//comment` syntax. Refer to [【CSS Comments】](https://developer.mozilla.org/en-US/docs/Web/CSS/Comments)
|
|
44
|
+
:::
|
|
45
|
+
|
|
46
|
+
## Example
|
|
47
|
+
|
|
48
|
+
### Add copyright information at the top of a JS file
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import pluginBanner from '@modern-js/plugin-module-banner';
|
|
52
|
+
|
|
53
|
+
const copyRight = `/*
|
|
54
|
+
© Copyright 2020 xxx.com or one of its affiliates.
|
|
55
|
+
* Some Sample Copyright Text Line
|
|
56
|
+
* Some Sample Copyright Text Line
|
|
57
|
+
* Some Sample Copyright Text Line
|
|
58
|
+
* Some Sample Copyright Text Line
|
|
59
|
+
* Some Sample Copyright Text Line
|
|
60
|
+
* Some Sample Copyright Text Line
|
|
61
|
+
*/`;
|
|
62
|
+
|
|
63
|
+
export default defineConfig({
|
|
64
|
+
plugins: [
|
|
65
|
+
pluginBanner({
|
|
66
|
+
banner: {
|
|
67
|
+
js: copyRight,
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
],
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Configuration
|
|
75
|
+
|
|
76
|
+
* **Type:**
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
type BannerOptions = {
|
|
80
|
+
banner: {
|
|
81
|
+
js?: string;
|
|
82
|
+
css?: string;
|
|
83
|
+
};
|
|
84
|
+
footer?: {
|
|
85
|
+
js?: string;
|
|
86
|
+
css?: string;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### banner
|
|
92
|
+
|
|
93
|
+
Add content at the top.
|
|
94
|
+
|
|
95
|
+
* `banner.js`: Add content at the top of a JS file.
|
|
96
|
+
* `banner.css`: Add content at the top of a CSS file.
|
|
97
|
+
|
|
98
|
+
### footer
|
|
99
|
+
|
|
100
|
+
Add content at the bottom.
|
|
101
|
+
|
|
102
|
+
* `footer.js`: Add content at the bottom of a JS file.
|
|
103
|
+
* `footer.css`: Add content at the bottom of a CSS file.
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# Import Plugin
|
|
2
|
+
|
|
3
|
+
Using [SWC](https://swc.rs/) provides the same ability and configuration as [`babel-plugin-import`](https://github.com/umijs/babel-plugin-import).
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
### Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# npm
|
|
11
|
+
npm install @modern-js/plugin-module-import -D
|
|
12
|
+
|
|
13
|
+
# yarn
|
|
14
|
+
yarn add @modern-js/plugin-module-import -D
|
|
15
|
+
|
|
16
|
+
# pnpm
|
|
17
|
+
pnpm add @modern-js/plugin-module-import -D
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Register
|
|
21
|
+
|
|
22
|
+
In Module Tools, you can register plugins in the following way:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import moduleTools, defineConfig from '@modern-js/module-tools';
|
|
26
|
+
import moduleImport from '@modern-js/plugin-module-import';
|
|
27
|
+
|
|
28
|
+
export default defineConfig({
|
|
29
|
+
plugins: [
|
|
30
|
+
moduleTools(),
|
|
31
|
+
moduleImport({
|
|
32
|
+
pluginImport: [{
|
|
33
|
+
libraryName: 'antd',
|
|
34
|
+
style: true,
|
|
35
|
+
}],
|
|
36
|
+
}),
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
This way we can use the ability of automatic import in Module Tools.
|
|
42
|
+
|
|
43
|
+
## Configurations
|
|
44
|
+
|
|
45
|
+
* **Type**:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
type Options = {
|
|
49
|
+
pluginImport?: ImportItem[];
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### pluginImport
|
|
54
|
+
|
|
55
|
+
* **Type**: `Array`
|
|
56
|
+
|
|
57
|
+
The elements of the array are configuration objects for `babel-plugin-import`, which can be referred to [options](https://github.com/umijs/babel-plugin-import#options)。
|
|
58
|
+
|
|
59
|
+
**Example:**
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import moduleImport from '@modern-js/plugin-module-import';
|
|
63
|
+
|
|
64
|
+
export default defineConfig({
|
|
65
|
+
plugins: [
|
|
66
|
+
moduleImport({
|
|
67
|
+
pluginImport: [
|
|
68
|
+
// babel-plugin-import`s options config
|
|
69
|
+
{
|
|
70
|
+
libraryName: 'foo',
|
|
71
|
+
style: true,
|
|
72
|
+
}
|
|
73
|
+
],
|
|
74
|
+
}),
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Notes
|
|
80
|
+
|
|
81
|
+
[SWC](https://swc.rs/) (Speedy Web Compiler) is written in Rust, and this plugin is based on SWC and ported from [babel-plugin-import](https://github.com/umijs/babel-plugin-import). The configuration options remain consistent.
|
|
82
|
+
|
|
83
|
+
Some configurations can accept functions, such as `customName`, `customStyleName`, etc. These JavaScript functions are called by Rust through Node-API, which can cause some performance degradation.
|
|
84
|
+
|
|
85
|
+
Simple function logic can be replaced by template language. Therefore, for configurations such as `customName`, `customStyleName`, etc., instead of passing functions, you can also pass a string as a template to replace the function, improving performance.
|
|
86
|
+
|
|
87
|
+
<CH.Spotlight>
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { MyButton as Btn } from 'foo';
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
We will use this piece of code for illustration.
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { MyButton as Btn } from 'foo';
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
Add the following configuration on the right-hand side:
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import moduleImport from '@modern-js/plugin-module-import';
|
|
108
|
+
|
|
109
|
+
export default defineConfig({
|
|
110
|
+
plugins: [
|
|
111
|
+
moduleImport({
|
|
112
|
+
pluginImport: [{
|
|
113
|
+
libraryName: 'foo',
|
|
114
|
+
customName: 'foo/es/{{ member }}'
|
|
115
|
+
}],
|
|
116
|
+
}),
|
|
117
|
+
],
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
The `{{ member }}` in it will be replaced with the corresponding import member.
|
|
124
|
+
|
|
125
|
+
```ts focus=8:8
|
|
126
|
+
import moduleImport from '@modern-js/plugin-module-import';
|
|
127
|
+
|
|
128
|
+
export default defineConfig({
|
|
129
|
+
plugins: [
|
|
130
|
+
moduleImport({
|
|
131
|
+
pluginImport: [{
|
|
132
|
+
libraryName: 'foo',
|
|
133
|
+
customName: 'foo/es/{{ member }}'
|
|
134
|
+
}],
|
|
135
|
+
}),
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
After transformation:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import Btn from 'foo/es/MyButton';
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
</CH.Spotlight>
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
Template `customName: 'foo/es/{{ member }}'` is the same as `` customName: (member) => `foo/es/${member}` ``, but template value has no performance overhead of Node-API.
|
|
152
|
+
|
|
153
|
+
The template used here is [handlebars](https://handlebarsjs.com). There are some useful builtin tools, Take the above import statement as an example:
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
```ts focus=8:8
|
|
157
|
+
import moduleImport from '@modern-js/plugin-module-import';
|
|
158
|
+
|
|
159
|
+
export default defineConfig({
|
|
160
|
+
plugins: [
|
|
161
|
+
moduleImport({
|
|
162
|
+
pluginImport: [{
|
|
163
|
+
libraryName: 'foo',
|
|
164
|
+
customName: 'foo/es/{{ kebabCase member }}',
|
|
165
|
+
}],
|
|
166
|
+
}),
|
|
167
|
+
],
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Transformed to:
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import Btn from 'foo/es/my-button';
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
In addition to kebabCase, there are cameraCase, snakeCase, upperCase and lowerCase can be used as well.
|
|
@@ -178,7 +178,7 @@ Usage: modern gen-release-note [options]
|
|
|
178
178
|
根据当前仓库 changeset 信息生成 Release Note
|
|
179
179
|
|
|
180
180
|
Options:
|
|
181
|
-
--repo <repo> 仓库名称,用于生成 Pull Request 链接, 例如:
|
|
181
|
+
--repo <repo> 仓库名称,用于生成 Pull Request 链接, 例如: web-infra-dev/modern.js
|
|
182
182
|
--custom <cumtom> 自定义 Release Note 生成函数
|
|
183
183
|
-h, --help display help for command
|
|
184
184
|
```
|
|
@@ -63,7 +63,7 @@ export default defineConfig({
|
|
|
63
63
|
|
|
64
64
|
关于如何在模块工程里使用 Tailwind CSS,可以查看:
|
|
65
65
|
|
|
66
|
-
- [使用 Tailwind CSS](https://modernjs.dev/
|
|
66
|
+
- [使用 Tailwind CSS](https://modernjs.dev/module-tools/guide/best-practices/components.html#tailwind-css)
|
|
67
67
|
|
|
68
68
|
:::tip
|
|
69
69
|
在成功开启后,会提示需要手动在配置中增加如下类似的代码。
|
|
@@ -82,7 +82,7 @@ export default defineConfig({
|
|
|
82
82
|
|
|
83
83
|
## 启动 Modern.js Runtime API
|
|
84
84
|
|
|
85
|
-
**Modern.js 提供了 [Runtime API](https://modernjs.dev/
|
|
85
|
+
**Modern.js 提供了 [Runtime API](https://modernjs.dev/configure/app/runtime/intro) 能力,这些 API 只能在 Modern.js 的应用项目环境中使用**。如果你需要开发一个 Modern.js 应用环境中使用的组件,那么你可以开启该特性,微生成器会增加 `"@modern-js/runtime"`依赖。
|
|
86
86
|
|
|
87
87
|
另外,Storybook 调试工具也会通过检测项目的依赖确定项目是否需要使用 Runtime API,并且提供与 Modern.js 应用项目一样的 Runtime API 运行环境。
|
|
88
88
|
|
|
@@ -44,7 +44,7 @@ export default defineConfig({
|
|
|
44
44
|
**如果你想要看看使用了模块工程方案的完整项目,可以执行以下命令**:
|
|
45
45
|
|
|
46
46
|
```bash
|
|
47
|
-
git clone https://github.com/
|
|
47
|
+
git clone https://github.com/web-infra-dev/module-tools-examples
|
|
48
48
|
cd module-tools-example/base
|
|
49
49
|
|
|
50
50
|
## 执行构建:
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Banner 插件
|
|
2
|
+
|
|
3
|
+
提供为每个 JS 和 CSS 文件的顶部和底部注入内容的能力。
|
|
4
|
+
|
|
5
|
+
## 快速开始
|
|
6
|
+
|
|
7
|
+
### 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# npm
|
|
11
|
+
npm install @modern-js/plugin-module-banner -D
|
|
12
|
+
|
|
13
|
+
# yarn
|
|
14
|
+
yarn add @modern-js/plugin-module-banner -D
|
|
15
|
+
|
|
16
|
+
# pnpm
|
|
17
|
+
pnpm add @modern-js/plugin-module-banner -D
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### 注册插件
|
|
22
|
+
|
|
23
|
+
在 Module Tools 中,你可以按照如下方式注册插件:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import moduleTools, defineConfig from '@modern-js/module-tools';
|
|
27
|
+
import pluginBanner from '@modern-js/plugin-module-banner';
|
|
28
|
+
|
|
29
|
+
export default defineConfig({
|
|
30
|
+
plugins: [
|
|
31
|
+
moduleTools(),
|
|
32
|
+
pluginBanner({
|
|
33
|
+
banner: {
|
|
34
|
+
js: '//comment',
|
|
35
|
+
css: '/*comment*/',
|
|
36
|
+
},
|
|
37
|
+
}),
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
:::tip
|
|
43
|
+
注意:CSS 的注释不支持 `//comment` 这样的写法。详见[【CSS 注释】](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Comments)
|
|
44
|
+
:::
|
|
45
|
+
|
|
46
|
+
## 示例
|
|
47
|
+
|
|
48
|
+
### 在 JS 文件顶部增加版权信息
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import pluginBanner from '@modern-js/plugin-module-banner';
|
|
52
|
+
|
|
53
|
+
const copyRight = `/*
|
|
54
|
+
© Copyright 2020 xxx.com or one of its affiliates.
|
|
55
|
+
* Some Sample Copyright Text Line
|
|
56
|
+
* Some Sample Copyright Text Line
|
|
57
|
+
* Some Sample Copyright Text Line
|
|
58
|
+
* Some Sample Copyright Text Line
|
|
59
|
+
* Some Sample Copyright Text Line
|
|
60
|
+
* Some Sample Copyright Text Line
|
|
61
|
+
*/`;
|
|
62
|
+
|
|
63
|
+
export default defineConfig({
|
|
64
|
+
plugins: [
|
|
65
|
+
pluginBanner({
|
|
66
|
+
banner: {
|
|
67
|
+
js: copyRight,
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
],
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## 配置
|
|
75
|
+
|
|
76
|
+
* 类型:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
type BannerOptions = {
|
|
80
|
+
banner: {
|
|
81
|
+
js?: string;
|
|
82
|
+
css?: string;
|
|
83
|
+
};
|
|
84
|
+
footer?: {
|
|
85
|
+
js?: string;
|
|
86
|
+
css?: string;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### banner
|
|
92
|
+
|
|
93
|
+
在顶部增加内容。
|
|
94
|
+
|
|
95
|
+
* `banner.js`:在 JS 文件顶部增加内容。
|
|
96
|
+
* `banner.css`:在 CSS 文件顶部增加内容。
|
|
97
|
+
|
|
98
|
+
### footer
|
|
99
|
+
|
|
100
|
+
在底部增加内容。
|
|
101
|
+
|
|
102
|
+
* `footer.js`:在 JS 文件底部增加内容。
|
|
103
|
+
* `footer.css`:在 CSS 文件底部增加内容。
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# Import 插件
|
|
2
|
+
|
|
3
|
+
提供与 [babel-plugin-import](https://github.com/umijs/babel-plugin-import) 等价的能力和配置,基于 [SWC](https://swc.rs/) 实现。
|
|
4
|
+
|
|
5
|
+
## 快速开始
|
|
6
|
+
|
|
7
|
+
### 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# npm
|
|
11
|
+
npm install @modern-js/plugin-module-import -D
|
|
12
|
+
|
|
13
|
+
# yarn
|
|
14
|
+
yarn add @modern-js/plugin-module-import -D
|
|
15
|
+
|
|
16
|
+
# pnpm
|
|
17
|
+
pnpm add @modern-js/plugin-module-import -D
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### 注册插件
|
|
21
|
+
|
|
22
|
+
在 Module Tools 中,你可以按照如下方式注册插件:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import moduleTools, defineConfig from '@modern-js/module-tools';
|
|
26
|
+
import moduleImport from '@modern-js/plugin-module-import';
|
|
27
|
+
|
|
28
|
+
export default defineConfig({
|
|
29
|
+
plugins: [
|
|
30
|
+
moduleTools(),
|
|
31
|
+
moduleImport({
|
|
32
|
+
pluginImport: [{
|
|
33
|
+
libraryName: 'antd',
|
|
34
|
+
style: true,
|
|
35
|
+
}],
|
|
36
|
+
}),
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
这样我们就可以在 Module Tools 中使用自动导入的能力了。
|
|
42
|
+
|
|
43
|
+
## 配置
|
|
44
|
+
|
|
45
|
+
* 类型:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
type Options = {
|
|
49
|
+
pluginImport?: ImportItem[];
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### pluginImport
|
|
54
|
+
|
|
55
|
+
* 类型:`Array`
|
|
56
|
+
|
|
57
|
+
其中数组元素为一个 babel-plugin-import 的配置对象。配置对象可以参考 [options](https://github.com/umijs/babel-plugin-import#options)。
|
|
58
|
+
|
|
59
|
+
使用示例:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import moduleImport from '@modern-js/plugin-module-import';
|
|
63
|
+
|
|
64
|
+
export default defineConfig({
|
|
65
|
+
plugins: [
|
|
66
|
+
moduleImport({
|
|
67
|
+
pluginImport: [
|
|
68
|
+
// babel-plugin-import 的 options 配置
|
|
69
|
+
{
|
|
70
|
+
libraryName: 'foo',
|
|
71
|
+
style: true,
|
|
72
|
+
}
|
|
73
|
+
],
|
|
74
|
+
}),
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## 注意事项
|
|
80
|
+
|
|
81
|
+
[SWC](https://swc.rs/) (Speedy Web Compiler) 是基于 Rust 语言编写的,而该插件是基于 SWC,移植自 [babel-plugin-import](https://github.com/umijs/babel-plugin-import),配置选项保持了一致。
|
|
82
|
+
|
|
83
|
+
一些配置可以传入函数,例如 `customName`,`customStyleName` 等,这些 JavaScript 函数会由 Rust 通过 Node-API 调用,这种调用会造成一些性能劣化。
|
|
84
|
+
|
|
85
|
+
简单的函数逻辑其实可以通过模版语言来代替,因此 `customName`,`customStyleName` 等这些配置除了可以传入函数,也可以传入字符串作为模版来代替函数,提高性能。
|
|
86
|
+
|
|
87
|
+
<CH.Spotlight>
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { MyButton as Btn } from 'foo';
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
我们以这段代码进行说明。
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { MyButton as Btn } from 'foo';
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
添加右侧的配置:
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import moduleImport from '@modern-js/plugin-module-import';
|
|
108
|
+
|
|
109
|
+
export default defineConfig({
|
|
110
|
+
plugins: [
|
|
111
|
+
moduleImport({
|
|
112
|
+
pluginImport: [{
|
|
113
|
+
libraryName: 'foo',
|
|
114
|
+
customName: 'foo/es/{{ member }}'
|
|
115
|
+
}],
|
|
116
|
+
}),
|
|
117
|
+
],
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
其中的 `{{ member }}` 会被替换为相应的引入成员
|
|
124
|
+
|
|
125
|
+
```ts focus=8:8
|
|
126
|
+
import moduleImport from '@modern-js/plugin-module-import';
|
|
127
|
+
|
|
128
|
+
export default defineConfig({
|
|
129
|
+
plugins: [
|
|
130
|
+
moduleImport({
|
|
131
|
+
pluginImport: [{
|
|
132
|
+
libraryName: 'foo',
|
|
133
|
+
customName: 'foo/es/{{ member }}'
|
|
134
|
+
}],
|
|
135
|
+
}),
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
转换后:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import Btn from 'foo/es/MyButton';
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
</CH.Spotlight>
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
可以看出配置 `customName: "foo/es/{{ member }}"` 的效果等同于配置 `` customName: (member) => `foo/es/${member}` `` ,但是不会有 Node-API 的调用开销。
|
|
152
|
+
|
|
153
|
+
这里使用到的模版是 [handlebars](https://handlebarsjs.com),模版配置中还内置了一些有用的辅助工具,还是以上面的导入语句为例,配置成:
|
|
154
|
+
|
|
155
|
+
```ts focus=8:8
|
|
156
|
+
import moduleImport from '@modern-js/plugin-module-import';
|
|
157
|
+
|
|
158
|
+
export default defineConfig({
|
|
159
|
+
plugins: [
|
|
160
|
+
moduleImport({
|
|
161
|
+
pluginImport: [{
|
|
162
|
+
libraryName: 'foo',
|
|
163
|
+
customName: 'foo/es/{{ kebabCase member }}',
|
|
164
|
+
}],
|
|
165
|
+
}),
|
|
166
|
+
],
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
会转换成下面的结果:
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
import Btn from 'foo/es/my-button';
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
除了 `kebabCase` 以外还有 `camelCase`,`snakeCase`,`upperCase`,`lowerCase` 可以使用。
|
package/modern.config.ts
CHANGED
|
@@ -38,11 +38,11 @@ function getNavbar(lang: 'zh' | 'en'): NavItem[] {
|
|
|
38
38
|
items: [
|
|
39
39
|
{
|
|
40
40
|
text: getText('更新日志', 'Changelog'),
|
|
41
|
-
link: 'https://github.com/
|
|
41
|
+
link: 'https://github.com/web-infra-dev/modern.js/tree/main/packages/solutions/module-tools/CHANGELOG.md',
|
|
42
42
|
},
|
|
43
43
|
{
|
|
44
44
|
text: getText('贡献指南', 'Contributing'),
|
|
45
|
-
link: 'https://github.com/
|
|
45
|
+
link: 'https://github.com/web-infra-dev/modern.js/tree/main/packages/solutions/module-tools/CHANGELOG.md',
|
|
46
46
|
},
|
|
47
47
|
],
|
|
48
48
|
},
|
|
@@ -91,7 +91,7 @@ export default defineConfig({
|
|
|
91
91
|
icon: 'github',
|
|
92
92
|
mode: 'link',
|
|
93
93
|
content:
|
|
94
|
-
'https://github.com/
|
|
94
|
+
'https://github.com/web-infra-dev/modern.js/tree/main/packages/solutions/module-tools',
|
|
95
95
|
},
|
|
96
96
|
],
|
|
97
97
|
locales: [
|
package/package.json
CHANGED
|
@@ -2,18 +2,18 @@
|
|
|
2
2
|
"name": "@modern-js/module-tools-docs",
|
|
3
3
|
"description": "Shared documentation of Modern.js Module Tools",
|
|
4
4
|
"homepage": "https://modernjs.dev/module-tools",
|
|
5
|
-
"bugs": "https://github.com/
|
|
6
|
-
"repository": "
|
|
5
|
+
"bugs": "https://github.com/web-infra-dev/modern.js/issues",
|
|
6
|
+
"repository": "web-infra-dev/modern.js",
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"version": "2.
|
|
8
|
+
"version": "2.8.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-
|
|
16
|
-
"@modern-js/doc-
|
|
15
|
+
"@modern-js/doc-plugin-auto-sidebar": "2.8.0",
|
|
16
|
+
"@modern-js/doc-tools": "2.8.0"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"dev": "modern dev",
|