@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
|
@@ -10,62 +10,41 @@ sidebar_position: 1
|
|
|
10
10
|
如果你还不了解 `buildConfig` 的作用,请先阅读 [修改输出产物](/guide/basic/modify-output-product)。
|
|
11
11
|
:::
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
而在本章里我们将要深入理解某些构建配置的作用以及了解执行 `modern build` 命令的时候发生了什么。
|
|
14
14
|
|
|
15
|
-
##
|
|
15
|
+
## buildConfig
|
|
16
16
|
|
|
17
|
-
###
|
|
17
|
+
### `bundle` / `bundleless`
|
|
18
18
|
|
|
19
|
-
那么首先我们来了解一下
|
|
19
|
+
那么首先我们来了解一下 bundle 和 bundleless。
|
|
20
20
|
|
|
21
|
-
所谓
|
|
21
|
+
所谓 bundle 是指对构建产物进行打包,构建产物可能是一个文件,也有可能是基于一定的[代码拆分策略](https://esbuild.github.io/api/#splitting)得到的多个文件。
|
|
22
22
|
|
|
23
|
-
而
|
|
23
|
+
而 bundleless 则是指对每个源文件单独进行编译构建,但是并不将它们打包在一起。每一个产物文件都可以找到与之相对应的源码文件。**bundleless 构建的过程,也可以理解为仅对源文件进行代码转换的过程**。
|
|
24
24
|
|
|
25
25
|
它们有各自的好处:
|
|
26
26
|
|
|
27
|
-
-
|
|
28
|
-
-
|
|
27
|
+
- bundle 可以减少构建产物的体积,也可以对依赖预打包,减小安装依赖的体积。提前对库进行打包,可以加快应用项目构建的速度。
|
|
28
|
+
- bundleless 则是可以保持原有的文件结构,更有利于调试和 tree shaking。
|
|
29
29
|
|
|
30
30
|
:::warning
|
|
31
|
-
|
|
31
|
+
bundleless 是单文件编译模式,因此对于类型的引用和导出你需要加上 `type` 字段, 例如 `import type { A } from './types`
|
|
32
32
|
:::
|
|
33
33
|
|
|
34
|
-
在 `buildConfig` 中可以通过 [`buildConfig.buildType`](/api/config/build-config#buildtype) 来指定当前构建任务是
|
|
34
|
+
在 `buildConfig` 中可以通过 [`buildConfig.buildType`](/api/config/build-config#buildtype) 来指定当前构建任务是 bundle 还是 bundleless。
|
|
35
35
|
|
|
36
|
-
### `input`
|
|
36
|
+
### `input` / `sourceDir`
|
|
37
37
|
|
|
38
|
-
[`buildConfig.input`](/api/config/build-config#input) 用于指定读取源码的文件路径或者目录路径,其默认值在
|
|
38
|
+
[`buildConfig.input`](/api/config/build-config#input) 用于指定读取源码的文件路径或者目录路径,其默认值在 bundle 和 bundleless 构建过程中有所不同:
|
|
39
39
|
|
|
40
40
|
- 当 `buildType: 'bundle'` 的时候,`input` 默认值为 `src/index.(j|t)sx?`
|
|
41
41
|
- 当 `buildType: 'bundleless'` 的时候,`input` 默认值为 `['src']`
|
|
42
42
|
|
|
43
43
|
:::warning
|
|
44
|
-
建议不要在
|
|
44
|
+
建议不要在 bundleless 构建过程中指定多个源码文件目录,这可能会导致产物里的相对路径不正确。
|
|
45
45
|
:::
|
|
46
46
|
|
|
47
|
-
从默认值上我们可以知道:**
|
|
48
|
-
|
|
49
|
-
#### `input` 的对象模式
|
|
50
|
-
|
|
51
|
-
在 Bundle 构建过程中,除了将 `input` 设置为一个数组,也可以将它设置为一个对象。**通过使用对象的形式,我们可以修改构建产物输出的文件名称**。那么对于下面的例子,`./src/index.ts` 对应的构建产物文件路径为 `./dist/main.js`。
|
|
52
|
-
|
|
53
|
-
```js title="modern.config.ts"
|
|
54
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
55
|
-
|
|
56
|
-
export default defineConfig({
|
|
57
|
-
buildConfig: {
|
|
58
|
-
input: {
|
|
59
|
-
main: ['./src/index.ts'],
|
|
60
|
-
},
|
|
61
|
-
outDir: './dist',
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
而在 Bundleless 构建过程中,虽然同样支持这样的使用方式,但是并不推荐。
|
|
67
|
-
|
|
68
|
-
#### `sourceDir`
|
|
47
|
+
从默认值上我们可以知道:**bundle 构建一般可以指定文件路径作为构建的入口,而 bundleless 构建则更期望使用目录路径寻找源文件**。
|
|
69
48
|
|
|
70
49
|
[`sourceDir`](/api/config/build-config#sourcedir) 用于指定源码目录,它主要与以下两个内容有关系:
|
|
71
50
|
|
|
@@ -74,10 +53,10 @@ export default defineConfig({
|
|
|
74
53
|
|
|
75
54
|
一般来说:
|
|
76
55
|
|
|
77
|
-
- **在
|
|
78
|
-
- 在
|
|
56
|
+
- **在 bundleless 构建过程中,`sourceDir` 与 `input` 的值要保持一致,它们的默认值都是 `src`**。
|
|
57
|
+
- 在 bundle 构建过程中,无需使用 `sourceDir`。
|
|
79
58
|
|
|
80
|
-
###
|
|
59
|
+
### dts
|
|
81
60
|
|
|
82
61
|
[`buildConfig.dts`](/api/config/build-config#dts) 配置主要用于类型文件的生成。
|
|
83
62
|
|
|
@@ -110,7 +89,7 @@ export default defineConfig({
|
|
|
110
89
|
|
|
111
90
|
#### 别名转换
|
|
112
91
|
|
|
113
|
-
在
|
|
92
|
+
在 bundleless 构建过程中,如果源代码中出现了别名,例如:
|
|
114
93
|
|
|
115
94
|
```js title="./src/index.ts"
|
|
116
95
|
import utils from '@common/utils';
|
|
@@ -121,19 +100,16 @@ import utils from '@common/utils';
|
|
|
121
100
|
- 对于类似 `import '@common/utils'` 或者 `import utils from '@common/utils'` 这样形式的代码可以进行别名转换。
|
|
122
101
|
- 对于类似 `export { utils } from '@common/utils'` 这样形式的代码可以进行别名转换。
|
|
123
102
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
- 对于类似 `Promise<import('@common/utils')>` 这样形式的输出类型目前无法进行转换。
|
|
103
|
+
然而也存在一些情况,目前还无法处理,例如 `Promise<import('@common/utils')>` 这样形式的输出类型目前无法进行转换。
|
|
104
|
+
对于这种情况的解决办法,可以参与[讨论](https://github.com/web-infra-dev/modern.js/discussions/4511)。
|
|
127
105
|
|
|
128
|
-
####
|
|
129
|
-
|
|
130
|
-
一般使用方式:
|
|
106
|
+
#### 一些示例
|
|
131
107
|
|
|
132
108
|
```js
|
|
133
109
|
import { defineConfig } from '@modern-js/module-tools';
|
|
134
110
|
|
|
135
111
|
export default defineConfig({
|
|
136
|
-
// 此时打包的类型文件输出路径为 `./dist/types
|
|
112
|
+
// 此时打包的类型文件输出路径为 `./dist/types`,并且将会读取项目下的 other-tsconfig.json 文件
|
|
137
113
|
buildConfig: {
|
|
138
114
|
buildType: 'bundle',
|
|
139
115
|
dts: {
|
|
@@ -145,8 +121,6 @@ export default defineConfig({
|
|
|
145
121
|
});
|
|
146
122
|
```
|
|
147
123
|
|
|
148
|
-
使用 `dts.only` 的情况:
|
|
149
|
-
|
|
150
124
|
```js
|
|
151
125
|
import { defineConfig } from '@modern-js/module-tools';
|
|
152
126
|
|
|
@@ -169,7 +143,7 @@ export default defineConfig({
|
|
|
169
143
|
});
|
|
170
144
|
```
|
|
171
145
|
|
|
172
|
-
### `
|
|
146
|
+
### `define`
|
|
173
147
|
|
|
174
148
|
[`buildConfig.define`](/api/config/build-config#define) 功能有些类似 [`webpack.DefinePlugin`](https://webpack.js.org/plugins/define-plugin/)。这里介绍几个使用场景:
|
|
175
149
|
|
|
@@ -240,9 +214,9 @@ declare const YOUR_ADD_GLOBAL_VAR;
|
|
|
240
214
|
当执行 `modern build` 命令的时候,会发生
|
|
241
215
|
|
|
242
216
|
- 根据 `buildConfig.outDir` 清理产物目录。
|
|
243
|
-
- 编译 `js/ts` 源代码生成
|
|
244
|
-
- 使用 `tsc` 生成
|
|
245
|
-
- 处理
|
|
217
|
+
- 编译 `js/ts` 源代码生成 bundle / bundleless 的 JS 构建产物。
|
|
218
|
+
- 使用 `tsc` 生成 bundle / bundleless 的类型文件。
|
|
219
|
+
- 处理 `copy` 任务。
|
|
246
220
|
|
|
247
221
|
## 构建报错
|
|
248
222
|
|
|
@@ -272,7 +246,7 @@ bundle DTS failed:
|
|
|
272
246
|
|
|
273
247
|
对于 `js/ts` 构建错误,我们可以从报错信息中知道:
|
|
274
248
|
|
|
275
|
-
-
|
|
276
|
-
-
|
|
277
|
-
-
|
|
278
|
-
-
|
|
249
|
+
- 报错的 `buildType`
|
|
250
|
+
- 报错的 `format`
|
|
251
|
+
- 报错的 `target`
|
|
252
|
+
- 其他具体报错信息
|
|
@@ -90,7 +90,7 @@ npm Registry 是一个 [npm 包存储的地方](https://docs.npmjs.com/about-the
|
|
|
90
90
|
|
|
91
91
|
> 想要了解关于 webpack 如何做这件事,可以查看这个[链接](https://webpack.js.org/configuration/resolve/#resolvemainfields)。
|
|
92
92
|
|
|
93
|
-
###
|
|
93
|
+
### "scripts"
|
|
94
94
|
|
|
95
95
|
`package.json` 文件的 `"scripts"` 属性支持一些内置的脚本和 npm 预设的生命周期事件,以及任意的脚本。
|
|
96
96
|
|
|
@@ -8,7 +8,7 @@ sidebar_position: 3
|
|
|
8
8
|
|
|
9
9
|
当你在初始化的项目里使用 `modern build` 命令的时候,Module Tools 会根据当前配置内容,生成相应的构建产物。
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
模板创建的默认配置内容如下:
|
|
12
12
|
|
|
13
13
|
```ts title="modern.config.ts"
|
|
14
14
|
import { moduleTools, defineConfig } from '@modern-js/module-tools';
|
|
@@ -39,58 +39,17 @@ export default defineConfig({
|
|
|
39
39
|
|
|
40
40
|
`buildPreset` 代表着提前准备好的一组或者多组构建相关的配置,只需要使用 `buildPreset` 对应的预设值,就可以省去麻烦且复杂的配置工作,得到符合预期的产物。
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
Module Tools 主要内置了两套构建预设,包括:
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
- `npm-component`: 用于构建组件库。
|
|
45
|
+
- `npm-library`: 用于打包其他库类型的项目,如工具库。
|
|
45
46
|
|
|
46
|
-
|
|
47
|
+
同时,还提供一些变体,例如 `npm-library-with-umd` 和 `npm-library-es5`,顾名思义,分别对应带有 umd 产物和支持到 es5 语法的库预设。
|
|
48
|
+
详细配置可以查看其[API](/api/config/build-preset)。
|
|
47
49
|
|
|
48
|
-
|
|
50
|
+
除此之外,我们也可以完全自定义构建配置:
|
|
49
51
|
|
|
50
|
-
|
|
51
|
-
- 在 `dist/es` 目录下会得到代码格式为 `esm`、语法支持为 `es6` 且经过打包处理后的产物。
|
|
52
|
-
- 在 `dist/types` 目录下会得到类型文件。如果不是 TypeScript 项目,则没有该目录。
|
|
53
|
-
|
|
54
|
-
而预设字符串 `"npm-library"` 对应的变体则是在原本产物的基础上修改了**代码语法支持**这一特点,同时在字符串命名上也变为了 `"npm-library-[es5 | es2016...es2020 | esnext]"` 这样的形式。
|
|
55
|
-
|
|
56
|
-
例如,如果在预设字符串 `"npm-library"` 对应的输出产物基础上,让产物代码支持的语法变为 `es5` 的话,那么只需要将 `"npm-library"` 改变为 `"npm-library-es5"` 就可以了。
|
|
57
|
-
|
|
58
|
-
### 构建预设的函数形式
|
|
59
|
-
|
|
60
|
-
**除了字符串形式以外,构建预设的值也可以是函数形式,在函数中可以打印或者修改某个预设值对应的具体配置**。
|
|
61
|
-
|
|
62
|
-
例如,如果使用预设函数的形式达到预设字符串 `"npm-library-es5"` 同样的效果,可以按照如下的方式:
|
|
63
|
-
|
|
64
|
-
```ts title="modern.config.ts"
|
|
65
|
-
import { moduleTools, defineConfig } from '@modern-js/module-tools';
|
|
66
|
-
|
|
67
|
-
export default defineConfig({
|
|
68
|
-
plugins: [moduleTools()],
|
|
69
|
-
buildPreset({ preset }) {
|
|
70
|
-
return preset.NPM_LIBRARY.map(config => {
|
|
71
|
-
return { ...config, target: 'es5' };
|
|
72
|
-
});
|
|
73
|
-
},
|
|
74
|
-
});
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
在上面的代码实现中,`preset.NPM_LIBRARY` 与预设字符串 `"npm-library"` 是相对应的,它代表与 `"npm-library"` 等价的多个构建相关的配置。
|
|
78
|
-
|
|
79
|
-
我们通过 `map` 方法遍历了 `NPM_LIBRARY` 这个数组,在这个数组中包含了多个 `buildConfig` 对象。我们将原本的 `buildConfig` 对象进行了浅拷贝,并修改了浅拷贝后 `target` 的值,将它指定为 `es5`。
|
|
80
|
-
|
|
81
|
-
如果你想了解 `preset.NPM_LIBRARY` 具体包含的内容,可以通过 [BuildPreset API](/api/config/build-preset) 查看。
|
|
82
|
-
|
|
83
|
-
此外,在 `preset` 对象下,不仅包含了 `NPM_LIBRARY`,也包含了其他类似的常量。
|
|
84
|
-
|
|
85
|
-
:::tip
|
|
86
|
-
我们不仅可以使用 `preset.NPM_LIBRARY` 来获取 `"npm-library"` 对应的构建配置,也可以使用 `preset['npm-library']` 这样的方式。
|
|
87
|
-
:::
|
|
88
|
-
|
|
89
|
-
那么这里的 `buildConfig` 对象是什么呢?之前提到的构建产物特点又是根据什么呢?
|
|
90
|
-
|
|
91
|
-
接下来我们解释一下。
|
|
92
|
-
|
|
93
|
-
## 构建配置(对象)
|
|
52
|
+
## 构建配置
|
|
94
53
|
|
|
95
54
|
**`buildConfig` 是一个用来描述如何编译、生成构建产物的配置项**。在最开始提到的关于“_构建产物的特点_”,其实都是 `buildConfig` 所支持的属性。目前所支持的属性覆盖了大部分模块类型项目在构建产物时的需求,`buildConfig` 不仅包含一些产物所具备的属性,也包含了构建产物所需要的一些特性功能。接下来从分类的角度简单罗列一下:
|
|
96
55
|
|
|
@@ -127,36 +86,54 @@ export default defineConfig({
|
|
|
127
86
|
|
|
128
87
|
除了以上分类以外,关于这些 API 的常见问题和最佳实践可以通过下面的链接来了解:
|
|
129
88
|
|
|
130
|
-
- [
|
|
131
|
-
- [`input`
|
|
132
|
-
- [
|
|
133
|
-
- [
|
|
134
|
-
- [
|
|
135
|
-
- [
|
|
136
|
-
- [如何构建 umd
|
|
137
|
-
- [
|
|
89
|
+
- [关于 `bundle` 和 `bundleless`](/guide/advance/in-depth-about-build#bundle--bundleless)
|
|
90
|
+
- [关于 `input` 和 `sourceDir`](/guide/advance/in-depth-about-build#input--sourcedir)。
|
|
91
|
+
- [关于类型描述文件](/guide/advance/in-depth-about-build#dts)。
|
|
92
|
+
- [如何使用 define](/guide/advance/in-depth-about-build#define)
|
|
93
|
+
- [如何处理第三方依赖](/guide/advance/external-dependency)
|
|
94
|
+
- [如何使用拷贝](/guide/advance/copy)
|
|
95
|
+
- [如何构建 umd 产物](/guide/advance/build-umd)
|
|
96
|
+
- [如何使用静态资源](/guide/advance/asset)
|
|
97
|
+
|
|
98
|
+
## 结合配置与预设
|
|
99
|
+
|
|
100
|
+
了解 `buildPreset` 和 `buildConfig` 之后,我们可以将二者进行结合使用。
|
|
138
101
|
|
|
139
|
-
|
|
102
|
+
在实际项目中,我们可以先使用 `buildPreset` 来快速获取一套默认构建配置。如果需要自定义,可以使用 `buildConfig` 进行覆盖和扩展。
|
|
140
103
|
|
|
141
|
-
|
|
104
|
+
扩展的逻辑如下:
|
|
105
|
+
|
|
106
|
+
- 当 `buildConfig` 是数组时,会在原来的预设基础上添加新的配置项。
|
|
142
107
|
|
|
143
108
|
```ts title="modern.config.ts"
|
|
144
|
-
import { defineConfig } from '@modern-js/module-tools';
|
|
109
|
+
import { moduleTools, defineConfig } from '@modern-js/module-tools';
|
|
145
110
|
|
|
146
111
|
export default defineConfig({
|
|
147
|
-
|
|
148
|
-
format: 'umd',
|
|
149
|
-
},
|
|
112
|
+
plugins: [moduleTools()],
|
|
150
113
|
buildPreset: 'npm-library',
|
|
114
|
+
buildConfig: [
|
|
115
|
+
{
|
|
116
|
+
format: 'iife',
|
|
117
|
+
target: 'es2020',
|
|
118
|
+
outDir: './dist/global'
|
|
119
|
+
}
|
|
120
|
+
]
|
|
151
121
|
});
|
|
152
122
|
```
|
|
153
123
|
|
|
154
|
-
|
|
124
|
+
这会在原本预设的基础上,额外生成一份 IIFE 格式、支持到 ES2020 语法的产物,输出目录为项目下的 `dist/global` 目录。
|
|
155
125
|
|
|
156
|
-
|
|
157
|
-
因为同时出现 'buildConfig' 和 'buildPreset' 配置,因此仅 'buildConfig' 配置生效
|
|
158
|
-
```
|
|
126
|
+
- 当 `buildConfig` 是对象时,会将对象中的配置项覆盖到预设中。
|
|
159
127
|
|
|
160
|
-
|
|
128
|
+
```ts title="modern.config.ts"
|
|
129
|
+
import { moduleTools, defineConfig } from '@modern-js/module-tools';
|
|
130
|
+
export default defineConfig({
|
|
131
|
+
plugins: [moduleTools()],
|
|
132
|
+
buildPreset: 'npm-library',
|
|
133
|
+
buildConfig: {
|
|
134
|
+
sourceMap: true,
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
```
|
|
161
138
|
|
|
162
|
-
|
|
139
|
+
这会使得每一项构建任务都会生成 sourceMap 文件。
|
|
@@ -7,7 +7,7 @@ sidebar_position: 4
|
|
|
7
7
|
模块工程解决方案提供了微生成器工具,它可以为当前项目:
|
|
8
8
|
|
|
9
9
|
- 新增目录和文件
|
|
10
|
-
- 修改 `package.json`
|
|
10
|
+
- 修改 `package.json`
|
|
11
11
|
- 执行命令
|
|
12
12
|
|
|
13
13
|
因此通过这些能力,**微生成器可以为项目开启额外的特性功能**。
|
|
@@ -20,17 +20,16 @@ sidebar_position: 4
|
|
|
20
20
|
|
|
21
21
|
:::tip
|
|
22
22
|
在成功开启后,会提示需要手动在配置中增加如下类似的代码。
|
|
23
|
+
|
|
23
24
|
```ts
|
|
24
25
|
import { moduleTools, defineConfig } from '@modern-js/module-tools';
|
|
25
26
|
import { testingPlugin } from '@modern-js/plugin-testing';
|
|
26
27
|
|
|
27
28
|
export default defineConfig({
|
|
28
|
-
plugins: [
|
|
29
|
-
moduleTools(),
|
|
30
|
-
testingPlugin(),
|
|
31
|
-
],
|
|
29
|
+
plugins: [moduleTools(), testingPlugin()],
|
|
32
30
|
});
|
|
33
31
|
```
|
|
32
|
+
|
|
34
33
|
:::
|
|
35
34
|
|
|
36
35
|
## Storybook 调试
|
|
@@ -39,17 +38,16 @@ export default defineConfig({
|
|
|
39
38
|
|
|
40
39
|
:::tip
|
|
41
40
|
在成功开启后,会提示需要手动在配置中增加如下类似的代码。
|
|
41
|
+
|
|
42
42
|
```ts
|
|
43
43
|
import { moduleTools, defineConfig } from '@modern-js/module-tools';
|
|
44
44
|
import { storybookPlugin } from '@modern-js/plugin-storybook';
|
|
45
45
|
|
|
46
46
|
export default defineConfig({
|
|
47
|
-
plugins: [
|
|
48
|
-
moduleTools(),
|
|
49
|
-
storybookPlugin(),
|
|
50
|
-
],
|
|
47
|
+
plugins: [moduleTools(), storybookPlugin()],
|
|
51
48
|
});
|
|
52
49
|
```
|
|
50
|
+
|
|
53
51
|
:::
|
|
54
52
|
|
|
55
53
|
关于如何启动 Storybook 以及如何使用 Storybook,可以查看下面的链接:
|
|
@@ -59,28 +57,11 @@ export default defineConfig({
|
|
|
59
57
|
|
|
60
58
|
## Tailwind CSS 支持
|
|
61
59
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
关于如何在模块工程里使用 Tailwind CSS,可以查看:
|
|
60
|
+
[Tailwind CSS](https://tailwindcss.com/) 是一个以 Utility Class 为基础的 CSS 框架和设计系统,可以快速地为组件添加常用样式,同时支持主题样式的灵活扩展。
|
|
65
61
|
|
|
66
|
-
|
|
62
|
+
如果你想要在项目使用 [Tailwind CSS](https://tailwindcss.com/),可以参考 [「使用 Tailwind CSS」](https://modernjs.dev/module-tools/guide/best-practices/components.html#tailwind-css)。
|
|
67
63
|
|
|
68
|
-
|
|
69
|
-
在成功开启后,会提示需要手动在配置中增加如下类似的代码。
|
|
70
|
-
```ts
|
|
71
|
-
import { moduleTools, defineConfig } from '@modern-js/module-tools';
|
|
72
|
-
import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
|
|
73
|
-
|
|
74
|
-
export default defineConfig({
|
|
75
|
-
plugins: [
|
|
76
|
-
moduleTools(),
|
|
77
|
-
tailwindPlugin(),
|
|
78
|
-
],
|
|
79
|
-
});
|
|
80
|
-
```
|
|
81
|
-
:::
|
|
82
|
-
|
|
83
|
-
## 启动 Modern.js Runtime API
|
|
64
|
+
## Modern.js Runtime API 支持
|
|
84
65
|
|
|
85
66
|
**Modern.js 提供了 [Runtime API](https://modernjs.dev/configure/app/runtime/intro) 能力,这些 API 只能在 Modern.js 的应用项目环境中使用**。如果你需要开发一个 Modern.js 应用环境中使用的组件,那么你可以开启该特性,微生成器会增加 `"@modern-js/runtime"`依赖。
|
|
86
67
|
|
|
@@ -88,15 +69,14 @@ export default defineConfig({
|
|
|
88
69
|
|
|
89
70
|
:::tip
|
|
90
71
|
在成功开启后,会提示需要手动在配置中增加如下类似的代码。
|
|
72
|
+
|
|
91
73
|
```ts
|
|
92
74
|
import { moduleTools, defineConfig } from '@modern-js/module-tools';
|
|
93
75
|
import runtime from '@modern-js/runtime/cli';
|
|
94
76
|
|
|
95
77
|
export default defineConfig({
|
|
96
|
-
plugins: [
|
|
97
|
-
moduleTools(),
|
|
98
|
-
runtime(),
|
|
99
|
-
],
|
|
78
|
+
plugins: [moduleTools(), runtime()],
|
|
100
79
|
});
|
|
101
80
|
```
|
|
81
|
+
|
|
102
82
|
:::
|
|
@@ -62,9 +62,7 @@ export const content = 'hello world';
|
|
|
62
62
|
"foo/*": ["./*"]
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
|
-
"include": [
|
|
66
|
-
"**/*"
|
|
67
|
-
]
|
|
65
|
+
"include": ["**/*"]
|
|
68
66
|
}
|
|
69
67
|
```
|
|
70
68
|
|
|
@@ -136,7 +134,6 @@ export default {
|
|
|
136
134
|
};
|
|
137
135
|
```
|
|
138
136
|
|
|
139
|
-
|
|
140
137
|
:::tip 为什么不推荐使用源码的方式?
|
|
141
138
|
不仅仅是因为使用组件源码无法验证组件产物是否正确,**同时模块工程对于构建产物支持的一些配置无法完全转换为 Storybook
|
|
142
139
|
内部的配置**。如果某些配置无法进行相互转换的话,就会在 Storybook 调试过程中出现不符合预期的结果。
|
|
@@ -159,7 +156,6 @@ Storybook 官方通过一个名为 `.storybook` 的文件夹来进行项目配
|
|
|
159
156
|
|
|
160
157
|
在未来我们会考虑这些配置是否可以允许修改,不过目前为了减少不可预知的问题暂时限制使用这些配置。
|
|
161
158
|
|
|
162
|
-
|
|
163
159
|
## 构建 Storybook 产物
|
|
164
160
|
|
|
165
161
|
除了可以对组件或者普通的模块进行 Storybook 调试,还可以通过下面的命令来执行 Storybook 的构建任务。
|
|
@@ -173,3 +169,18 @@ modern build --platform storybook
|
|
|
173
169
|
- [`modern build`](/guide/basic/command-preview#modern-build)
|
|
174
170
|
|
|
175
171
|
构建完成后,可以在 `dist/storybook-static` 目录看到构建产物文件。
|
|
172
|
+
|
|
173
|
+
## 结合 Tailwind CSS 使用
|
|
174
|
+
|
|
175
|
+
如果你需要在 `stories` 目录下使用 Tailwind CSS,请确保当前项目的 Tailwind CSS 配置中包含了 `stories` 目录。
|
|
176
|
+
|
|
177
|
+
以 `tailwind.config.ts` 文件为例,你需要配置以下内容:
|
|
178
|
+
|
|
179
|
+
```diff title="tailwind.config.ts"
|
|
180
|
+
export default {
|
|
181
|
+
content: [
|
|
182
|
+
'./src/**/*.{js,jsx,ts,tsx}',
|
|
183
|
+
+ './stories/**/*.{js,jsx,ts,tsx}',
|
|
184
|
+
],
|
|
185
|
+
};
|
|
186
|
+
```
|
|
@@ -168,202 +168,7 @@ body {
|
|
|
168
168
|
|
|
169
169
|
### Tailwind CSS
|
|
170
170
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
默认情况下,模块工程没有开启该功能,需要按照下面的方式开启它。
|
|
174
|
-
|
|
175
|
-
1. 在项目根目录下执行 `new` 命令,可以开启 Tailwind CSS 功能。
|
|
176
|
-
|
|
177
|
-
```text title="终端"
|
|
178
|
-
pnpm run new
|
|
179
|
-
|
|
180
|
-
? 请选择你想要的操作 启用可选功能
|
|
181
|
-
? 请选择功能名称 启用 「Tailwind CSS」 支持
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
2. 成功开启后,会看到 `package.json` 中新增了依赖。
|
|
185
|
-
|
|
186
|
-
```json title="./package.json"
|
|
187
|
-
{
|
|
188
|
-
"devDependencies": {
|
|
189
|
-
"@modern-js/plugin-tailwindcss": "x.y.z"
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
3. Tailwind CSS 提供了两种使用方式:
|
|
195
|
-
|
|
196
|
-
#### HTML 类名
|
|
197
|
-
|
|
198
|
-
Tailwind CSS 支持在 HTML 标签上使用类名的方式增加样式。**当使用 HTML 类名的时候,一定要注意导入 Tailwind CSS 相应的 css 文件。**
|
|
199
|
-
|
|
200
|
-
```tsx title="./src/index.tsx"
|
|
201
|
-
import 'tailwindcss/utilities.css';
|
|
202
|
-
|
|
203
|
-
export default () => {
|
|
204
|
-
return <div className="bg-black text-white">hello world</div>;
|
|
205
|
-
};
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
样式产物(此时是 bundle 构建):
|
|
209
|
-
|
|
210
|
-
```css title="./dist/es/index.css"
|
|
211
|
-
/* node_modules/tailwindcss/utilities.css */
|
|
212
|
-
.table {
|
|
213
|
-
display: table;
|
|
214
|
-
}
|
|
215
|
-
.bg-black {
|
|
216
|
-
--tw-bg-opacity: 1;
|
|
217
|
-
background-color: rgba(0, 0, 0, var(--tw-bg-opacity));
|
|
218
|
-
}
|
|
219
|
-
.text-white {
|
|
220
|
-
--tw-text-opacity: 1;
|
|
221
|
-
color: rgba(255, 255, 255, var(--tw-text-opacity));
|
|
222
|
-
}
|
|
223
|
-
/** some more... */
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
#### `@apply`
|
|
227
|
-
|
|
228
|
-
Tailwind CSS 提供了 [`@apply`](https://v2.tailwindcss.com/docs/functions-and-directives#apply) 指令,可以通过它将 Tailwind CSS 提供的样式内联到我们编写的样式中。
|
|
229
|
-
|
|
230
|
-
`@apply` 可以用于 CSS、Less、Sass 中。
|
|
231
|
-
|
|
232
|
-
```css
|
|
233
|
-
.btn {
|
|
234
|
-
@apply font-bold py-2 px-4 rounded;
|
|
235
|
-
}
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
但是使用过程中,对于 Less 和 Sass 有些情况需要注意:
|
|
239
|
-
|
|
240
|
-
##### Sass
|
|
241
|
-
|
|
242
|
-
当将 Tailwind 与 Sass 一起使用时,`@apply` 后面存在 `!important` 的时候,需要使用插值来让 Sass 正确编译。
|
|
243
|
-
|
|
244
|
-
- 不能正常工作:
|
|
245
|
-
|
|
246
|
-
```sass
|
|
247
|
-
.alert {
|
|
248
|
-
@apply bg-red-500 !important;
|
|
249
|
-
}
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
- 能够正常工作:
|
|
253
|
-
|
|
254
|
-
```sass
|
|
255
|
-
.alert {
|
|
256
|
-
@apply bg-red-500 #{!important};
|
|
257
|
-
}
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
##### Less
|
|
261
|
-
|
|
262
|
-
在与 Less 一起使用 Tailwind 时,你不能嵌套 Tailwind 的 `@screen` 指令。
|
|
263
|
-
|
|
264
|
-
- 不能正常工作:
|
|
265
|
-
|
|
266
|
-
```less
|
|
267
|
-
.card {
|
|
268
|
-
@apply rounded-none;
|
|
269
|
-
|
|
270
|
-
@screen sm {
|
|
271
|
-
@apply rounded-lg;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
- 相反,使用常规的媒体查询和 `theme()` 函数来引用你的屏幕尺寸,或者干脆不要嵌套你的 `@screen` 指令。
|
|
277
|
-
|
|
278
|
-
```less title="方法一"
|
|
279
|
-
// Use a regular media query and theme()
|
|
280
|
-
.card {
|
|
281
|
-
@apply rounded-none;
|
|
282
|
-
|
|
283
|
-
@media (min-width: theme('screens.sm')) {
|
|
284
|
-
@apply rounded-lg;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
```less title="方法二"
|
|
290
|
-
// Use the @screen directive at the top-level
|
|
291
|
-
.card {
|
|
292
|
-
@apply rounded-none;
|
|
293
|
-
|
|
294
|
-
@media (min-width: theme('screens.sm')) {
|
|
295
|
-
@apply rounded-lg;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
#### 推荐方式
|
|
301
|
-
|
|
302
|
-
**推荐使用 `@apply` 指定的方式开发样式,这样在样式产物中仅包含通过指令内联的样式。**
|
|
303
|
-
|
|
304
|
-
当使用 HTML 类名的方式添加样式的时候,默认情况下 Tailwind 不仅会将本身类名对应的样式加入产物中,同时还会有额外的样式代码存在,虽然这些代码可能不会对本身的样式产生影响。
|
|
305
|
-
|
|
306
|
-
#### bundle 和 bundleless 构建产物区别
|
|
307
|
-
|
|
308
|
-
对于以下代码,在 bundle 和 bundleless 两种模式下,构建产物会有很大区别。
|
|
309
|
-
|
|
310
|
-
> 所谓 bundle 和 bundleless 可以查看 [「Bundle 和 Bundleless」](/guide/advance/in-depth-about-build#bundle-和-bundleless)
|
|
311
|
-
|
|
312
|
-
```tsx title="./src/index.tsx"
|
|
313
|
-
import 'tailwindcss/utilities.css';
|
|
314
|
-
|
|
315
|
-
export default () => {
|
|
316
|
-
return <div className="bg-black text-white">hello world11</div>;
|
|
317
|
-
};
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
Bundle 模式下,会将第三方依赖打包进来。
|
|
321
|
-
|
|
322
|
-
对于样式则会生成一份单独的产物文件,并且在 JS 产物文件中并不会存在导入样式的相关代码。
|
|
323
|
-
|
|
324
|
-
如果需要将样式注入 JS 产物中,可以开启 [`style.inject`](/api/config/build-config#styleinject) 选项。
|
|
325
|
-
|
|
326
|
-
```css title="./dist/es/index.css"
|
|
327
|
-
/* node_modules/tailwindcss/utilities.css */
|
|
328
|
-
.table {
|
|
329
|
-
display: table;
|
|
330
|
-
}
|
|
331
|
-
.bg-black {
|
|
332
|
-
--tw-bg-opacity: 1;
|
|
333
|
-
background-color: rgba(0, 0, 0, var(--tw-bg-opacity));
|
|
334
|
-
}
|
|
335
|
-
.text-white {
|
|
336
|
-
--tw-text-opacity: 1;
|
|
337
|
-
color: rgba(255, 255, 255, var(--tw-text-opacity));
|
|
338
|
-
}
|
|
339
|
-
/** some more... */
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
```js title="./dist/es/index.js"
|
|
343
|
-
// src/index.tsx
|
|
344
|
-
import { jsx } from 'react/jsx-runtime';
|
|
345
|
-
var src_default = () => {
|
|
346
|
-
return /* @__PURE__ */ jsx('div', {
|
|
347
|
-
className: 'bg-black text-white',
|
|
348
|
-
children: 'hello world11',
|
|
349
|
-
});
|
|
350
|
-
};
|
|
351
|
-
export { src_default as default };
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
Bundleless 模式下,并不会将第三方依赖打包进来,此时不会有样式产物生成。
|
|
355
|
-
|
|
356
|
-
```js title="./dist/es/index.js"
|
|
357
|
-
import { jsx } from 'react/jsx-runtime';
|
|
358
|
-
import 'tailwindcss/utilities.css';
|
|
359
|
-
var src_default = () => {
|
|
360
|
-
return /* @__PURE__ */ jsx('div', {
|
|
361
|
-
className: 'bg-black text-white',
|
|
362
|
-
children: 'hello world11',
|
|
363
|
-
});
|
|
364
|
-
};
|
|
365
|
-
export { src_default as default };
|
|
366
|
-
```
|
|
171
|
+
请参考 [「使用 Tailwind CSS」](/guide/best-practices/use-tailwindcss.html) 来了解相关用法。
|
|
367
172
|
|
|
368
173
|
### CSS Modules
|
|
369
174
|
|