@modern-js/module-tools-docs 2.32.1 → 2.33.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/.eslintrc.js +1 -1
- package/CHANGELOG.md +11 -0
- package/docs/en/api/config/build-config.mdx +17 -16
- package/docs/en/api/config/build-preset.mdx +11 -72
- package/docs/en/guide/advance/asset.mdx +1 -1
- package/docs/en/guide/advance/copy.md +17 -18
- package/docs/en/guide/advance/external-dependency.mdx +1 -1
- package/docs/en/guide/advance/in-depth-about-build.md +27 -28
- package/docs/en/guide/advance/in-depth-about-dev-command.md +1 -1
- package/docs/en/guide/basic/before-getting-started.md +1 -1
- package/docs/en/guide/basic/modify-output-product.md +44 -70
- package/docs/en/guide/basic/test-your-project.mdx +0 -2
- package/docs/en/guide/basic/use-micro-generator.md +12 -37
- package/docs/en/guide/basic/using-storybook.mdx +15 -0
- package/docs/en/guide/best-practices/components.mdx +5 -198
- package/docs/en/guide/best-practices/use-tailwindcss.mdx +289 -0
- package/docs/en/plugins/guide/setup-function.mdx +0 -1
- package/docs/zh/api/config/build-config.mdx +19 -20
- package/docs/zh/api/config/build-preset.mdx +12 -69
- package/docs/zh/guide/advance/asset.mdx +3 -6
- package/docs/zh/guide/advance/copy.md +0 -2
- package/docs/zh/guide/advance/external-dependency.mdx +1 -1
- package/docs/zh/guide/advance/in-depth-about-build.md +30 -56
- package/docs/zh/guide/basic/before-getting-started.md +1 -1
- package/docs/zh/guide/basic/modify-output-product.md +46 -69
- package/docs/zh/guide/basic/use-micro-generator.md +13 -33
- package/docs/zh/guide/basic/using-storybook.mdx +16 -5
- package/docs/zh/guide/best-practices/components.mdx +1 -196
- package/docs/zh/guide/best-practices/use-tailwindcss.mdx +289 -0
- package/docs/zh/plugins/guide/setup-function.mdx +0 -1
- package/package.json +7 -6
- package/rspress.config.ts +130 -0
- package/theme/index.ts +3 -2
- package/tsconfig.json +1 -1
- package/docs/en/api/config/design-system.md +0 -1166
- package/docs/en/guide/advance/theme-config.mdx +0 -60
- package/docs/zh/api/config/design-system.md +0 -1166
- package/docs/zh/guide/advance/theme-config.mdx +0 -64
- package/modern.config.ts +0 -124
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_position: 2
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Using Tailwind CSS
|
|
6
|
+
|
|
7
|
+
[Tailwind CSS](https://tailwindcss.com/) is a CSS framework and design system based on Utility Class, which can quickly add common styles to components, and support flexible extension of theme styles.
|
|
8
|
+
|
|
9
|
+
Module Tools supports developing component styles using Tailwind CSS.
|
|
10
|
+
|
|
11
|
+
## Enabling Tailwind CSS
|
|
12
|
+
|
|
13
|
+
By default, Module Tools does not have this feature enabled. You can enable it by following the steps below.
|
|
14
|
+
|
|
15
|
+
1. Execute the `new` command in the project root directory to enable Tailwind CSS.
|
|
16
|
+
|
|
17
|
+
```text title="Terminal"
|
|
18
|
+
pnpm run new
|
|
19
|
+
|
|
20
|
+
? Please select the operation you want: Enable features
|
|
21
|
+
? Please select the feature name: Enable Tailwind CSS
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
2. After successful enabling, you will see a new dependency added to `package.json`.
|
|
25
|
+
|
|
26
|
+
```json title="./package.json"
|
|
27
|
+
{
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@modern-js/plugin-tailwindcss": "x.y.z"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
3. Finally, you need to manually register the `tailwindcssPlugin` in the configuration file:
|
|
35
|
+
|
|
36
|
+
```ts title="modern.config.ts"
|
|
37
|
+
import { moduleTools, defineConfig } from '@modern-js/module-tools';
|
|
38
|
+
import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
|
|
39
|
+
|
|
40
|
+
export default defineConfig({
|
|
41
|
+
plugins: [moduleTools(), tailwindPlugin()],
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Configuring Tailwind CSS
|
|
46
|
+
|
|
47
|
+
In Module Tools, you have two ways to configure Tailwind CSS:
|
|
48
|
+
|
|
49
|
+
1. Using the `tailwind.config.{ts,js}` file, which follows the official usage of Tailwind CSS. Please refer to ["Tailwind CSS - Configuration"](https://tailwindcss.com/docs/configuration) for more details.
|
|
50
|
+
|
|
51
|
+
```ts title="tailwind.config.ts"
|
|
52
|
+
import type { Config } from 'tailwindcss';
|
|
53
|
+
|
|
54
|
+
export default {
|
|
55
|
+
content: ['./src/**/*.{js,jsx,ts,tsx}'],
|
|
56
|
+
} as Config;
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
:::tip
|
|
60
|
+
Please upgrade Modern.js to version >= MAJOR_VERSION.33.0 to support automatic reading of `tailwind.config.{ts,js}` files.
|
|
61
|
+
:::
|
|
62
|
+
|
|
63
|
+
2. Using the [style.tailwindcss](/api/config/build-config.html#styletailwindcss) configuration option. This is the old way of configuring Tailwind CSS, and we recommend using the `tailwind.config.{ts,js}` file for configuration.
|
|
64
|
+
|
|
65
|
+
```ts title="modern.config.ts"
|
|
66
|
+
export default {
|
|
67
|
+
tools: {
|
|
68
|
+
tailwindcss: {
|
|
69
|
+
content: ['./src/**/*.{js,jsx,ts,tsx}'],
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
If you are using both the `tailwind.config.{ts,js}` file and `style.tailwindcss` option, the configuration defined in `style.tailwindcss` will take precedence and override the content defined in `tailwind.config.{ts,js}`.
|
|
76
|
+
|
|
77
|
+
### Tailwind CSS Autocomplete
|
|
78
|
+
|
|
79
|
+
Tailwind CSS provides an official extension called [Tailwind CSS IntelliSense](https://github.com/tailwindlabs/tailwindcss-intellisense) for autocompletion of Tailwind CSS class names, CSS functions, and directives in VS Code.
|
|
80
|
+
|
|
81
|
+
You can follow the steps below to enable the autocomplete feature:
|
|
82
|
+
|
|
83
|
+
1. Install the [Tailwind CSS IntelliSense](https://github.com/tailwindlabs/tailwindcss-intellisense) extension in VS Code.
|
|
84
|
+
2. If the root directory of your project does not have a `tailwind.config.{ts,js}` file, you need to create one and write the Tailwind CSS configuration for your current project. Otherwise, the IDE plugin will not work correctly.
|
|
85
|
+
|
|
86
|
+
## Usage Introduction
|
|
87
|
+
|
|
88
|
+
Tailwind CSS provides two ways of usage:
|
|
89
|
+
|
|
90
|
+
### HTML Class Names
|
|
91
|
+
|
|
92
|
+
Tailwind CSS supports adding styles by using class names on HTML tags. **When using HTML class names, make sure to import the corresponding CSS file of Tailwind CSS.**
|
|
93
|
+
|
|
94
|
+
```tsx title="./src/index.tsx"
|
|
95
|
+
import 'tailwindcss/utilities.css';
|
|
96
|
+
|
|
97
|
+
export default () => {
|
|
98
|
+
return <div className="bg-black text-white">hello world</div>;
|
|
99
|
+
};
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Generated styles (after bundling):
|
|
103
|
+
|
|
104
|
+
```css title="./dist/es/index.css"
|
|
105
|
+
/* node_modules/tailwindcss/utilities.css */
|
|
106
|
+
.table {
|
|
107
|
+
display: table;
|
|
108
|
+
}
|
|
109
|
+
.bg-black {
|
|
110
|
+
--tw-bg-opacity: 1;
|
|
111
|
+
background-color: rgba(0, 0, 0, var(--tw-bg-opacity));
|
|
112
|
+
}
|
|
113
|
+
.text-white {
|
|
114
|
+
--tw-text-opacity: 1;
|
|
115
|
+
color: rgba(255, 255, 255, var(--tw-text-opacity));
|
|
116
|
+
}
|
|
117
|
+
/** some more... */
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### `@apply`
|
|
121
|
+
|
|
122
|
+
Tailwind CSS provides the [`@apply`](https://v2.tailwindcss.com/docs/functions-and-directives#apply) directive, which allows us to inline the styles provided by Tailwind CSS into our own styles.
|
|
123
|
+
|
|
124
|
+
`@apply` can be used in CSS, Less, and Sass.
|
|
125
|
+
|
|
126
|
+
```css
|
|
127
|
+
.btn {
|
|
128
|
+
@apply font-bold py-2 px-4 rounded;
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
However, there are some considerations when using it with Less and Sass:
|
|
133
|
+
|
|
134
|
+
#### Sass
|
|
135
|
+
|
|
136
|
+
When using Tailwind with Sass and there is an `!important` after `@apply`, interpolation should be used to ensure Sass compiles correctly.
|
|
137
|
+
|
|
138
|
+
- Won't work as expected:
|
|
139
|
+
|
|
140
|
+
```sass
|
|
141
|
+
.alert {
|
|
142
|
+
@apply bg-red-500 !important;
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
- Will work as expected:
|
|
147
|
+
|
|
148
|
+
```sass
|
|
149
|
+
.alert {
|
|
150
|
+
@apply bg-red-500 #{!important};
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
#### Less
|
|
155
|
+
|
|
156
|
+
When using Tailwind with Less, you cannot nest Tailwind's `@screen` directive.
|
|
157
|
+
|
|
158
|
+
- Won't work as expected:
|
|
159
|
+
|
|
160
|
+
```less
|
|
161
|
+
.card {
|
|
162
|
+
@apply rounded-none;
|
|
163
|
+
|
|
164
|
+
@screen sm {
|
|
165
|
+
@apply rounded-lg;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
- Instead, use regular media queries and the `theme()` function to reference your screen sizes or simply avoid nesting your `@screen` directive.
|
|
171
|
+
|
|
172
|
+
```less title="Method 1"
|
|
173
|
+
// Use a regular media query and theme()
|
|
174
|
+
.card {
|
|
175
|
+
@apply rounded-none;
|
|
176
|
+
|
|
177
|
+
@media (min-width: theme('screens.sm')) {
|
|
178
|
+
@apply rounded-lg;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
```less title="Method 2"
|
|
184
|
+
// Use the @screen directive at the top-level
|
|
185
|
+
.card {
|
|
186
|
+
@apply rounded-none;
|
|
187
|
+
|
|
188
|
+
@media (min-width: theme('screens.sm')) {
|
|
189
|
+
@apply rounded-lg;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Recommended Approach
|
|
195
|
+
|
|
196
|
+
**It is recommended to develop styles using the `@apply` directive so that the resulting styles only include the inline styles specified by the directive.**
|
|
197
|
+
|
|
198
|
+
When adding styles using HTML class names, by default, Tailwind includes not only the styles corresponding to the class names but also additional style code. Although these additional code may not affect the styles themselves.
|
|
199
|
+
|
|
200
|
+
### Difference between Bundle and Bundleless Builds
|
|
201
|
+
|
|
202
|
+
For the following code, there is a significant difference in the build output between bundle and bundleless modes.
|
|
203
|
+
|
|
204
|
+
> For more information about bundle and bundleless, refer to [Bundle and Bundleless](/guide/advance/in-depth-about-build#bundle--bundleless)
|
|
205
|
+
|
|
206
|
+
```tsx title="./src/index.tsx"
|
|
207
|
+
import 'tailwindcss/utilities.css';
|
|
208
|
+
|
|
209
|
+
export default () => {
|
|
210
|
+
return <div className="bg-black text-white">hello world11</div>;
|
|
211
|
+
};
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
In the Bundle mode, third-party dependencies are bundled together.
|
|
215
|
+
|
|
216
|
+
For styles, a separate output file is generated, and there is no import code related to styles in the JS output file.
|
|
217
|
+
|
|
218
|
+
To inject styles into the JS output, you can enable the [`style.inject`](/api/config/build-config#styleinject) option.
|
|
219
|
+
|
|
220
|
+
```css title="./dist/es/index.css"
|
|
221
|
+
/* node_modules/tailwindcss/utilities.css */
|
|
222
|
+
.table {
|
|
223
|
+
display: table;
|
|
224
|
+
}
|
|
225
|
+
.bg-black {
|
|
226
|
+
--tw-bg-opacity: 1;
|
|
227
|
+
background-color: rgba(0, 0, 0, var(--tw-bg-opacity));
|
|
228
|
+
}
|
|
229
|
+
.text-white {
|
|
230
|
+
--tw-text-opacity: 1;
|
|
231
|
+
color: rgba(255, 255, 255, var(--tw-text-opacity));
|
|
232
|
+
}
|
|
233
|
+
/** some more... */
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
```js title="./dist/es/index.js"
|
|
237
|
+
// src/index.tsx
|
|
238
|
+
import { jsx } from 'react/jsx-runtime';
|
|
239
|
+
var src_default = () => {
|
|
240
|
+
return /* @__PURE__ */ jsx('div', {
|
|
241
|
+
className: 'bg-black text-white',
|
|
242
|
+
children: 'hello world11',
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
export { src_default as default };
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
In Bundleless mode, third-party dependencies are not bundled together, and no style artifacts will be generated.
|
|
249
|
+
|
|
250
|
+
```js title="./dist/es/index.js"
|
|
251
|
+
import { jsx } from 'react/jsx-runtime';
|
|
252
|
+
import 'tailwindcss/utilities.css';
|
|
253
|
+
var src_default = () => {
|
|
254
|
+
return /* @__PURE__ */ jsx('div', {
|
|
255
|
+
className: 'bg-black text-white',
|
|
256
|
+
children: 'hello world11',
|
|
257
|
+
});
|
|
258
|
+
};
|
|
259
|
+
export { src_default as default };
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### About `designSystem` config
|
|
263
|
+
|
|
264
|
+
`designSystem` is a deprecated configuration option in Module Tools.
|
|
265
|
+
|
|
266
|
+
Starting from Module Tools vMAJOR_VERSION.33.0, you can use the `theme` configuration option of Tailwind CSS as a replacement for `designSystem`. It is no longer necessary to split the `theme` configuration and set it on `designSystem`.
|
|
267
|
+
|
|
268
|
+
- Previous usage:
|
|
269
|
+
|
|
270
|
+
```ts title="modern.config.ts"
|
|
271
|
+
const { theme, ...rest } = tailwindConfig;
|
|
272
|
+
|
|
273
|
+
export default {
|
|
274
|
+
style: {
|
|
275
|
+
tailwindcss: rest,
|
|
276
|
+
},
|
|
277
|
+
designSystem: theme,
|
|
278
|
+
};
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
- Current usage:
|
|
282
|
+
|
|
283
|
+
```ts title="modern.config.ts"
|
|
284
|
+
export default {
|
|
285
|
+
style: {
|
|
286
|
+
tailwindcss: tailwindConfig,
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
```
|
|
@@ -7,7 +7,6 @@ sidebar_position: 1
|
|
|
7
7
|
`buildConfig` 是一个用来描述如何编译、生成构建产物的配置项,它包含了构建的所有配置。
|
|
8
8
|
|
|
9
9
|
- 类型:`object | object[]`
|
|
10
|
-
- 默认值: `undefined`
|
|
11
10
|
|
|
12
11
|
:::tip
|
|
13
12
|
在开始使用 `buildConfig` 之前,请先阅读以下文档来了解其作用:
|
|
@@ -521,7 +520,7 @@ export default defineConfig({
|
|
|
521
520
|
- 类型:`'esm' | 'cjs' | 'iife' | 'umd'`
|
|
522
521
|
- 默认值:`cjs`
|
|
523
522
|
|
|
524
|
-
### format:
|
|
523
|
+
### format: esm
|
|
525
524
|
|
|
526
525
|
esm 代表 "ECMAScript 模块",它需要运行环境支持 import 和 export 语法。
|
|
527
526
|
|
|
@@ -535,7 +534,7 @@ export default defineConfig({
|
|
|
535
534
|
});
|
|
536
535
|
```
|
|
537
536
|
|
|
538
|
-
### format:
|
|
537
|
+
### format: cjs
|
|
539
538
|
|
|
540
539
|
cjs 代表 "CommonJS",它需要运行环境支持 exports、require 和 module 语法,通常为 Node.js 环境。
|
|
541
540
|
|
|
@@ -549,7 +548,7 @@ export default defineConfig({
|
|
|
549
548
|
});
|
|
550
549
|
```
|
|
551
550
|
|
|
552
|
-
### format:
|
|
551
|
+
### format: iife
|
|
553
552
|
|
|
554
553
|
iife 代表 "立即调用函数表达式",它将代码包裹在函数表达式中,确保代码中的任何变量不会意外地与全局范围中的变量冲突,通常在浏览器环境中运行。
|
|
555
554
|
|
|
@@ -563,7 +562,7 @@ export default defineConfig({
|
|
|
563
562
|
});
|
|
564
563
|
```
|
|
565
564
|
|
|
566
|
-
### format:
|
|
565
|
+
### format: umd
|
|
567
566
|
|
|
568
567
|
umd 代表 "Universal Module Definition",用于在不同环境(浏览器、Node.js 等)中运行。umd 格式的模块可以在多种环境下使用,既可以作为全局变量访问,也可以通过模块加载器(如 RequireJS)进行模块化加载。
|
|
569
568
|
|
|
@@ -1086,13 +1085,10 @@ export default defineConfig({
|
|
|
1086
1085
|
|
|
1087
1086
|
## style.tailwindcss
|
|
1088
1087
|
|
|
1089
|
-
Tailwind CSS
|
|
1088
|
+
用于修改 [Tailwind CSS](https://tailwindcss.com/docs/configuration) 的配置项。
|
|
1090
1089
|
|
|
1091
1090
|
- 类型: `object | Function`
|
|
1092
|
-
- 默认值:
|
|
1093
|
-
|
|
1094
|
-
<details>
|
|
1095
|
-
<summary>Tailwind CSS 配置详情</summary>
|
|
1091
|
+
- 默认值:
|
|
1096
1092
|
|
|
1097
1093
|
```js title="modern.config.ts"
|
|
1098
1094
|
const tailwind = {
|
|
@@ -1104,7 +1100,13 @@ const tailwind = {
|
|
|
1104
1100
|
};
|
|
1105
1101
|
```
|
|
1106
1102
|
|
|
1107
|
-
|
|
1103
|
+
### 启用 Tailwind CSS
|
|
1104
|
+
|
|
1105
|
+
在使用 `style.tailwindcss` 之前,你需要启用 Module Tools 的 Tailwind CSS 插件。
|
|
1106
|
+
|
|
1107
|
+
请阅读[「使用 Tailwind CSS」](/guide/best-practices/use-tailwindcss.html) 章节来了解开启方式。
|
|
1108
|
+
|
|
1109
|
+
### 类型
|
|
1108
1110
|
|
|
1109
1111
|
值为 `object` 类型时,与默认配置通过 `Object.assign` 合并。
|
|
1110
1112
|
|
|
@@ -1114,7 +1116,10 @@ const tailwind = {
|
|
|
1114
1116
|
|
|
1115
1117
|
### 注意事项
|
|
1116
1118
|
|
|
1117
|
-
|
|
1119
|
+
注意:
|
|
1120
|
+
|
|
1121
|
+
- 如果你同时使用了 `tailwind.config.{ts,js}` 文件和 `tools.tailwindcss` 选项,那么 `tools.tailwindcss` 定义的配置会优先生效,并覆盖 `tailwind.config.{ts,js}` 中定义的内容。
|
|
1122
|
+
- 如果你同时使用了 `designSystem` 配置项,那么 Tailwind CSS 的 `theme` 配置将会被 `designSystem` 的值所覆盖。
|
|
1118
1123
|
|
|
1119
1124
|
其他配置的使用方式与 Tailwind CSS 官方用法一致,请参考 [tailwindcss - Configuration](https://tailwindcss.com/docs/configuration)。
|
|
1120
1125
|
|
|
@@ -1176,8 +1181,6 @@ export default defineConfig({
|
|
|
1176
1181
|
});
|
|
1177
1182
|
```
|
|
1178
1183
|
|
|
1179
|
-
### 注意事项
|
|
1180
|
-
|
|
1181
1184
|
参考[「Import 插件——注意事项」](plugins/official-list/plugin-import.html#注意事项)
|
|
1182
1185
|
|
|
1183
1186
|
## transformLodash
|
|
@@ -1187,13 +1190,11 @@ export default defineConfig({
|
|
|
1187
1190
|
- 类型:`boolean`
|
|
1188
1191
|
- 默认值:`true`
|
|
1189
1192
|
|
|
1190
|
-
### 示例
|
|
1191
|
-
|
|
1192
1193
|
该选项默认开启,Module Tools 会自动将 lodash 的代码引用指向子路径。
|
|
1193
1194
|
|
|
1194
1195
|
比如:
|
|
1195
1196
|
|
|
1196
|
-
```
|
|
1197
|
+
```ts title="input.js"
|
|
1197
1198
|
import _ from 'lodash';
|
|
1198
1199
|
import { add } from 'lodash/fp';
|
|
1199
1200
|
|
|
@@ -1211,9 +1212,7 @@ const addOne = _add(1);
|
|
|
1211
1212
|
_map([1, 2, 3], addOne);
|
|
1212
1213
|
```
|
|
1213
1214
|
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
在个别情况下,lodash 的 import 转换可能会生成不符合预期的代码,此时你可以手动关闭这项优化:
|
|
1215
|
+
然而有时候 lodash 的 import 转换可能会生成不符合预期的代码,此时你可以手动关闭这项优化:
|
|
1217
1216
|
|
|
1218
1217
|
```js title="modern.config.ts"
|
|
1219
1218
|
export default defineConfig({
|
|
@@ -9,19 +9,8 @@ sidebar_position: 2
|
|
|
9
9
|
- 类型:`string | Function`
|
|
10
10
|
- 默认值: `undefined`
|
|
11
11
|
|
|
12
|
-
## string
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
```js title="modern.config.ts"
|
|
17
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
18
|
-
|
|
19
|
-
export default defineConfig({
|
|
20
|
-
buildPreset: 'npm-library',
|
|
21
|
-
});
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
### `'npm-library'`
|
|
13
|
+
## `npm-library`
|
|
25
14
|
|
|
26
15
|
在类 [NPM](https://www.npmjs.com/) 包管理器下使用的 Library 通用模式,包含 `esm` 和 `cjs` 两种 Bundle 产物,并且包含一份类型文件。
|
|
27
16
|
|
|
@@ -68,7 +57,7 @@ export const buildConfig = [
|
|
|
68
57
|
];
|
|
69
58
|
```
|
|
70
59
|
|
|
71
|
-
|
|
60
|
+
## `npm-library-with-umd`
|
|
72
61
|
|
|
73
62
|
在类 [NPM](https://www.npmjs.com/) 包管理器下使用,并且 Library 支持类似 [unpkg](https://unpkg.com/) 的模式。在预设 `'npm-library'` 的基础上,额外提供 `umd` 产物。
|
|
74
63
|
|
|
@@ -114,7 +103,7 @@ export const buildConfig = [
|
|
|
114
103
|
];
|
|
115
104
|
```
|
|
116
105
|
|
|
117
|
-
|
|
106
|
+
## `npm-component`
|
|
118
107
|
|
|
119
108
|
在类 [NPM](https://www.npmjs.com/) 包管理器下使用的 组件(库)通用模式。包含 `esm` 和 `cjs` 两种 Bundleless 产物(便于 _[Tree shaking](https://developer.mozilla.org/zh-CN/docs/Glossary/Tree_shaking)_ 优化),以及包含一份类型文件。
|
|
120
109
|
|
|
@@ -154,7 +143,7 @@ export const buildConfig = [
|
|
|
154
143
|
];
|
|
155
144
|
```
|
|
156
145
|
|
|
157
|
-
|
|
146
|
+
## `npm-component-with-umd`
|
|
158
147
|
|
|
159
148
|
在类 [NPM](https://www.npmjs.com/) 包管理器下使用的组件(库),同时支持类 [unpkg](https://unpkg.com/) 的模式。 在预设 `'npm-component'` 的基础上,额外提供 `umd` 产物。
|
|
160
149
|
|
|
@@ -198,7 +187,7 @@ export const buildConfig = [
|
|
|
198
187
|
];
|
|
199
188
|
```
|
|
200
189
|
|
|
201
|
-
|
|
190
|
+
## `npm-library-{es5...esnext}`
|
|
202
191
|
|
|
203
192
|
当想要使用支持其他 ECMAScript 版本的 `buildPreset` 预设的时候,可以直接在 `'npm-library'`、`'npm-library-with-umd'`、`'npm-component'`、`'npm-component-with-umd'` 这些预设值后面增加想要支持的版本。
|
|
204
193
|
|
|
@@ -212,10 +201,11 @@ export default defineConfig({
|
|
|
212
201
|
});
|
|
213
202
|
```
|
|
214
203
|
|
|
215
|
-
## Function
|
|
204
|
+
## string / Function
|
|
216
205
|
|
|
217
|
-
|
|
218
|
-
|
|
206
|
+
buildPreset 除了支持基本的字符串形式,还支持函数形式,可以通过 `preset` 或者 `extendPreset` 参数获取我们提供的预设值,然后进行修改。
|
|
207
|
+
|
|
208
|
+
以下是两个分别使用 `preset` 和 `extendPreset` 的例子:
|
|
219
209
|
|
|
220
210
|
```js title="modern.config.ts"
|
|
221
211
|
import { defineConfig } from '@modern-js/module-tools';
|
|
@@ -224,10 +214,8 @@ export default defineConfig({
|
|
|
224
214
|
buildPreset({ preset }) {
|
|
225
215
|
const { NPM_LIBRARY } = preset;
|
|
226
216
|
return NPM_LIBRARY.map(config => {
|
|
227
|
-
config.
|
|
228
|
-
|
|
229
|
-
drop_console: true,
|
|
230
|
-
},
|
|
217
|
+
config.define = {
|
|
218
|
+
VERSION: '1.0.1',
|
|
231
219
|
};
|
|
232
220
|
return config;
|
|
233
221
|
});
|
|
@@ -235,52 +223,7 @@ export default defineConfig({
|
|
|
235
223
|
});
|
|
236
224
|
```
|
|
237
225
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
`buildPreset` 的函数形式包含了一个对象形式的函数参数。该对象包含以下字段:
|
|
241
|
-
|
|
242
|
-
* `preset`
|
|
243
|
-
* `extendPreset`
|
|
244
|
-
|
|
245
|
-
#### `preset`
|
|
246
|
-
|
|
247
|
-
类型:**Object**
|
|
248
|
-
|
|
249
|
-
包含了所有可用的构建预设对应的构建配置。可以通过 `buildPreset` 所支持的字符串来使用构建配置,也可以使用这些字符串的下划线命令的方式。下面是两种方式的使用示例:
|
|
250
|
-
|
|
251
|
-
- 使用 `buildPreset` 所支持的字符串。
|
|
252
|
-
|
|
253
|
-
```ts title="modern.config.ts"
|
|
254
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
255
|
-
|
|
256
|
-
export default defineConfig({
|
|
257
|
-
buildPreset({ preset }) {
|
|
258
|
-
return preset['npm-library'];
|
|
259
|
-
},
|
|
260
|
-
});
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
- 使用 `buildPreset` 所支持的字符串的下划线命名方式。
|
|
264
|
-
|
|
265
|
-
```ts title="modern.config.ts"
|
|
266
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
267
|
-
|
|
268
|
-
export default defineConfig({
|
|
269
|
-
buildPreset({ preset }) {
|
|
270
|
-
return preset.NPM_LIBRARY;
|
|
271
|
-
},
|
|
272
|
-
});
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
#### `extendPreset`
|
|
276
|
-
|
|
277
|
-
类型:`Function`
|
|
278
|
-
|
|
279
|
-
用于扩展某个 `buildPreset` 的工具函数,可以修改 `buildPreset` 对应的构建配置。
|
|
280
|
-
|
|
281
|
-
> 底层使用类似 `{...oldBuildConfig, ...extendBuildConfig}` 方式进行处理。
|
|
282
|
-
|
|
283
|
-
例如在 `'npm-library'` 构建预设的基础上增加 `define` 配置:
|
|
226
|
+
`extendPreset` 里会使用 lodash.merge 进行配置合并
|
|
284
227
|
|
|
285
228
|
```ts title="modern.config.ts"
|
|
286
229
|
import { defineConfig } from '@modern-js/module-tools';
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
sidebar_position: 7
|
|
3
3
|
---
|
|
4
4
|
|
|
5
|
-
#
|
|
5
|
+
# 处理静态资源
|
|
6
6
|
|
|
7
7
|
模块工程会对代码中使用的静态资源进行处理。如果需要配置,则可以使用 [`buildConfig.asset`](/api/config/build-config#asset) API。
|
|
8
8
|
|
|
@@ -16,10 +16,10 @@ sidebar_position: 7
|
|
|
16
16
|
|
|
17
17
|
对于静态文件的处理,模块工程目前默认支持的功能有:
|
|
18
18
|
|
|
19
|
-
-
|
|
19
|
+
- 输出静态资源至 `./assets`。
|
|
20
20
|
- 对于不超过 **10kb** 的文件会内联到代码中。
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
## 示例
|
|
23
23
|
|
|
24
24
|
让我们看下面的例子:
|
|
25
25
|
|
|
@@ -27,7 +27,6 @@ sidebar_position: 7
|
|
|
27
27
|
|
|
28
28
|
```ts title="./src/asset.ts"
|
|
29
29
|
import img from './bg.png';
|
|
30
|
-
console.log(img);
|
|
31
30
|
```
|
|
32
31
|
|
|
33
32
|
- 如果 `bg.png` 的大小小于 10 kb,则此时产物目录结构和产物内容为:
|
|
@@ -39,7 +38,6 @@ console.log(img);
|
|
|
39
38
|
|
|
40
39
|
```js title="./dist/asset.js"
|
|
41
40
|
var img_default = 'data:image/png;base64,';
|
|
42
|
-
console.info(img_default);
|
|
43
41
|
```
|
|
44
42
|
|
|
45
43
|
- 如果 `bg.png` 的大小大于 10 kb,则此时产物目录结构和产物内容为:
|
|
@@ -53,7 +51,6 @@ console.info(img_default);
|
|
|
53
51
|
|
|
54
52
|
```js title="./dist/asset.js"
|
|
55
53
|
import img from './assets/bg.13e2aba2.png';
|
|
56
|
-
console.info(img);
|
|
57
54
|
```
|
|
58
55
|
|
|
59
56
|
当你想要修改默认行为的时候,可以使用以下 API:
|