@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.
Files changed (39) hide show
  1. package/.eslintrc.js +1 -1
  2. package/CHANGELOG.md +11 -0
  3. package/docs/en/api/config/build-config.mdx +17 -16
  4. package/docs/en/api/config/build-preset.mdx +11 -72
  5. package/docs/en/guide/advance/asset.mdx +1 -1
  6. package/docs/en/guide/advance/copy.md +17 -18
  7. package/docs/en/guide/advance/external-dependency.mdx +1 -1
  8. package/docs/en/guide/advance/in-depth-about-build.md +27 -28
  9. package/docs/en/guide/advance/in-depth-about-dev-command.md +1 -1
  10. package/docs/en/guide/basic/before-getting-started.md +1 -1
  11. package/docs/en/guide/basic/modify-output-product.md +44 -70
  12. package/docs/en/guide/basic/test-your-project.mdx +0 -2
  13. package/docs/en/guide/basic/use-micro-generator.md +12 -37
  14. package/docs/en/guide/basic/using-storybook.mdx +15 -0
  15. package/docs/en/guide/best-practices/components.mdx +5 -198
  16. package/docs/en/guide/best-practices/use-tailwindcss.mdx +289 -0
  17. package/docs/en/plugins/guide/setup-function.mdx +0 -1
  18. package/docs/zh/api/config/build-config.mdx +19 -20
  19. package/docs/zh/api/config/build-preset.mdx +12 -69
  20. package/docs/zh/guide/advance/asset.mdx +3 -6
  21. package/docs/zh/guide/advance/copy.md +0 -2
  22. package/docs/zh/guide/advance/external-dependency.mdx +1 -1
  23. package/docs/zh/guide/advance/in-depth-about-build.md +30 -56
  24. package/docs/zh/guide/basic/before-getting-started.md +1 -1
  25. package/docs/zh/guide/basic/modify-output-product.md +46 -69
  26. package/docs/zh/guide/basic/use-micro-generator.md +13 -33
  27. package/docs/zh/guide/basic/using-storybook.mdx +16 -5
  28. package/docs/zh/guide/best-practices/components.mdx +1 -196
  29. package/docs/zh/guide/best-practices/use-tailwindcss.mdx +289 -0
  30. package/docs/zh/plugins/guide/setup-function.mdx +0 -1
  31. package/package.json +7 -6
  32. package/rspress.config.ts +130 -0
  33. package/theme/index.ts +3 -2
  34. package/tsconfig.json +1 -1
  35. package/docs/en/api/config/design-system.md +0 -1166
  36. package/docs/en/guide/advance/theme-config.mdx +0 -60
  37. package/docs/zh/api/config/design-system.md +0 -1166
  38. package/docs/zh/guide/advance/theme-config.mdx +0 -64
  39. package/modern.config.ts +0 -124
@@ -23,7 +23,7 @@ export default defineConfig({
23
23
 
24
24
  **The default output files has the following characteristics**.
25
25
 
26
- - will generate [CommonJS](https://nodejs.org/api/modules.html#modules-commonjs-modules) and [ESM](https://nodejs.org/api/esm.html#modules- ecmascript-modules).
26
+ - will generate [CommonJS](https://nodejs.org/api/modules.html#modules-commonjs-modules) and [ESM](https://nodejs.org/api/esm.html#modules-ecmascript-modules).
27
27
  - The code syntax is supported up to `ES6` , and more advanced syntax will be converted.
28
28
  - All code is bundled into one file, i.e. **bundle** processing is performed.
29
29
  - The output root directory is the `dist` directory under the project, and the type file output directory is `dist/types`.
@@ -37,62 +37,18 @@ Then the next step is to first explain `buildPreset`.
37
37
 
38
38
  ## buildPreset
39
39
 
40
- The `buildPreset` represents one or more sets of build-related configurations prepared in advance. By using the corresponding preset values of the `buildPreset`, you can eliminate the troublesome and complicated configuration work and get the expected product.
40
+ The `buildPreset` represents a prepared set or sets of build-related configurations that can be used to eliminate the trouble and complexity of configuration by using the default values corresponding to the build Preset, resulting in the expected product.
41
41
 
42
- ### String form of build preset
42
+ Module Tools mainly comes with two built-in build presets, including:
43
43
 
44
- The value of a **build preset can be in the form of a string**, so a build preset of this form is called a preset string.
44
+ - npm-component: Used to build component libraries.
45
+ - npm-library: Used to package projects of other library types, such as tool libraries.
45
46
 
46
- The Module Tools provides generic build preset strings and corresponding variants, depending on the generic scenario in which the npm package is used. All currently supported preset strings can be viewed via the [BuildPreset API](/api/config/build-preset). Here is an explanation about the relationship between **generic preset strings and variants**.
47
+ It also provides some variations, such as `npm-library-with-umd` and `npm-library-es5`, which, as their names suggest, correspond to library presets with umd output and support for es5 syntax, respectively. For more detailed configuration, you can refer to its [API](/api/config/build-preset).
47
48
 
48
- Among the generic preset strings, `"npm-library"` can be used in the scenario of developing npm packages of the library type, which is suitable for most common module type projects. When `"npm-library"` is set, the output files of the project will have the following characteristics.
49
+ In addition, we can also fully customize the build configuration:
49
50
 
50
- - In the `dist/lib` directory you will get code formatted as `cjs`, syntax supported up to `es6` and bundled.
51
- - In the `dist/es` directory, you get code in the format `esm`, with syntax support up to `es6` and after bundling.
52
- - In the `dist/types` directory, you get the type files. If it is not a TypeScript project, there is no such directory.
53
-
54
- The default string `"npm-library"` is a variant of the original product with a modified **code syntax support** feature and a string naming change to `"npm-library-[es5 | es2016.... . es2020 | esnext]"`.
55
-
56
- For example, if the output file is based on the preset string `"npm-library"` and the syntax supported by the output code is changed to `es5`, then simply changing `"npm-library"` to `"npm-library-es5"` would be sufficient.
57
-
58
- ### Build pre-defined function forms
59
-
60
- **In addition to the string form, the value of a build preset can also be in the form of a function, where the specific configuration corresponding to a preset value can be printed or modified**.
61
-
62
- For example, to achieve the same effect as the preset string ``npm-library-es5"` using the form of a preset function, you can do the following.
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
- In the above code implementation, `preset.NPM_LIBRARY` corresponds to the preset string `"npm-library"`, representing multiple build-related configurations equivalent to `"npm-library"`.
78
-
79
- We use the `map` method to iterate over the `NPM_LIBRARY` array, which contains multiple `buildConfig` objects. We perform a shallow copy of the original `buildConfig` object and modify the value of the `target` property in the shallow copy to be `es5`.
80
-
81
- If you want to know the specific contents included in `preset.NPM_LIBRARY`, you can refer to the [BuildPreset API](/api/config/build-preset).
82
-
83
- In addition, under the `preset` object, it not only includes `NPM_LIBRARY`, but also other similar constants.
84
-
85
- > NPM_LIBRARY`, you can check it with [BuildPreset API](/api/config/build-preset). The`preset`object contains not only`NPM_LIBRARY`, but also other similar constants.
86
-
87
- :::tip
88
- We can not only use `preset.NPM_LIBRARY`to get the build configuration corresponding to`"npm-library"`, but also `preset['npm-library']` in this way.
89
- :::
90
-
91
- So what is the `buildConfig` object here? What is the basis for the build artifacts feature mentioned before?
92
-
93
- Let's explain next.
94
-
95
- ## Build configuration (object)
51
+ ## buildConfig
96
52
 
97
53
  **`buildConfig` is a configuration option that describes how to compile and generate build artifacts**. What was mentioned at the beginning about "_features of build products_" are actually properties supported by `buildConfig`. The currently supported properties cover the needs of most module type projects when building products. `buildConfig` not only contains some properties that artifacts have, but also contains some features needed to build products. The following is a brief list from a classification point of view.
98
54
 
@@ -129,36 +85,54 @@ Let's explain next.
129
85
 
130
86
  In addition to the above categories, frequently asked questions and best practices about these APIs can be found at the following links
131
87
 
132
- - [What are `bundle` and `bundleless`?](/guide/advance/in-depth-about-build#bundle- and-bundleless)
133
- - [Relationship of `input` to `sourceDir`](/guide/advance/in-depth-about-build#input- to -sourcedir-)
134
- - [Multiple ways to generate type files in product](/guide/advance/in-depth-about-build#type-files).
135
- - [`buildConfig.define` Different ways to use it for different scenarios.](/guide/advance/in-depth-about-build#buildconfigdefine - How to use it for different scenarios)
88
+ - [About `bundle` and `bundleless`?](/guide/advance/in-depth-about-build#bundle--bundleless)
89
+ - [About `input` and `sourceDir`](/guide/advance/in-depth-about-build#input--sourcedir)
90
+ - [About d.ts](/guide/advance/in-depth-about-build#dts).
91
+ - [How to use define](/guide/advance/in-depth-about-build#define)
136
92
  - [How to handle third-party dependencies?](/guide/advance/external-dependency)
137
93
  - [How to use copy?](/guide/advance/copy)
138
- - How do I build umd artifacts? (/guide/advance/build-umd# sets the global variable name of the project)
139
- - [The capabilities currently supported by static resources.](/guide/advance/asset)
94
+ - [How to use umd](/guide/advance/build-umd)
95
+ - [How to use asset](/guide/advance/asset)
96
+
97
+ ## Combining Configuration and Presets
98
+
99
+ Once we understand `buildPreset` and `buildConfig`, we can use them together.
140
100
 
141
- ## When to use `buildConfig`
101
+ In a real project, we can use `buildPreset` to quickly get a set of default build configurations. If you need to customise it, you can use `buildConfig` to override and extend it.
142
102
 
143
- `buildConfig` is one of the methods used to modify the product, **only `buildConfig` will take effect when configured in conjunction with `buildPreset`**. So if configured as follows.
103
+ The extension logic is as follows.
104
+
105
+ - When `buildConfig` is an array, new configuration items are added to the original preset.
144
106
 
145
107
  ```ts title="modern.config.ts"
146
- import { defineConfig } from '@modern-js/module-tools';
108
+ import { moduleTools, defineConfig } from '@modern-js/module-tools';
147
109
 
148
110
  export default defineConfig({
149
- buildConfig: {
150
- format: 'umd',
151
- },
111
+ plugins: [moduleTools()],
152
112
  buildPreset: 'npm-library',
113
+ buildConfig: [
114
+ {
115
+ format: 'iife',
116
+ target: 'es2020',
117
+ outDir: '. /dist/global'
118
+ }
119
+ ]
153
120
  });
154
121
  ```
155
122
 
156
- Then at this point you will see the following prompt.
123
+ This will generate an additional IIFE-formatted product that supports up to ES2020 syntax on top of the original preset, in the `dist/global` directory under the project.
157
124
 
158
- ```bash
159
- Since both 'buildConfig' and 'buildPreset' are present, only the 'buildConfig' configuration will take effect
160
- ```
125
+ - When `buildConfig` is an object, the configuration items in the object are overwritten in the preset.
161
126
 
162
- The set or sets of build-related configurations represented by `buildPreset` are composed of `buildConfig`, **which can be used to customize output products** when the current project needs cannot be met using `buildPreset`.
127
+ ```ts title="modern.config.ts"
128
+ import { moduleTools, defineConfig } from '@modern-js/module-tools';
129
+ export default defineConfig({
130
+ plugins: [moduleTools()],
131
+ buildPreset: 'npm-library',
132
+ buildConfig: {
133
+ sourceMap: true,
134
+ },
135
+ }).
136
+ ```
163
137
 
164
- The process of using `buildConfig` is the process of thinking about **what kind of build artifacts to get**.
138
+ This will cause a sourceMap file to be generated for each build task.
@@ -79,8 +79,6 @@ npm run test
79
79
 
80
80
  ### Components
81
81
 
82
- {/* 链接待补充 */}
83
-
84
82
  For components, Modern.js's Runtime API provides functionality for testing UI components, which is provided by `@modern-js/runtime/testing`.
85
83
 
86
84
  :::tip
@@ -18,20 +18,18 @@ The microgenerator can be started via [`modern new`](/guide/basic/command-previe
18
18
 
19
19
  When we want to test some modules, we can enable the test feature. When this feature is enabled, **a `tests` directory and related files will be created in the project directory, and a new `"@modern-js/plugin-testing"` dependency will be added to package.json**.
20
20
 
21
-
22
21
  :::tip
23
22
  After successfully enabling it, you will be prompted to manually add a code similar to the one below to the configuration.
23
+
24
24
  ```ts
25
25
  import { moduleTools, defineConfig } from '@modern-js/module-tools';
26
26
  import { testingPlugin } from '@modern-js/plugin-testing';
27
27
 
28
28
  export default defineConfig({
29
- plugins: [
30
- moduleTools(),
31
- testingPlugin(),
32
- ],
29
+ plugins: [moduleTools(), testingPlugin()],
33
30
  });
34
31
  ```
32
+
35
33
  :::
36
34
 
37
35
  ## Storybook
@@ -40,17 +38,16 @@ The **Storybook feature** can be enabled when we want to debug a component or a
40
38
 
41
39
  :::tip
42
40
  After successfully enabling it, you will be prompted to manually add a code similar to the one below to the configuration.
41
+
43
42
  ```ts
44
43
  import { moduleTools, defineConfig } from '@modern-js/module-tools';
45
44
  import { storybookPlugin } from '@modern-js/plugin-storybook';
46
45
 
47
46
  export default defineConfig({
48
- plugins: [
49
- moduleTools(),
50
- storybookPlugin(),
51
- ],
47
+ plugins: [moduleTools(), storybookPlugin()],
52
48
  });
53
49
  ```
50
+
54
51
  :::
55
52
 
56
53
  For more information on how to start Storybook and how to use it, check out the following link.
@@ -58,50 +55,28 @@ For more information on how to start Storybook and how to use it, check out the
58
55
  - [`modern dev`](/en/guide/basic/command-preview#modern-dev)
59
56
  - [`using Storybook`](/en/guide/basic/using-storybook)
60
57
 
61
- ## Tailwind CSS support
58
+ ## Tailwind CSS Support
62
59
 
63
- This can be enabled when we want to add [Tailwind CSS](https://v2.tailwindcss.com/) support to our project. Tailwind CSS is a CSS library that provides out-of-the-box styling.
64
-
65
- For more information on how to use Tailwind CSS in your module projects, check out.
66
-
67
- <!-- 链接待补充 -->
68
-
69
- - Using Tailwind CSS
70
-
71
- :::tip
72
- After successfully enabling it, you will be prompted to manually add a code similar to the one below to the configuration.
73
- ```ts
74
- import { moduleTools, defineConfig } from '@modern-js/module-tools';
75
- import { tailwindcssPlugin } from '@modern-js/plugin-tailwindcss';
60
+ [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.
76
61
 
77
- export default defineConfig({
78
- plugins: [
79
- moduleTools(),
80
- tailwindPlugin(),
81
- ],
82
- });
83
- ```
84
- :::
62
+ If you want to use Tailwind CSS for a project, you can refer to ["Using Tailwind CSS"](https://modernjs.dev/module-tools/guide/best-practices/components.html#tailwind-css).
85
63
 
86
64
  ## Modern.js Runtime API
87
65
 
88
- <!-- 链接待补充 -->
89
-
90
66
  **Modern.js provides Runtime API capabilities that can only be used in the Modern.js application project environment**. If you need to develop a component for use in a Modern.js application environment, then you can turn on this feature and the microgenerator will add the `"@modern-js/runtime"` dependency.
91
67
 
92
68
  Also, the Storybook debugging tool will determine if the project needs to use the Runtime API by checking the project's dependencies and providing the same Runtime API runtime environment as the Modern.js application project.
93
69
 
94
70
  :::tip
95
71
  After successfully enabling it, you will be prompted to manually add a code similar to the one below to the configuration.
72
+
96
73
  ```ts
97
74
  import { moduleTools, defineConfig } from '@modern-js/module-tools';
98
75
  import runtime from '@modern-js/runtime/cli';
99
76
 
100
77
  export default defineConfig({
101
- plugins: [
102
- moduleTools(),
103
- runtime(),
104
- ],
78
+ plugins: [moduleTools(), runtime()],
105
79
  });
106
80
  ```
81
+
107
82
  :::
@@ -166,3 +166,18 @@ For the `modern build --platform` command you can see.
166
166
  - [`modern build`](/en/guide/basic/command-preview#modern-build)
167
167
 
168
168
  After the build is complete, you can see the build artifacts files in the `dist/storybook-static` directory.
169
+
170
+ ## Use Tailwind CSS with Storybook
171
+
172
+ If you need to use Tailwind CSS in the `stories` directory, make sure that the Tailwind CSS configuration for your current project includes the `stories` directory.
173
+
174
+ Taking the `tailwind.config.ts` file as an example, you need to configure the following content:
175
+
176
+ ```diff title="tailwind.config.ts"
177
+ export default {
178
+ content: [
179
+ './src/**/*.{js,jsx,ts,tsx}',
180
+ + './stories/**/*.{js,jsx,ts,tsx}',
181
+ ],
182
+ };
183
+ ```
@@ -1,3 +1,7 @@
1
+ ---
2
+ sidebar_position: 1
3
+ ---
4
+
1
5
  # Developing Components
2
6
 
3
7
  This chapter will describe how to develop component projects using the Module Tools.
@@ -164,204 +168,7 @@ body {
164
168
 
165
169
  ### Tailwind CSS
166
170
 
167
- The module project supports the development of component styles using Tailwind CSS.
168
-
169
- By default, this feature is not enabled in the module project, you need to enable it as follows.
170
-
171
- 1. The Tailwind CSS feature can be enabled by executing the `new` command in the project root directory.
172
-
173
- ```text title="Terminal"
174
- pnpm run new
175
-
176
- ? Please select the operation you want: Enable features
177
- ? Please select the feature name: Enable Tailwind CSS
178
- ```
179
-
180
- 2. Once successfully enabled, you will see that a new dependency has been added to `package.json`.
181
-
182
- ```json title="./package.json"
183
- {
184
- "devDependencies": {
185
- "@modern-js/plugin-tailwindcss": "x.y.z"
186
- }
187
- }
188
- ```
189
-
190
- 3. Tailwind CSS offers two ways to use it.
191
-
192
- #### HTML class
193
-
194
- Tailwind CSS supports adding styles to HTML tags by using class names. **When using HTML class names, be sure to import the Tailwind CSS equivalent css file. **
195
-
196
- ```tsx title="./src/index.tsx"
197
- import 'tailwindcss/utilities.css';
198
-
199
- export default () => {
200
- return <div className="bg-black text-white">hello world</div>;
201
- };
202
- ```
203
-
204
- Style product (This is a bundle build):
205
-
206
- ```css title="./dist/es/index.css"
207
- /* node_modules/tailwindcss/utilities.css */
208
- .table {
209
- display: table;
210
- }
211
- .bg-black {
212
- --tw-bg-opacity: 1;
213
- background-color: rgba(0, 0, 0, var(--tw-bg-opacity));
214
- }
215
- .text-white {
216
- --tw-text-opacity: 1;
217
- color: rgba(255, 255, 255, var(--tw-text-opacity));
218
- }
219
- /** some more... */
220
- ```
221
-
222
- #### `@apply`
223
-
224
- 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.
225
-
226
- `@apply` can be used in CSS, Less, and Sass.
227
-
228
- ```css
229
- .btn {
230
- @apply font-bold py-2 px-4 rounded;
231
- }
232
- ```
233
-
234
- However, there are some things to keep in mind when using Less and Sass.
235
-
236
- ##### Sass
237
-
238
- When using Tailwind with Sass, the presence of `!important` after `@apply` requires interpolation to get Sass to compile correctly.
239
-
240
- - It does not work properly:
241
-
242
- ```sass
243
- .alert {
244
- @apply bg-red-500 !important;
245
- }
246
- ```
247
-
248
- - Can work properly:
249
-
250
- ```sass
251
- .alert {
252
- @apply bg-red-500 #{!important};
253
- }
254
- ```
255
-
256
- ##### Less
257
-
258
- When using Tailwind with Less, you cannot nest Tailwind's `@screen` directive.
259
-
260
- - It does not work properly:
261
-
262
- ```less
263
- .card {
264
- @apply rounded-none;
265
-
266
- @screen sm {
267
- @apply rounded-lg;
268
- }
269
- }
270
- ```
271
-
272
- - Instead, use regular media queries and the `theme()` function to reference your screen size, or simply don't nest your `@screen` directive.
273
-
274
- ```less title="Method One"
275
- // Use a regular media query and theme()
276
- .card {
277
- @apply rounded-none;
278
-
279
- @media (min-width: theme('screens.sm')) {
280
- @apply rounded-lg;
281
- }
282
- }
283
- ```
284
-
285
- ```less title="Method Two"
286
- // Use the @screen directive at the top-level
287
- .card {
288
- @apply rounded-none;
289
-
290
- @media (min-width: theme('screens.sm')) {
291
- @apply rounded-lg;
292
- }
293
- }
294
- ```
295
-
296
- #### Recommended method
297
-
298
- **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. **
299
-
300
- 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.
301
-
302
- #### The difference between bundle and bundleless build products
303
-
304
- For the following code, there is a big difference between the bundle and bundleless modes of building products.
305
-
306
- > The so-called bundle and bundleless can be found in ["Bundle and Bundleless"](/en/guide/advance/in-depth-about-build#bundle- and-bundleless)
307
-
308
- ```tsx title="./src/index.tsx"
309
- import 'tailwindcss/utilities.css';
310
-
311
- export default () => {
312
- return <div className="bg-black text-white">hello world11</div>;
313
- };
314
- ```
315
-
316
- In Bundle mode, third-party dependencies are bundled in.
317
-
318
- For styles, a separate output file is generated, and there is no code related to importing styles in the JS output files.
319
-
320
- If you need to inject styles into JS output files, you can enable the [`style.inject`](/en/api/config/build-config#styleinject) option.
321
-
322
- ```css title="./dist/es/index.css"
323
- /* node_modules/tailwindcss/utilities.css */
324
- .table {
325
- display: table;
326
- }
327
- .bg-black {
328
- --tw-bg-opacity: 1;
329
- background-color: rgba(0, 0, 0, var(--tw-bg-opacity));
330
- }
331
- .text-white {
332
- --tw-text-opacity: 1;
333
- color: rgba(255, 255, 255, var(--tw-text-opacity));
334
- }
335
- /** some more... */
336
- ```
337
-
338
- ``` js ./dist/es/index.js
339
- // src/index.tsx
340
- import { jsx } from "react/jsx-runtime";
341
- var src_default = () => {
342
- return /* @__PURE__ */ jsx("div", {
343
- className: "bg-black text-white",
344
- children: "hello world11"
345
- });
346
- };
347
- export {
348
- src_default as default
349
- };
350
- ```
351
-
352
- In Bundleless mode, no third-party dependencies are bundled, and no style artifacts are generated at this time.
353
-
354
- ```js title="./dist/es/index.js"
355
- import { jsx } from 'react/jsx-runtime';
356
- import 'tailwindcss/utilities.css';
357
- var src_default = () => {
358
- return /* @__PURE__ */ jsx('div', {
359
- className: 'bg-black text-white',
360
- children: 'hello world11',
361
- });
362
- };
363
- export { src_default as default };
364
- ```
171
+ Please refer to ["Using Tailwind CSS"](/guide/best-practices/use-tailwindcss.html) for detailed usage.
365
172
 
366
173
  ### CSS Modules
367
174