@modern-js/module-tools-docs 2.0.0-beta.4 → 2.0.1

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.
Files changed (198) hide show
  1. package/.eslintrc.js +13 -0
  2. package/docs/en/api/config/_category_.json +4 -0
  3. package/docs/en/api/{build-config.md → config/build-config.md} +198 -105
  4. package/docs/en/api/{build-preset.md → config/build-preset.md} +65 -46
  5. package/docs/{zh/api → en/api/config}/design-system.md +130 -127
  6. package/docs/en/api/config/plugins.md +18 -0
  7. package/docs/en/api/{test.md → config/testing.md} +21 -12
  8. package/docs/en/api/index.md +3 -1
  9. package/docs/en/api/plugin-api/_category_.json +4 -0
  10. package/docs/en/api/plugin-api/plugin-hooks.md +501 -0
  11. package/docs/en/guide/advance/_category_.json +4 -0
  12. package/docs/en/guide/advance/asset.mdx +47 -30
  13. package/docs/en/guide/advance/build-umd.mdx +88 -80
  14. package/docs/en/guide/advance/copy.md +31 -28
  15. package/docs/en/guide/advance/external-dependency.mdx +27 -23
  16. package/docs/en/guide/advance/in-depth-about-build.md +56 -50
  17. package/docs/en/guide/advance/in-depth-about-dev-command.md +7 -4
  18. package/docs/en/guide/advance/theme-config.mdx +80 -0
  19. package/docs/en/guide/basic/_category_.json +4 -0
  20. package/docs/en/guide/basic/before-getting-started.md +28 -23
  21. package/docs/en/guide/basic/command-preview.md +18 -14
  22. package/docs/en/guide/basic/modify-output-product.md +27 -20
  23. package/docs/en/guide/basic/publish-your-project.md +21 -19
  24. package/docs/en/guide/basic/test-your-project.mdx +19 -16
  25. package/docs/en/guide/basic/use-micro-generator.md +10 -7
  26. package/docs/en/guide/basic/using-storybook.mdx +27 -26
  27. package/docs/en/guide/best-practices/_category_.json +4 -0
  28. package/docs/en/guide/best-practices/components.mdx +783 -0
  29. package/docs/en/guide/intro/_category_.json +4 -0
  30. package/docs/en/guide/intro/getting-started.md +9 -4
  31. package/docs/en/guide/intro/welcome.md +4 -0
  32. package/docs/en/guide/intro/why-module-engineering-solution.md +5 -1
  33. package/docs/en/index.md +3 -5
  34. package/docs/en/plugins/guide/_category_.json +4 -0
  35. package/docs/en/plugins/guide/getting-started.mdx +84 -0
  36. package/docs/en/plugins/guide/plugin-object.mdx +85 -0
  37. package/docs/en/plugins/guide/setup-function.mdx +117 -0
  38. package/docs/en/plugins/official-list/_category_.json +4 -0
  39. package/docs/en/plugins/official-list/overview.md +6 -0
  40. package/docs/zh/api/config/_category_.json +4 -0
  41. package/docs/zh/api/config/build-config.md +658 -0
  42. package/docs/zh/api/{build-preset.md → config/build-preset.md} +49 -36
  43. package/docs/zh/api/config/design-system.md +1166 -0
  44. package/docs/zh/api/config/plugins.md +18 -0
  45. package/docs/zh/api/{testing.md → config/testing.md} +18 -13
  46. package/docs/zh/api/index.md +4 -1
  47. package/docs/zh/api/plugin-api/_category_.json +4 -0
  48. package/docs/zh/api/plugin-api/plugin-hooks.md +500 -0
  49. package/docs/zh/guide/advance/_category_.json +4 -0
  50. package/docs/zh/guide/advance/asset.mdx +47 -30
  51. package/docs/zh/guide/advance/build-umd.mdx +88 -71
  52. package/docs/zh/guide/advance/copy.md +31 -28
  53. package/docs/zh/guide/advance/external-dependency.mdx +27 -23
  54. package/docs/zh/guide/advance/in-depth-about-build.md +55 -50
  55. package/docs/zh/guide/advance/in-depth-about-dev-command.md +7 -4
  56. package/docs/zh/guide/advance/theme-config.mdx +78 -0
  57. package/docs/zh/guide/basic/_category_.json +4 -0
  58. package/docs/zh/guide/basic/before-getting-started.md +27 -22
  59. package/docs/zh/guide/basic/command-preview.md +17 -13
  60. package/docs/zh/guide/basic/modify-output-product.md +28 -21
  61. package/docs/zh/guide/basic/publish-your-project.md +22 -17
  62. package/docs/zh/guide/basic/test-your-project.mdx +20 -16
  63. package/docs/zh/guide/basic/use-micro-generator.md +10 -7
  64. package/docs/zh/guide/basic/using-storybook.mdx +27 -24
  65. package/docs/zh/guide/best-practices/_category_.json +4 -0
  66. package/docs/zh/guide/best-practices/components.mdx +793 -0
  67. package/docs/zh/guide/intro/_category_.json +4 -0
  68. package/docs/zh/guide/intro/getting-started.md +10 -4
  69. package/docs/zh/guide/intro/welcome.md +4 -0
  70. package/docs/zh/guide/intro/why-module-engineering-solution.md +5 -1
  71. package/docs/zh/index.md +3 -2
  72. package/docs/zh/plugins/guide/_category_.json +4 -0
  73. package/docs/zh/plugins/guide/getting-started.mdx +84 -0
  74. package/docs/zh/plugins/guide/plugin-object.mdx +85 -0
  75. package/docs/zh/plugins/guide/setup-function.mdx +117 -0
  76. package/docs/zh/plugins/official-list/_category_.json +4 -0
  77. package/docs/zh/plugins/official-list/overview.md +5 -0
  78. package/modern.config.ts +118 -0
  79. package/package.json +8 -6
  80. package/{docs/.island/styles → theme}/index.css +0 -1
  81. package/theme/index.ts +4 -0
  82. package/tsconfig.json +7 -0
  83. package/docs/.island/config.ts +0 -245
  84. package/docs/.island/dist/404.html +0 -41
  85. package/docs/.island/dist/assets/before-getting-started.1b82b538.js +0 -87
  86. package/docs/.island/dist/assets/before-getting-started.582a31cc.js +0 -87
  87. package/docs/.island/dist/assets/build-config.72eb0918.js +0 -804
  88. package/docs/.island/dist/assets/build-config.d8bb1658.js +0 -809
  89. package/docs/.island/dist/assets/build-preset.96805d7d.js +0 -256
  90. package/docs/.island/dist/assets/build-preset.c20dcd40.js +0 -256
  91. package/docs/.island/dist/assets/build-your-ui.7f349247.js +0 -2
  92. package/docs/.island/dist/assets/build-your-ui.a8361604.js +0 -2
  93. package/docs/.island/dist/assets/command-preview.2d45fc82.js +0 -264
  94. package/docs/.island/dist/assets/command-preview.dc51b953.js +0 -264
  95. package/docs/.island/dist/assets/components.esm.03560353.js +0 -9
  96. package/docs/.island/dist/assets/design-system.86694ff5.js +0 -1254
  97. package/docs/.island/dist/assets/design-system.c4745cce.js +0 -639
  98. package/docs/.island/dist/assets/dev.1d326a37.js +0 -37
  99. package/docs/.island/dist/assets/dev.1fd06000.js +0 -37
  100. package/docs/.island/dist/assets/down.f35427d3.svg +0 -1
  101. package/docs/.island/dist/assets/extension.12299fd6.js +0 -2
  102. package/docs/.island/dist/assets/extension.96dc63a4.js +0 -2
  103. package/docs/.island/dist/assets/getting-started.40e9218d.js +0 -117
  104. package/docs/.island/dist/assets/getting-started.b1ed3f10.js +0 -114
  105. package/docs/.island/dist/assets/github.3bf8ccee.svg +0 -1
  106. package/docs/.island/dist/assets/index.2b2347ea.js +0 -33
  107. package/docs/.island/dist/assets/index.6cef6f5f.js +0 -4
  108. package/docs/.island/dist/assets/index.cb118238.js +0 -36
  109. package/docs/.island/dist/assets/index.ccb6ce27.js +0 -4
  110. package/docs/.island/dist/assets/island_inject.11a12ecc.js +0 -1
  111. package/docs/.island/dist/assets/island_inject.b13deaee.js +0 -1
  112. package/docs/.island/dist/assets/loading.8c9bb911.svg +0 -1
  113. package/docs/.island/dist/assets/modify-output-product.7f6bff35.js +0 -100
  114. package/docs/.island/dist/assets/modify-output-product.b91eff1f.js +0 -100
  115. package/docs/.island/dist/assets/moon.6b705924.svg +0 -3
  116. package/docs/.island/dist/assets/plugin.895932d8.js +0 -42
  117. package/docs/.island/dist/assets/plugin.d2fbc531.js +0 -42
  118. package/docs/.island/dist/assets/publish-your-project.21b8309f.js +0 -164
  119. package/docs/.island/dist/assets/publish-your-project.8d398b17.js +0 -166
  120. package/docs/.island/dist/assets/right.89674cd7.svg +0 -1
  121. package/docs/.island/dist/assets/search.0aea6901.svg +0 -1
  122. package/docs/.island/dist/assets/search.1c85d17c.js +0 -3
  123. package/docs/.island/dist/assets/search.484eca11.js +0 -222
  124. package/docs/.island/dist/assets/search.54fca8d0.js +0 -3
  125. package/docs/.island/dist/assets/style.09015a4b.css +0 -1
  126. package/docs/.island/dist/assets/style.2e5f7bc2.css +0 -1970
  127. package/docs/.island/dist/assets/sun.841dac10.svg +0 -11
  128. package/docs/.island/dist/assets/test-your-project.18bd4582.js +0 -190
  129. package/docs/.island/dist/assets/test-your-project.f53bebf7.js +0 -190
  130. package/docs/.island/dist/assets/test.0da1f99f.js +0 -67
  131. package/docs/.island/dist/assets/test.0e81f002.js +0 -66
  132. package/docs/.island/dist/assets/translator.b1077c44.svg +0 -1
  133. package/docs/.island/dist/assets/use-micro-generator.7d9e4016.js +0 -60
  134. package/docs/.island/dist/assets/use-micro-generator.db5520c1.js +0 -60
  135. package/docs/.island/dist/assets/using-storybook.57ea6b77.js +0 -260
  136. package/docs/.island/dist/assets/using-storybook.a2212f2e.js +0 -260
  137. package/docs/.island/dist/assets/welcome.0449a9c8.js +0 -13
  138. package/docs/.island/dist/assets/welcome.a8448931.js +0 -13
  139. package/docs/.island/dist/assets/why-module-engineering-solution.6ae8c0e3.js +0 -26
  140. package/docs/.island/dist/assets/why-module-engineering-solution.c9a45cbd.js +0 -26
  141. package/docs/.island/dist/chunk-COLCRJ2V.js +0 -1
  142. package/docs/.island/dist/chunk-K5FMOYDC.js +0 -10
  143. package/docs/.island/dist/chunk-WE42KMYS.js +0 -26
  144. package/docs/.island/dist/client-entry.js +0 -3
  145. package/docs/.island/dist/en/api/build-config.html +0 -344
  146. package/docs/.island/dist/en/api/build-preset.html +0 -82
  147. package/docs/.island/dist/en/api/design-system.html +0 -155
  148. package/docs/.island/dist/en/api/dev.html +0 -45
  149. package/docs/.island/dist/en/api/index.html +0 -41
  150. package/docs/.island/dist/en/api/plugin.html +0 -48
  151. package/docs/.island/dist/en/api/test.html +0 -58
  152. package/docs/.island/dist/en/guide/before-getting-started.html +0 -127
  153. package/docs/.island/dist/en/guide/build-your-ui.html +0 -41
  154. package/docs/.island/dist/en/guide/command-preview.html +0 -100
  155. package/docs/.island/dist/en/guide/extension.html +0 -41
  156. package/docs/.island/dist/en/guide/getting-started.html +0 -76
  157. package/docs/.island/dist/en/guide/modify-output-product.html +0 -140
  158. package/docs/.island/dist/en/guide/publish-your-project.html +0 -91
  159. package/docs/.island/dist/en/guide/test-your-project.html +0 -72
  160. package/docs/.island/dist/en/guide/use-micro-generator.html +0 -65
  161. package/docs/.island/dist/en/guide/using-storybook.html +0 -113
  162. package/docs/.island/dist/en/guide/welcome.html +0 -53
  163. package/docs/.island/dist/en/guide/why-module-engineering-solution.html +0 -49
  164. package/docs/.island/dist/en/index.html +0 -42
  165. package/docs/.island/dist/react-dom.js +0 -1
  166. package/docs/.island/dist/react-dom_client.js +0 -1
  167. package/docs/.island/dist/react.js +0 -1
  168. package/docs/.island/dist/react_jsx-runtime.js +0 -10
  169. package/docs/.island/dist/ssr-manifest.json +0 -57
  170. package/docs/.island/dist/test-result.png +0 -0
  171. package/docs/.island/dist/why-module-solution.png +0 -0
  172. package/docs/.island/dist/zh/api/build-config.html +0 -347
  173. package/docs/.island/dist/zh/api/build-preset.html +0 -82
  174. package/docs/.island/dist/zh/api/design-system.html +0 -149
  175. package/docs/.island/dist/zh/api/dev.html +0 -46
  176. package/docs/.island/dist/zh/api/index.html +0 -41
  177. package/docs/.island/dist/zh/api/plugin.html +0 -48
  178. package/docs/.island/dist/zh/api/test.html +0 -59
  179. package/docs/.island/dist/zh/guide/before-getting-started.html +0 -127
  180. package/docs/.island/dist/zh/guide/build-your-ui.html +0 -41
  181. package/docs/.island/dist/zh/guide/command-preview.html +0 -100
  182. package/docs/.island/dist/zh/guide/extension.html +0 -41
  183. package/docs/.island/dist/zh/guide/getting-started.html +0 -79
  184. package/docs/.island/dist/zh/guide/modify-output-product.html +0 -140
  185. package/docs/.island/dist/zh/guide/publish-your-project.html +0 -92
  186. package/docs/.island/dist/zh/guide/test-your-project.html +0 -72
  187. package/docs/.island/dist/zh/guide/use-micro-generator.html +0 -65
  188. package/docs/.island/dist/zh/guide/using-storybook.html +0 -114
  189. package/docs/.island/dist/zh/guide/welcome.html +0 -53
  190. package/docs/.island/dist/zh/guide/why-module-engineering-solution.html +0 -49
  191. package/docs/.island/dist/zh/index.html +0 -42
  192. package/docs/.island/index.html +0 -39
  193. package/docs/en/api/design-system.md +0 -524
  194. package/docs/en/api/dev.md +0 -32
  195. package/docs/en/api/plugin.md +0 -34
  196. package/docs/zh/api/build-config.md +0 -570
  197. package/docs/zh/api/dev.md +0 -33
  198. package/docs/zh/api/plugins.md +0 -108
@@ -0,0 +1,783 @@
1
+ # Developing Components
2
+
3
+ This chapter will describe how to develop component projects using the module engineering solution.
4
+
5
+ # # Initialize the project
6
+
7
+ <CH.Spotlight>
8
+
9
+ ``` bash Interactive questions
10
+ npx @modern-js/create components-project
11
+
12
+ ? Please select the solution you want to create Module Solution
13
+ ? Package Name components-demo
14
+ ? Development Language TS
15
+ ? Package Management Tool pnpm
16
+ ```
17
+ ---
18
+
19
+ It is recommended to use the `@modern-js/create` command to initialize an npm project.
20
+
21
+ ``` bash Interactive questions
22
+ npx @modern-js/create components-project
23
+
24
+ ? Please select the solution you want to create Module Solution
25
+ ? Package Name components-demo
26
+ ? Development Language TS
27
+ ? Package Management Tool pnpm
28
+ ```
29
+
30
+ ---
31
+
32
+ The initialized directory structure.
33
+
34
+ ``` bash The initialized directory structure
35
+ .
36
+ ├── README.md
37
+ ├── node_modules/
38
+ ├── dist/
39
+ ├── modern.config.ts
40
+ ├── package.json
41
+ ├── pnpm-lock.yaml
42
+ ├── src
43
+ │ ├── index.ts
44
+ │ └── modern-app-env.d.ts
45
+ └── tsconfig.json
46
+ ```
47
+
48
+ ---
49
+
50
+ Modify the `. /src/index.ts` file suffix and content.
51
+
52
+ At this point, a component project is initialized.
53
+
54
+ ``` tsx ./src/index.tsx
55
+ export default () => {
56
+ return (
57
+ <div>hello world</div>
58
+ );
59
+ }
60
+ ```
61
+ </CH.Spotlight>
62
+
63
+ ## Debugging code with Storybook
64
+
65
+ <CH.Spotlight>
66
+
67
+ ``` bash Terminal
68
+ pnpm run new
69
+
70
+ ? Action Enable features
71
+ ? Enable features Enable Visual Testing (Storybook)
72
+ ```
73
+ ---
74
+
75
+ Execute the `new` command in the project root directory to enable the Storybook feature.
76
+
77
+ ``` bash Terminal
78
+ pnpm run new
79
+
80
+ ? Action Enable features
81
+ ? Enable features Enable Visual Testing (Storybook)
82
+ ```
83
+ ---
84
+
85
+ Once successfully opened, you will see that a new dependency has been added to `package.json`. The `stories` directory and related initialization files are also created.
86
+
87
+ <CH.Code>
88
+
89
+ ```json ./package.json focus=4:7
90
+ {
91
+ "name": "components-demo",
92
+ "devDependencies": {
93
+ "@modern-js/plugin-storybook": "x.y.z",
94
+ "@modern-js/runtime": "x.y.z",
95
+ "react": "^17",
96
+ "react-dom": "^17"
97
+ }
98
+ }
99
+ ```
100
+
101
+ ```bash Directory Structure
102
+ .
103
+ ├── src
104
+ │ ├── index.ts
105
+ │ └── modern-app-env.d.ts
106
+ ├── stories
107
+ │ ├── .eslintrc.js
108
+ │ ├── index.stories.tsx
109
+ │ └── tsconfig.json
110
+ ```
111
+
112
+ </CH.Code>
113
+
114
+ ---
115
+
116
+ After initialization, the `tsconfig.json` file in the `. /stories` directory, the `tsconfig.json` file is set by default with the `paths` configuration of the same name as the project.
117
+
118
+ ```json ./stories/tsconfig.json focus=5:7
119
+ {
120
+ "extends": "../tsconfig.json",
121
+ "compilerOptions": {
122
+ "baseUrl": "../",
123
+ "paths": {
124
+ "components-demo": ["./"],
125
+ "components-demo/*": ["./*"]
126
+ }
127
+ },
128
+ "include": ["**/*"]
129
+ }
130
+ ```
131
+
132
+ ---
133
+
134
+ Such a configuration allows you to introduce code in Story code directly using the name of the project.
135
+
136
+ <CH.Code>
137
+
138
+ ``` tsx ./stories/index.stories.tsx
139
+ import Component from 'components-demo';
140
+
141
+ export const YourStory = () => <Component />;
142
+
143
+ export default {
144
+ title: 'Your Stories',
145
+ };
146
+
147
+ ```
148
+ ---
149
+ ``` tsx ./src/index.tsx
150
+ export default () => {
151
+ return (
152
+ <div>hello world</div>
153
+ );
154
+ }
155
+ ```
156
+
157
+ </CH.Code>
158
+
159
+ ---
160
+
161
+ At this point Storybook identifies the entry point for the imported code based on fields like `main`, `exports` in the project's `package.json` file.
162
+ The location of the type file is determined by the `types` field.
163
+
164
+ ```json package.json
165
+ {
166
+ "name": "components-demo",
167
+ "main": "./dist/esm/index.js",
168
+ "types": "./dist/types/index.d.ts"
169
+ }
170
+ ```
171
+
172
+ ---
173
+
174
+ While importing source code for debugging is also supported, debugging using project artifacts is more reliable.
175
+
176
+ Debugging with source code has a limitation: some configurations are not equivalent in Storybook and in the original build support.
177
+
178
+ **This is why debugging with the product is recommended**.
179
+
180
+ ``` tsx ./stories/index.stories.tsx
181
+ import Component from '../src';
182
+
183
+ export const YourStory = () => <Component />;
184
+
185
+ export default {
186
+ title: 'Your Stories',
187
+ };
188
+ ```
189
+
190
+ </CH.Spotlight>
191
+
192
+ ## Developing Styles
193
+
194
+ Next we can add styles to the component.
195
+
196
+ The following capabilities are currently supported for developing styles.
197
+
198
+ * CSS/PostCSS
199
+ * Less
200
+ * Scss/Sass
201
+ * Tailwind CSS
202
+ * CSS Modules
203
+
204
+ ### CSS/PostCSS
205
+
206
+ The module project supports PostCSS and has the following built-in postcss plugins.
207
+
208
+ + [flexbugs-fixes](https://github.com/luisrudge/postcss-flexbugs-fixes)
209
+ + [custom-properties](https://github.com/postcss/postcss-custom-properties)
210
+ + [initial](https://github.com/maximkoretskiy/postcss-initial)
211
+ + [page-break](https://github.com/shrpne/postcss-page-break)
212
+ + [font-variant](https://github.com/postcss/postcss-font-variant)
213
+ + [media-minmax](https://github.com/postcss/postcss-media-minmax)
214
+ + [nesting](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-nesting#readme)
215
+
216
+ So we can create `.css` files in our projects and use the syntax support and capabilities provided by these plugins directly in our css files.
217
+
218
+ <CH.Spotlight>
219
+
220
+
221
+ ``` css ./src/index.css
222
+ a,
223
+ b {
224
+ color: red;
225
+
226
+ /* "&" comes first */
227
+ & c,
228
+ & d {
229
+ color: white;
230
+ }
231
+
232
+ /* "&" comes later, requiring "@nest" */
233
+ @nest e & {
234
+ color: yellow;
235
+ }
236
+ }
237
+ ```
238
+
239
+ ---
240
+
241
+ Source Code.
242
+
243
+ ``` css ./src/index.css
244
+ ```
245
+
246
+ ---
247
+
248
+ css product.
249
+
250
+ ``` css ./dist/es/index.css
251
+ a,
252
+ b {
253
+ color: red;
254
+ }
255
+ a c,
256
+ b c,
257
+ a d,
258
+ b d {
259
+ color: white;
260
+ }
261
+ e a,
262
+ e b {
263
+ color: yellow;
264
+ }
265
+ ```
266
+
267
+ </CH.Spotlight>
268
+
269
+ ### Less
270
+
271
+ Module projects support development styles using Less.
272
+
273
+ > Currently supported version is 4.1.3
274
+
275
+ <CH.Spotlight>
276
+
277
+ ```less ./src/common.less
278
+ @bg: black;
279
+ @bg-light: boolean(luma(@bg) > 50%);
280
+
281
+ div {
282
+ background: @bg;
283
+ color: if(@bg-light, black, white);
284
+ }
285
+ ```
286
+ ---
287
+
288
+ Source Code.
289
+
290
+ ```less ./src/common.less
291
+ ```
292
+
293
+ ---
294
+
295
+ Less product.
296
+
297
+ ```css ./dist/es/common.css
298
+ div {
299
+ background: black;
300
+ color: white;
301
+ }
302
+ ```
303
+
304
+ </CH.Spotlight>
305
+
306
+ ### Sass/Scss
307
+
308
+ Module projects support developing styles using Scss/Sass.
309
+
310
+ > Currently supported version is 1.54.4
311
+
312
+ <CH.Spotlight>
313
+
314
+ ```sass ./src/common.sass
315
+ $font-stack: Helvetica, sans-serif;
316
+ $primary-color: #333;
317
+
318
+ body {
319
+ font: 100% $font-stack;
320
+ color: $primary-color;
321
+ }
322
+ ```
323
+ ---
324
+
325
+ 源代码。
326
+
327
+ ```sass ./src/common.sass
328
+ ```
329
+
330
+ ---
331
+
332
+ Less product.
333
+
334
+ ```css ./dist/es/common.css
335
+ body {
336
+ font: 100% Helvetica, sans-serif;
337
+ color: #333;
338
+ }
339
+ ```
340
+
341
+ </CH.Spotlight>
342
+
343
+ ### Tailwind CSS
344
+
345
+ The module project supports the development of component styles using Tailwind CSS.
346
+
347
+ By default, this feature is not enabled in the module project, you need to enable it as follows.
348
+
349
+ <CH.Spotlight>
350
+
351
+ ``` bash Terminal
352
+ pnpm run new
353
+
354
+ ? Action Enable features
355
+ ? Enable features Enable Visual Testing (Storybook)
356
+ ```
357
+ ---
358
+
359
+ The Tailwind CSS feature can be enabled by executing the `new` command in the project root directory.
360
+
361
+ ``` bash Terminal
362
+ pnpm run new
363
+
364
+ ? Action Enable features
365
+ ? Enable features Enable Tailwind CSS
366
+ ```
367
+ ---
368
+
369
+ Once successfully opened, you will see that a new dependency has been added to `package.json`.
370
+
371
+ ``` json ./package.json
372
+ {
373
+ "devDependencies": {
374
+ "@modern-js/plugin-tailwindcss": "x.y.z"
375
+ }
376
+ }
377
+ ```
378
+
379
+ </CH.Spotlight>
380
+
381
+ Tailwind CSS offers two ways to use it.
382
+
383
+ #### HTML class
384
+
385
+ <CH.Spotlight>
386
+
387
+ ``` tsx ./src/index.tsx
388
+ import 'tailwindcss/utilities.css';
389
+
390
+ export default () => {
391
+ return (
392
+ <div className="bg-black text-white">hello world</div>
393
+ );
394
+ }
395
+ ```
396
+ ---
397
+
398
+ Tailwind CSS supports adding styles to HTML tags by using class names.
399
+
400
+ ``` tsx ./src/index.tsx focus=5:5
401
+ ```
402
+ ---
403
+
404
+ **When using HTML class names, be sure to import the Tailwind CSS equivalent css file. **
405
+
406
+ ``` tsx ./src/index.tsx focus=1:1
407
+
408
+ ```
409
+
410
+ ---
411
+
412
+ Style product.
413
+
414
+ > This is a bundle build.
415
+
416
+
417
+ ```css ./dist/es/index.css
418
+ /* ../../node_modules/.pnpm/tailwindcss@2.2.19/node_modules/tailwindcss/utilities.css */
419
+ .table {
420
+ display: table;
421
+ }
422
+ @keyframes spin {
423
+ to {
424
+ transform: rotate(360deg);
425
+ }
426
+ }
427
+ @keyframes ping {
428
+ 75%, 100% {
429
+ transform: scale(2);
430
+ opacity: 0;
431
+ }
432
+ }
433
+ @keyframes pulse {
434
+ 50% {
435
+ opacity: .5;
436
+ }
437
+ }
438
+ @keyframes bounce {
439
+ 0%, 100% {
440
+ transform: translateY(-25%);
441
+ animation-timing-function: cubic-bezier(0.8, 0, 1, 1);
442
+ }
443
+ 50% {
444
+ transform: none;
445
+ animation-timing-function: cubic-bezier(0, 0, 0.2, 1);
446
+ }
447
+ }
448
+ .bg-black {
449
+ --tw-bg-opacity: 1;
450
+ background-color: rgba(0, 0, 0, var(--tw-bg-opacity));
451
+ }
452
+ .text-white {
453
+ --tw-text-opacity: 1;
454
+ color: rgba(255, 255, 255, var(--tw-text-opacity));
455
+ }
456
+ *,
457
+ ::before,
458
+ ::after {
459
+ --tw-shadow: 0 0 #0000 ;
460
+ }
461
+ *,
462
+ ::before,
463
+ ::after {
464
+ --tw-ring-inset: var(--tw-empty, );
465
+ --tw-ring-offset-width: 0px;
466
+ --tw-ring-offset-color: #fff;
467
+ --tw-ring-color: rgba(59, 130, 246, 0.5);
468
+ --tw-ring-offset-shadow: 0 0 #0000;
469
+ --tw-ring-shadow: 0 0 #0000 ;
470
+ }
471
+ @media (min-width: 640px) {
472
+ }
473
+ @media (min-width: 768px) {
474
+ }
475
+ @media (min-width: 1024px) {
476
+ }
477
+ @media (min-width: 1280px) {
478
+ }
479
+ @media (min-width: 1536px) {
480
+ }
481
+
482
+ ```
483
+
484
+
485
+ </CH.Spotlight>
486
+
487
+ #### `@apply`
488
+
489
+ 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 the styles we write.
490
+
491
+ `@apply` can be used in CSS, Less, and Sass.
492
+
493
+ ``` css
494
+ .btn {
495
+ @apply font-bold py-2 px-4 rounded;
496
+ }
497
+ ```
498
+
499
+ However, there are some things to keep in mind when using Less and Sass.
500
+
501
+ ##### Sass
502
+
503
+ When using Tailwind with Sass, the presence of `!important` after `@apply` requires interpolation to get Sass to compile correctly.
504
+
505
+ <CH.Spotlight>
506
+
507
+ ``` sass
508
+ .alert {
509
+ @apply bg-red-500 !important;
510
+ }
511
+ ```
512
+ ---
513
+
514
+ It does not work properly.
515
+
516
+ ``` sass
517
+ ```
518
+ ---
519
+
520
+ Can work properly.
521
+
522
+ ``` sass
523
+ .alert {
524
+ @apply bg-red-500 #{!important};
525
+ }
526
+ ```
527
+
528
+ </CH.Spotlight>
529
+
530
+ ##### Less
531
+
532
+ When using Tailwind with Less, you cannot nest Tailwind's `@screen` directive.
533
+
534
+ <CH.Spotlight>
535
+
536
+ ``` less
537
+ .card {
538
+ @apply rounded-none;
539
+
540
+ @screen sm {
541
+ @apply rounded-lg;
542
+ }
543
+ }
544
+ ```
545
+ ---
546
+
547
+ It does not work properly.
548
+
549
+ ``` less
550
+ ```
551
+ ---
552
+
553
+ Instead, use regular media queries and the `theme()` function to reference your screen size, or simply don't nest your `@screen` directive.
554
+
555
+ <CH.Code>
556
+
557
+ ``` less Method One
558
+ // Use a regular media query and theme()
559
+ .card {
560
+ @apply rounded-none;
561
+
562
+ @media (min-width: theme('screens.sm')) {
563
+ @apply rounded-lg;
564
+ }
565
+ }
566
+ ```
567
+
568
+ ---
569
+
570
+ ``` less Method Two
571
+ // Use the @screen directive at the top-level
572
+ .card {
573
+ @apply rounded-none;
574
+
575
+ @media (min-width: theme('screens.sm')) {
576
+ @apply rounded-lg;
577
+ }
578
+ }
579
+ ```
580
+
581
+ </CH.Code>
582
+
583
+ </CH.Spotlight>
584
+
585
+ #### Recommended method
586
+
587
+ **It is recommended to develop styles in the way specified by `@apply`, so that only styles inlined by directives are included in the style product. **
588
+
589
+ When adding styles using HTML class names, by default Tailwind will not only add the styles corresponding to its own class name to the product, but will also have additional style code that may not affect its own styles.
590
+
591
+ #### The difference between bundle and bundleless build products
592
+
593
+ For the following code, there is a big difference between the bundle and bundleless modes of building products.
594
+
595
+ > The so-called bundle and bundleless can be found in [[Bundle and Bundleless]](/en/guide/advance/in-depth-about-build#bundle- and-bundleless)
596
+
597
+
598
+ ``` tsx ./src/index.tsx
599
+ import 'tailwindcss/utilities.css';
600
+
601
+ export default () => {
602
+ return (
603
+ <div className="bg-black text-white">hello world11</div>
604
+ );
605
+ }
606
+ ```
607
+
608
+ In Bundle mode, third-party dependencies are packaged in.
609
+
610
+ For styles, a separate product file is generated, and there is no code related to importing styles in the Js product file.
611
+
612
+ If you need to inject styles into Js products, you can enable the [`style.inject` API](/en/api/build-config#inject).
613
+
614
+ <CH.Code>
615
+ ```css ./dist/es/index.css
616
+ /* ../../node_modules/.pnpm/tailwindcss@2.2.19/node_modules/tailwindcss/utilities.css */
617
+ .table {
618
+ display: table;
619
+ }
620
+ @keyframes spin {
621
+ to {
622
+ transform: rotate(360deg);
623
+ }
624
+ }
625
+ @keyframes ping {
626
+ 75%, 100% {
627
+ transform: scale(2);
628
+ opacity: 0;
629
+ }
630
+ }
631
+ @keyframes pulse {
632
+ 50% {
633
+ opacity: .5;
634
+ }
635
+ }
636
+ @keyframes bounce {
637
+ 0%, 100% {
638
+ transform: translateY(-25%);
639
+ animation-timing-function: cubic-bezier(0.8, 0, 1, 1);
640
+ }
641
+ 50% {
642
+ transform: none;
643
+ animation-timing-function: cubic-bezier(0, 0, 0.2, 1);
644
+ }
645
+ }
646
+ .bg-black {
647
+ --tw-bg-opacity: 1;
648
+ background-color: rgba(0, 0, 0, var(--tw-bg-opacity));
649
+ }
650
+ .text-white {
651
+ --tw-text-opacity: 1;
652
+ color: rgba(255, 255, 255, var(--tw-text-opacity));
653
+ }
654
+ *,
655
+ ::before,
656
+ ::after {
657
+ --tw-shadow: 0 0 #0000 ;
658
+ }
659
+ *,
660
+ ::before,
661
+ ::after {
662
+ --tw-ring-inset: var(--tw-empty, );
663
+ --tw-ring-offset-width: 0px;
664
+ --tw-ring-offset-color: #fff;
665
+ --tw-ring-color: rgba(59, 130, 246, 0.5);
666
+ --tw-ring-offset-shadow: 0 0 #0000;
667
+ --tw-ring-shadow: 0 0 #0000 ;
668
+ }
669
+ @media (min-width: 640px) {
670
+ }
671
+ @media (min-width: 768px) {
672
+ }
673
+ @media (min-width: 1024px) {
674
+ }
675
+ @media (min-width: 1280px) {
676
+ }
677
+ @media (min-width: 1536px) {
678
+ }
679
+ ```
680
+ ---
681
+ ``` js ./dist/es/index.js
682
+ // src/index.tsx
683
+ import { jsx } from "react/jsx-runtime";
684
+ var src_default = () => {
685
+ return /* @__PURE__ */ jsx("div", {
686
+ className: "bg-black text-white",
687
+ children: "hello world11"
688
+ });
689
+ };
690
+ export {
691
+ src_default as default
692
+ };
693
+ ```
694
+ </CH.Code>
695
+
696
+ In Bundleless mode, no third-party dependencies are packaged in, and no style products are generated at this time.
697
+
698
+ ```js ./dist/es/index.js
699
+ import { jsx } from "react/jsx-runtime";
700
+ import "tailwindcss/utilities.css";
701
+ var src_default = () => {
702
+ return /* @__PURE__ */ jsx("div", {
703
+ className: "bg-black text-white",
704
+ children: "hello world11"
705
+ });
706
+ };
707
+ export {
708
+ src_default as default
709
+ };
710
+ ```
711
+
712
+ ### CSS Modules
713
+
714
+ Module projects support the development of styles using CSS Modules. By default, the following files are recognized as CSS Module files.
715
+
716
+ * `.module.css`
717
+ * `.module.less`
718
+ * `.module.scss`
719
+ * `.module.sass`
720
+
721
+ If you need to configure CSS Modules, you can check out the API at
722
+
723
+ * [autoModules](en/api/build-config#automodules)
724
+ * [modules](/en/api/build-config#modules)
725
+
726
+ The following is a code example.
727
+
728
+ <CH.Code>
729
+
730
+ ``` tsx ./src/index.tsx
731
+ import style from './index.module.css';
732
+
733
+ export default () => {
734
+ return (
735
+ <div className={style.btn}>hello world</div>
736
+ );
737
+ }
738
+ ```
739
+
740
+ ``` css ./src/index.module.css
741
+ .btn {
742
+ color: blue;
743
+ }
744
+ ```
745
+ </CH.Code>
746
+
747
+ ## Configuring build products
748
+
749
+ Based on most scenarios of component project usage, **it is recommended to use the `npm-component` build preset**. This preset yields a product directory structure of
750
+
751
+ ```bash
752
+ .
753
+ ├── dist
754
+ │ ├── es
755
+ │ ├── lib
756
+ │ └── types
757
+ ```
758
+
759
+ * `. /dist/es`: Contains bundleless products in ES modules format that support the es6 syntax.
760
+ * `. /dist/lib`: Contains bundleless products in CommonJS format with support for es6 syntax.
761
+ * `. /dist/types`: Contains the types file.
762
+
763
+ The [`buildPreset`](/en/api/build-preset) can be configured manually if there is a requirement to use syntax support, and supports modifying the supported syntax by adding a suffix to `npm-component`.
764
+
765
+ ``` tsx
766
+ export default defineConfig({
767
+ buildPreset: 'npm-component-es2019',
768
+ });
769
+ ```
770
+
771
+ If you have special needs for the build product directory structure, you can use the [`buildConfig` API](/en/api/build-config), which can be used by the following documentation.
772
+
773
+ * [modify-output-product](/en/guide/basic/modify-output-product#build-configuration-object)
774
+ * [in-depth-about-build](/en/guide/advance/in-depth-about-build)
775
+
776
+ ## Testing components
777
+
778
+ For more information on how to test components, please refer to [[Test project]](/en/guide/basic/test-your-project).
779
+
780
+
781
+ ## Releasing components
782
+
783
+ It is recommended to use module project to provide version publishing function, you can refer to [[Versioning and publishing]](/en/guide/basic/publish-your-project).