@modern-js/module-tools-docs 2.0.0-beta.4
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/LICENSE +144 -0
- package/docs/.island/config.ts +245 -0
- package/docs/.island/dist/404.html +41 -0
- package/docs/.island/dist/assets/before-getting-started.1b82b538.js +87 -0
- package/docs/.island/dist/assets/before-getting-started.582a31cc.js +87 -0
- package/docs/.island/dist/assets/build-config.72eb0918.js +804 -0
- package/docs/.island/dist/assets/build-config.d8bb1658.js +809 -0
- package/docs/.island/dist/assets/build-preset.96805d7d.js +256 -0
- package/docs/.island/dist/assets/build-preset.c20dcd40.js +256 -0
- package/docs/.island/dist/assets/build-your-ui.7f349247.js +2 -0
- package/docs/.island/dist/assets/build-your-ui.a8361604.js +2 -0
- package/docs/.island/dist/assets/command-preview.2d45fc82.js +264 -0
- package/docs/.island/dist/assets/command-preview.dc51b953.js +264 -0
- package/docs/.island/dist/assets/components.esm.03560353.js +9 -0
- package/docs/.island/dist/assets/design-system.86694ff5.js +1254 -0
- package/docs/.island/dist/assets/design-system.c4745cce.js +639 -0
- package/docs/.island/dist/assets/dev.1d326a37.js +37 -0
- package/docs/.island/dist/assets/dev.1fd06000.js +37 -0
- package/docs/.island/dist/assets/down.f35427d3.svg +1 -0
- package/docs/.island/dist/assets/extension.12299fd6.js +2 -0
- package/docs/.island/dist/assets/extension.96dc63a4.js +2 -0
- package/docs/.island/dist/assets/getting-started.40e9218d.js +117 -0
- package/docs/.island/dist/assets/getting-started.b1ed3f10.js +114 -0
- package/docs/.island/dist/assets/github.3bf8ccee.svg +1 -0
- package/docs/.island/dist/assets/index.2b2347ea.js +33 -0
- package/docs/.island/dist/assets/index.6cef6f5f.js +4 -0
- package/docs/.island/dist/assets/index.cb118238.js +36 -0
- package/docs/.island/dist/assets/index.ccb6ce27.js +4 -0
- package/docs/.island/dist/assets/island_inject.11a12ecc.js +1 -0
- package/docs/.island/dist/assets/island_inject.b13deaee.js +1 -0
- package/docs/.island/dist/assets/loading.8c9bb911.svg +1 -0
- package/docs/.island/dist/assets/modify-output-product.7f6bff35.js +100 -0
- package/docs/.island/dist/assets/modify-output-product.b91eff1f.js +100 -0
- package/docs/.island/dist/assets/moon.6b705924.svg +3 -0
- package/docs/.island/dist/assets/plugin.895932d8.js +42 -0
- package/docs/.island/dist/assets/plugin.d2fbc531.js +42 -0
- package/docs/.island/dist/assets/publish-your-project.21b8309f.js +164 -0
- package/docs/.island/dist/assets/publish-your-project.8d398b17.js +166 -0
- package/docs/.island/dist/assets/right.89674cd7.svg +1 -0
- package/docs/.island/dist/assets/search.0aea6901.svg +1 -0
- package/docs/.island/dist/assets/search.1c85d17c.js +3 -0
- package/docs/.island/dist/assets/search.484eca11.js +222 -0
- package/docs/.island/dist/assets/search.54fca8d0.js +3 -0
- package/docs/.island/dist/assets/style.09015a4b.css +1 -0
- package/docs/.island/dist/assets/style.2e5f7bc2.css +1970 -0
- package/docs/.island/dist/assets/sun.841dac10.svg +11 -0
- package/docs/.island/dist/assets/test-your-project.18bd4582.js +190 -0
- package/docs/.island/dist/assets/test-your-project.f53bebf7.js +190 -0
- package/docs/.island/dist/assets/test.0da1f99f.js +67 -0
- package/docs/.island/dist/assets/test.0e81f002.js +66 -0
- package/docs/.island/dist/assets/translator.b1077c44.svg +1 -0
- package/docs/.island/dist/assets/use-micro-generator.7d9e4016.js +60 -0
- package/docs/.island/dist/assets/use-micro-generator.db5520c1.js +60 -0
- package/docs/.island/dist/assets/using-storybook.57ea6b77.js +260 -0
- package/docs/.island/dist/assets/using-storybook.a2212f2e.js +260 -0
- package/docs/.island/dist/assets/welcome.0449a9c8.js +13 -0
- package/docs/.island/dist/assets/welcome.a8448931.js +13 -0
- package/docs/.island/dist/assets/why-module-engineering-solution.6ae8c0e3.js +26 -0
- package/docs/.island/dist/assets/why-module-engineering-solution.c9a45cbd.js +26 -0
- package/docs/.island/dist/chunk-COLCRJ2V.js +1 -0
- package/docs/.island/dist/chunk-K5FMOYDC.js +10 -0
- package/docs/.island/dist/chunk-WE42KMYS.js +26 -0
- package/docs/.island/dist/client-entry.js +3 -0
- package/docs/.island/dist/en/api/build-config.html +344 -0
- package/docs/.island/dist/en/api/build-preset.html +82 -0
- package/docs/.island/dist/en/api/design-system.html +155 -0
- package/docs/.island/dist/en/api/dev.html +45 -0
- package/docs/.island/dist/en/api/index.html +41 -0
- package/docs/.island/dist/en/api/plugin.html +48 -0
- package/docs/.island/dist/en/api/test.html +58 -0
- package/docs/.island/dist/en/guide/before-getting-started.html +127 -0
- package/docs/.island/dist/en/guide/build-your-ui.html +41 -0
- package/docs/.island/dist/en/guide/command-preview.html +100 -0
- package/docs/.island/dist/en/guide/extension.html +41 -0
- package/docs/.island/dist/en/guide/getting-started.html +76 -0
- package/docs/.island/dist/en/guide/modify-output-product.html +140 -0
- package/docs/.island/dist/en/guide/publish-your-project.html +91 -0
- package/docs/.island/dist/en/guide/test-your-project.html +72 -0
- package/docs/.island/dist/en/guide/use-micro-generator.html +65 -0
- package/docs/.island/dist/en/guide/using-storybook.html +113 -0
- package/docs/.island/dist/en/guide/welcome.html +53 -0
- package/docs/.island/dist/en/guide/why-module-engineering-solution.html +49 -0
- package/docs/.island/dist/en/index.html +42 -0
- package/docs/.island/dist/react-dom.js +1 -0
- package/docs/.island/dist/react-dom_client.js +1 -0
- package/docs/.island/dist/react.js +1 -0
- package/docs/.island/dist/react_jsx-runtime.js +10 -0
- package/docs/.island/dist/ssr-manifest.json +57 -0
- package/docs/.island/dist/test-result.png +0 -0
- package/docs/.island/dist/why-module-solution.png +0 -0
- package/docs/.island/dist/zh/api/build-config.html +347 -0
- package/docs/.island/dist/zh/api/build-preset.html +82 -0
- package/docs/.island/dist/zh/api/design-system.html +149 -0
- package/docs/.island/dist/zh/api/dev.html +46 -0
- package/docs/.island/dist/zh/api/index.html +41 -0
- package/docs/.island/dist/zh/api/plugin.html +48 -0
- package/docs/.island/dist/zh/api/test.html +59 -0
- package/docs/.island/dist/zh/guide/before-getting-started.html +127 -0
- package/docs/.island/dist/zh/guide/build-your-ui.html +41 -0
- package/docs/.island/dist/zh/guide/command-preview.html +100 -0
- package/docs/.island/dist/zh/guide/extension.html +41 -0
- package/docs/.island/dist/zh/guide/getting-started.html +79 -0
- package/docs/.island/dist/zh/guide/modify-output-product.html +140 -0
- package/docs/.island/dist/zh/guide/publish-your-project.html +92 -0
- package/docs/.island/dist/zh/guide/test-your-project.html +72 -0
- package/docs/.island/dist/zh/guide/use-micro-generator.html +65 -0
- package/docs/.island/dist/zh/guide/using-storybook.html +114 -0
- package/docs/.island/dist/zh/guide/welcome.html +53 -0
- package/docs/.island/dist/zh/guide/why-module-engineering-solution.html +49 -0
- package/docs/.island/dist/zh/index.html +42 -0
- package/docs/.island/index.html +39 -0
- package/docs/.island/styles/index.css +10 -0
- package/docs/en/api/build-config.md +501 -0
- package/docs/en/api/build-preset.md +214 -0
- package/docs/en/api/design-system.md +524 -0
- package/docs/en/api/dev.md +32 -0
- package/docs/en/api/index.md +3 -0
- package/docs/en/api/plugin.md +34 -0
- package/docs/en/api/test.md +48 -0
- package/docs/en/guide/advance/asset.mdx +132 -0
- package/docs/en/guide/advance/build-umd.mdx +241 -0
- package/docs/en/guide/advance/copy.md +235 -0
- package/docs/en/guide/advance/external-dependency.mdx +125 -0
- package/docs/en/guide/advance/in-depth-about-build.md +266 -0
- package/docs/en/guide/advance/in-depth-about-dev-command.md +22 -0
- package/docs/en/guide/basic/before-getting-started.md +187 -0
- package/docs/en/guide/basic/command-preview.md +204 -0
- package/docs/en/guide/basic/modify-output-product.md +145 -0
- package/docs/en/guide/basic/publish-your-project.md +115 -0
- package/docs/en/guide/basic/test-your-project.mdx +158 -0
- package/docs/en/guide/basic/use-micro-generator.md +35 -0
- package/docs/en/guide/basic/using-storybook.mdx +187 -0
- package/docs/en/guide/intro/getting-started.md +78 -0
- package/docs/en/guide/intro/welcome.md +14 -0
- package/docs/en/guide/intro/why-module-engineering-solution.md +17 -0
- package/docs/en/index.md +35 -0
- package/docs/public/test-result.png +0 -0
- package/docs/public/why-module-solution.png +0 -0
- package/docs/zh/api/build-config.md +570 -0
- package/docs/zh/api/build-preset.md +220 -0
- package/docs/zh/api/design-system.md +1147 -0
- package/docs/zh/api/dev.md +33 -0
- package/docs/zh/api/index.md +3 -0
- package/docs/zh/api/plugins.md +108 -0
- package/docs/zh/api/testing.md +52 -0
- package/docs/zh/guide/advance/asset.mdx +132 -0
- package/docs/zh/guide/advance/build-umd.mdx +232 -0
- package/docs/zh/guide/advance/copy.md +235 -0
- package/docs/zh/guide/advance/external-dependency.mdx +125 -0
- package/docs/zh/guide/advance/in-depth-about-build.md +267 -0
- package/docs/zh/guide/advance/in-depth-about-dev-command.md +26 -0
- package/docs/zh/guide/basic/before-getting-started.md +187 -0
- package/docs/zh/guide/basic/command-preview.md +204 -0
- package/docs/zh/guide/basic/modify-output-product.md +144 -0
- package/docs/zh/guide/basic/publish-your-project.md +112 -0
- package/docs/zh/guide/basic/test-your-project.mdx +158 -0
- package/docs/zh/guide/basic/use-micro-generator.md +35 -0
- package/docs/zh/guide/basic/using-storybook.mdx +186 -0
- package/docs/zh/guide/intro/getting-started.md +78 -0
- package/docs/zh/guide/intro/welcome.md +14 -0
- package/docs/zh/guide/intro/why-module-engineering-solution.md +17 -0
- package/docs/zh/index.md +29 -0
- package/package.json +19 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# How to handle third-party dependencies
|
|
2
|
+
|
|
3
|
+
Generally, third-party dependencies required by a project can be installed via the `install` command in the package manager. After the third-party dependencies are successfully installed, they will generally appear under `dependencies` and `devDependencies` in the project `package.json`.
|
|
4
|
+
|
|
5
|
+
``` json pacakge.json
|
|
6
|
+
{
|
|
7
|
+
"dependencies": {},
|
|
8
|
+
"devDependencies": {}
|
|
9
|
+
}
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Dependencies under `"dependencies"` are generally related to project code and builds, and if these third-party dependencies are declared under `"devDependencies"`, then there will be missing dependencies in production environments.
|
|
13
|
+
|
|
14
|
+
In addition to `"dependencies"`, [`"peerDependencies"`](/en/guide/basic/before-getting-started#peerdependencies) can also declare dependencies that are needed in the production environment, but it puts more emphasis on the existence of these dependencies declared by `"peerDependencies"` in the project's runtime environment, similar to the plugin mechanism.
|
|
15
|
+
|
|
16
|
+
## Default handling of third-party dependencies
|
|
17
|
+
|
|
18
|
+
By default, third-party dependencies under `"dependencies"` and `"peerDependencies"` are not packaged in the module project**, so:
|
|
19
|
+
|
|
20
|
+
<CH.Spotlight>
|
|
21
|
+
|
|
22
|
+
```json ./v1/package.json
|
|
23
|
+
{
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"react": "^17"
|
|
26
|
+
},
|
|
27
|
+
// or
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"react": "^17"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
If the project has a dependency on `react`.
|
|
37
|
+
|
|
38
|
+
```json ./v1/package.json
|
|
39
|
+
{
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"react": "^17"
|
|
42
|
+
},
|
|
43
|
+
// or
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"react": "^17"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
When a `react` dependency is used in the source code.
|
|
53
|
+
|
|
54
|
+
``` tsx src/index.ts
|
|
55
|
+
import React from "react";
|
|
56
|
+
console.info(React);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
The `react` code is not packaged into the product at this point.
|
|
62
|
+
|
|
63
|
+
``` js dist/index.js
|
|
64
|
+
import React from "react";
|
|
65
|
+
console.info(React);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
</CH.Spotlight>
|
|
69
|
+
|
|
70
|
+
## Packaging third-party dependencies
|
|
71
|
+
|
|
72
|
+
However there are cases where you want to package these third-party dependencies into the product. The benefits of packaging third-party dependencies into the product are: **Reducing the time spent downloading third-party dependencies**.
|
|
73
|
+
|
|
74
|
+
This way of handling third-party dependencies is generally more common in developing command-line tools, which can improve the loading speed of command-line tools and give users a better experience.
|
|
75
|
+
|
|
76
|
+
So how do you enable the packaging of third-party dependencies in your module project?
|
|
77
|
+
|
|
78
|
+
The following APIs are provided in the build configuration to handle third-party dependencies:
|
|
79
|
+
|
|
80
|
+
* [`buildConfig.autoExternal`](/en/api/build-config#autoexternal)
|
|
81
|
+
* [`buildConfig.externals`](/en/api/build-config#externals)
|
|
82
|
+
|
|
83
|
+
### Turn off default behavior
|
|
84
|
+
|
|
85
|
+
When we want to turn off the default handling behavior for third-party dependencies, we can do so by:
|
|
86
|
+
|
|
87
|
+
``` ts
|
|
88
|
+
export default defineConfig({
|
|
89
|
+
buildConfig: {
|
|
90
|
+
autoExternal: false,
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
This way the dependencies under `"dependencies"` and `"peerDependencies"` will be packaged. If you want to turn off the processing of only one of these dependencies, you can use the
|
|
96
|
+
`buildConfig.autoExternal` in the form of an object.
|
|
97
|
+
|
|
98
|
+
``` ts
|
|
99
|
+
export default defineConfig({
|
|
100
|
+
buildConfig: {
|
|
101
|
+
autoExternal: {
|
|
102
|
+
dependencies: false,
|
|
103
|
+
peerDependencies: false,
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Exclude specified third-party dependencies
|
|
110
|
+
|
|
111
|
+
And in addition to the `buildConfig.autoExternal` API, `buildConfig.externals` can also control which third-party dependencies are to be handled. We can combine these two
|
|
112
|
+
APIs to handle the project's dependencies in a more granular way.
|
|
113
|
+
|
|
114
|
+
For example, when we need to leave only certain dependencies unpacked, we can configure it as follows.
|
|
115
|
+
|
|
116
|
+
> In this case, it is likely that some dependencies are not suitable for packaging. If this is the case, then you can handle it as follows.
|
|
117
|
+
|
|
118
|
+
``` ts
|
|
119
|
+
epxort default defineConfig({
|
|
120
|
+
buildConfig: {
|
|
121
|
+
autoExternal: false,
|
|
122
|
+
externals: ['pkg-1', /pkg-2/],
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
```
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
# In-depth understanding of build
|
|
2
|
+
|
|
3
|
+
In the [Basic Usage] section, we already knew that you can modify the output product of a project through the `buildConfig` configuration. `buildConfig` not only describes some of the features of the product, but also provides some functionality for building the product.
|
|
4
|
+
|
|
5
|
+
:::tip{title=notes}
|
|
6
|
+
If you are not sure what `buildConfig` is, it is recommended to take some time to understand it by following this link.
|
|
7
|
+
|
|
8
|
+
* [[modify-output-product](/en/guide/modify-output-product)]
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
In this chapter we'll dive into the use of certain build configurations and understand what happens when the `modern build` command is executed.
|
|
12
|
+
|
|
13
|
+
## In-depth understanding of `buildConfig`
|
|
14
|
+
|
|
15
|
+
### Bundle and Bundleless
|
|
16
|
+
|
|
17
|
+
So first let's understand Bundle and Bundleless.
|
|
18
|
+
|
|
19
|
+
A Bundle is a package of build products, which may be a single file or multiple files based on a certain [code splitting strategy](https://esbuild.github.io/api/#splitting).
|
|
20
|
+
|
|
21
|
+
Bundleless, on the other hand, means that each source file is compiled and built separately, but not packaged together. Each product file can be found with its corresponding source code file. The process of **Bundleless build can also be understood as the process of code conversion of source files only**.
|
|
22
|
+
|
|
23
|
+
In `buildConfig` you can specify whether the current build task is Bundle or Bundleless by using [`buildConfig.buildType`](/en/api/build-config#buildtype).
|
|
24
|
+
|
|
25
|
+
### Relationship between `input` and `sourceDir`
|
|
26
|
+
|
|
27
|
+
[`buildConfig.input`](/en/api/build-config#input) is used to specify the file path or directory path where the source code is read, and its default value differs between Bundle and Bundleless builds.
|
|
28
|
+
|
|
29
|
+
* When `buildType: 'bundle'`, `input` defaults to `['src/index.ts']`
|
|
30
|
+
* When `buildType: 'bundleless'`, `input` defaults to `['src']`
|
|
31
|
+
> In fact, at `buildType: 'bundle'`, the build tool detects the existence of a file matching the name rule `src/index.(j|t)sx?` and uses it as an entry file.
|
|
32
|
+
|
|
33
|
+
:::warn{title=notes}
|
|
34
|
+
It is recommended that you do not specify multiple source file directories during a Bundleless build, as unintended results may occur. Bundleless builds with multiple source directories are currently in an unstable stage.
|
|
35
|
+
:::
|
|
36
|
+
|
|
37
|
+
We know from the defaults: **Bundle builds can generally specify a file path as the entry point to the build, while Bundleless builds are more expected to use directory paths to find source files**.
|
|
38
|
+
|
|
39
|
+
#### Object type of `input`
|
|
40
|
+
|
|
41
|
+
In addition to setting `input` to an array, you can also set it to an object during the Bundle build process. **By using the object form, we can modify the name of the file that the build product outputs**. So for the following example, `. /src/index.ts` corresponds to the path of the build product file as `. /dist/main.js`.
|
|
42
|
+
|
|
43
|
+
``` tsx modern.config.ts
|
|
44
|
+
import { defineConfig } from '@modern-js/module-tools';
|
|
45
|
+
|
|
46
|
+
export default defineConfig({
|
|
47
|
+
buildConfig: {
|
|
48
|
+
input: {
|
|
49
|
+
main: ['./src/index.ts'],
|
|
50
|
+
},
|
|
51
|
+
outdir: './dist',
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
The Bundleless build process also supports such use, but it is not recommended.
|
|
57
|
+
|
|
58
|
+
#### `sourceDir`
|
|
59
|
+
|
|
60
|
+
[`sourceDir`](/en/api/build-config#sourcedir) is used to specify the source code directory, which is related to both.
|
|
61
|
+
|
|
62
|
+
* type file generation
|
|
63
|
+
* [`outbase`](https://esbuild.github.io/api/#outbase) for specifying the build process
|
|
64
|
+
|
|
65
|
+
In general:
|
|
66
|
+
|
|
67
|
+
* **During the Bundleless build process, the values of `sourceDir` and `input` should be the same, and their default values are both `src`**.
|
|
68
|
+
* It is rarely necessary to use `sourceDir` during the Bundle build process.
|
|
69
|
+
|
|
70
|
+
### Declaration Type Files
|
|
71
|
+
|
|
72
|
+
The [`buildConfig.dts`](/en/api/build-config#dts) configuration is mainly used for type file generation.
|
|
73
|
+
|
|
74
|
+
#### Turn off type generation
|
|
75
|
+
|
|
76
|
+
Type generation is turned on by default, if you need to turn it off, you can configure it as follows:
|
|
77
|
+
|
|
78
|
+
```ts ./modern.config.ts
|
|
79
|
+
import { defineConfig } from '@modern-js/module-tools';
|
|
80
|
+
|
|
81
|
+
export default defineConfig({
|
|
82
|
+
buildConfig: {
|
|
83
|
+
dts: false
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
:::tip
|
|
89
|
+
The build speed is generally improved by closing the type file.
|
|
90
|
+
:::
|
|
91
|
+
|
|
92
|
+
#### Build type files
|
|
93
|
+
|
|
94
|
+
With `buildType: 'bundleless'`, type files are generated using the project's `tsc` command to complete production.
|
|
95
|
+
|
|
96
|
+
The **module engineering solution also supports packaging of type files**, although care needs to be taken when using this feature.
|
|
97
|
+
|
|
98
|
+
* Some third-party dependencies have incorrect syntax that can cause the packaging process to fail. So in this case, you need to exclude such third-party packages manually with [`buildConfig.externals`](/en/api/build-config#externals).
|
|
99
|
+
* It is not possible to handle the case where the type file of a third-party dependency points to a `.ts` file. For example, the `package.json` of a third-party dependency contains something like this: `{"types": ". /src/index.ts"}`.
|
|
100
|
+
|
|
101
|
+
#### Alias Conversion
|
|
102
|
+
|
|
103
|
+
During the Bundleless build process, if an alias appears in the source code, e.g.
|
|
104
|
+
|
|
105
|
+
```ts ./src/index.ts
|
|
106
|
+
import utils from '@common/utils';
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Normally, product type files generated with `tsc` will also contain these aliases. However, Module Tools will convert the aliases in the type file generated by `tsc` to:
|
|
110
|
+
|
|
111
|
+
* Alias conversion is possible for code of the form `import '@common/utils'` or `import utils from '@common/utils'`.
|
|
112
|
+
* Aliasing is possible for code of the form `export { utils } from '@common/utils'`.
|
|
113
|
+
|
|
114
|
+
However, there are some cases that cannot be handled at this time.
|
|
115
|
+
|
|
116
|
+
* Output types of the form `Promise<import('@common/utils')>` cannot be converted at this time.
|
|
117
|
+
|
|
118
|
+
#### Some examples of the use of `dts`
|
|
119
|
+
|
|
120
|
+
General usage:
|
|
121
|
+
|
|
122
|
+
``` ts
|
|
123
|
+
import { defineConfig } from '@modern-js/module-tools';
|
|
124
|
+
|
|
125
|
+
export default defineConfig({
|
|
126
|
+
// The output path of the packaged type file at this point is `./dist/types`
|
|
127
|
+
buildConfig: {
|
|
128
|
+
buildType: 'bundle',
|
|
129
|
+
dts: {
|
|
130
|
+
tsconfigPath: './other-tsconfig.json',
|
|
131
|
+
distPath: './types',
|
|
132
|
+
},
|
|
133
|
+
outdir: './dist',
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
For the use of `dts.only`:
|
|
138
|
+
|
|
139
|
+
``` ts
|
|
140
|
+
import { defineConfig } from '@modern-js/module-tools';
|
|
141
|
+
|
|
142
|
+
export default defineConfig({
|
|
143
|
+
// At this moment the type file is not packaged and the output path is `./dist/types`
|
|
144
|
+
buildConfig: [
|
|
145
|
+
{
|
|
146
|
+
buildType: 'bundle',
|
|
147
|
+
dts: false,
|
|
148
|
+
outdir: './dist',
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
buildType: 'bundleless',
|
|
152
|
+
dts: {
|
|
153
|
+
only: true,
|
|
154
|
+
},
|
|
155
|
+
outdir: './dist/types',
|
|
156
|
+
}
|
|
157
|
+
]
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### `buildConfig.define` Usage for different scenarios
|
|
162
|
+
|
|
163
|
+
[`buildConfig.define`](/en/api/build-config#define) functions somewhat similar to [`webpack.DefinePlugin`](https://webpack.js.org/plugins/define-plugin/). A few usage scenarios are described here.
|
|
164
|
+
|
|
165
|
+
#### Environment variable replacement
|
|
166
|
+
|
|
167
|
+
``` ts
|
|
168
|
+
import { defineConfig } from '@modern-js/module-tools';
|
|
169
|
+
export default defineConfig({
|
|
170
|
+
buildConfig: {
|
|
171
|
+
define: {
|
|
172
|
+
'process.env.VERSION': JSON.stringify(process.env.VERSION || '0.0.0')
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
With the above configuration, we can put the following code.
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
// pre-compiler code
|
|
182
|
+
console.log(process.env.VERSION);
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
When executing ``VERSION=1.0.0 modern build``, the conversion is:
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
// compiled code
|
|
189
|
+
console.log('1.0.0');
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
#### Global variable replacement
|
|
193
|
+
|
|
194
|
+
``` ts
|
|
195
|
+
import { defineConfig } from '@modern-js/module-tools';
|
|
196
|
+
export default defineConfig({
|
|
197
|
+
buildConfig: {
|
|
198
|
+
define: {
|
|
199
|
+
'VERSION': JSON.stringify(require('. /package.json').version || '0.0.0')
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
With the above configuration, we can put the following code.
|
|
206
|
+
|
|
207
|
+
```
|
|
208
|
+
// pre-compile code
|
|
209
|
+
console.log(VERSION);
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Convert to:
|
|
213
|
+
|
|
214
|
+
```
|
|
215
|
+
// post-compile code
|
|
216
|
+
console.log('1.0.0');
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Note, however: If the project is a TypeScript project, then you may need to add the following to the `.d.ts` file in the project source directory.
|
|
220
|
+
> If the `.d.ts` file does not exist, then you can create it manually.
|
|
221
|
+
|
|
222
|
+
``` ts env.d.ts
|
|
223
|
+
declare const YOUR_ADD_GLOBAL_VAR;
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Build process
|
|
227
|
+
|
|
228
|
+
When the `modern build` command is executed, the
|
|
229
|
+
|
|
230
|
+
* Clear the products directory according to `buildConfig.outdir`.
|
|
231
|
+
* Compile `js/ts` source code to generate the JS build product for Bundle/Bundleless.
|
|
232
|
+
* Generate Bundle/Bundleless type files using `tsc`.
|
|
233
|
+
* Handle Copy tasks.
|
|
234
|
+
|
|
235
|
+
## Build errors
|
|
236
|
+
|
|
237
|
+
When a build error occurs, based on the information learned above, it is easy to understand what error appears in the terminal.
|
|
238
|
+
|
|
239
|
+
**Errors reported for js or ts builds:**
|
|
240
|
+
|
|
241
|
+
``` bash
|
|
242
|
+
error ModuleBuildError:
|
|
243
|
+
|
|
244
|
+
╭───────────────────────╮
|
|
245
|
+
│ bundle failed: │
|
|
246
|
+
│ - format is "cjs" │
|
|
247
|
+
│ - target is "esnext" │
|
|
248
|
+
╰───────────────────────╯
|
|
249
|
+
|
|
250
|
+
Detailed Information:
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
**Errors reported for the type file generation process:**
|
|
254
|
+
|
|
255
|
+
``` bash
|
|
256
|
+
error ModuleBuildError:
|
|
257
|
+
|
|
258
|
+
bundle DTS failed:
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
For `js/ts` build errors, we can tell from the error message.
|
|
262
|
+
|
|
263
|
+
* By `'bundle failed:'` to determine if the error is reported for a Bundle build or a Bundleless build?
|
|
264
|
+
* What is the `format` of the build process?
|
|
265
|
+
* What is the `target` of the build process?
|
|
266
|
+
* The specific error message.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# 深入理解构建
|
|
2
|
+
|
|
3
|
+
在【基础使用】的部分,我们已经知道可以通过 `buildConfig` 配置对项目的输出产物进行修改。`buildConfig` 不仅描述了产物的一些特性,同时还为构建产物提供了一些功能。
|
|
4
|
+
|
|
5
|
+
:::tip{title=注意}
|
|
6
|
+
如果你还不清楚 `buildConfig` 是什么,建议花一些时间通过下面的链接了解一下:
|
|
7
|
+
|
|
8
|
+
* 【[修改输出产物](/zh/guide/modify-output-product)】
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
而在本章里我们将要深入理解某些构建配置的使用以及了解执行 `modern build` 命令的时候发生了什么。
|
|
12
|
+
|
|
13
|
+
## 深入理解 `buildConfig`
|
|
14
|
+
|
|
15
|
+
那么首先我们来理解 **bundle** 和 **bundleless** 的构建模式。
|
|
16
|
+
|
|
17
|
+
### bundle 和 bundleless
|
|
18
|
+
|
|
19
|
+
### input 与 sourceDir 的关系
|
|
20
|
+
|
|
21
|
+
## 构建过程
|
|
22
|
+
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# Before you start
|
|
2
|
+
|
|
3
|
+
## Environment preparation
|
|
4
|
+
|
|
5
|
+
In order to use the Modern.js module engineering solution, you first need [NodeJS](https://nodejs.org/zh/) engine, we recommend the latest [LTS version](https://github.com/nodejs/Release), and make sure the Node version is **>=14.17.6**. because non-stable NodeJS releases frequently have bugs. You might consider installing via [nvm-windows](https://github.com/coreybutler/nvm-windows) and [nvm](https://github.com/nvm-sh/nvm) (Mac/linux), so you can easily switch to different NodeJS versions that might be required for different projects that you work on.
|
|
6
|
+
## Getting Started with npm
|
|
7
|
+
|
|
8
|
+
Once NodeJS is installed, not only can you access the `node` executable from the command line, but you can also execute the `npm` command.
|
|
9
|
+
|
|
10
|
+
Npm is the standard package manager for NodeJS. It started out as a tool for downloading and managing NodeJS package dependencies, but it has since evolved into a tool for front-end JavaScript.
|
|
11
|
+
|
|
12
|
+
**If you already know something about usage of npm and npm packages, then you can directly see [npm package manager](/en/guide/before-getting-started#npm-package-manager) section.**
|
|
13
|
+
|
|
14
|
+
## npm package type project
|
|
15
|
+
|
|
16
|
+
So what is an npm package type project? When we execute the `npm init` command in an empty project directory, it creates a JSON file with the file name `package.json` under the current directory. During the creation process, we will need to fill in information including but not limited to the *name*, *version* *number*, *description*, etc. of the npm package, which will be found in the resulting `package.json` file as follows
|
|
17
|
+
|
|
18
|
+
``` json
|
|
19
|
+
{
|
|
20
|
+
"name": "npm-demo",
|
|
21
|
+
"version": "1.0.0",
|
|
22
|
+
"description": "",
|
|
23
|
+
"main": "index.js",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
26
|
+
},
|
|
27
|
+
"author": "",
|
|
28
|
+
"license": "ISC"
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
At this point the project containing the initialized `package.json` file is an npm package type project, and you can execute the `npm publish` command to publish the project to the [npm Registry](https://www.npmjs.com/).
|
|
33
|
+
|
|
34
|
+
The npm Registry is a [npm package store](https://docs.npmjs.com/about-the-public-npm-registry) where developers can not only publish their own npm packages to the npm Registry, but also use npm packages published by other developers through the npm Registry.
|
|
35
|
+
|
|
36
|
+
A quality npm package will be used by more people because it not only saves a lot of code implementation work, but is also less likely to cause problems with the project.
|
|
37
|
+
|
|
38
|
+
## Using third-party npm packages
|
|
39
|
+
|
|
40
|
+
When adding a third-party npm package to an initial project, we can call this process "installing dependencies for the project" or "adding dependencies to the project". Before adding dependencies, we need to know one thing in particular -- **the types of packages npm depends on**.
|
|
41
|
+
|
|
42
|
+
- `"dependencies"`: a type of package that your application will need in a production environment.
|
|
43
|
+
- `"devDependencies"`: another type of package that is only needed for local development and testing.
|
|
44
|
+
> packages can be understood as third-party npm packages.
|
|
45
|
+
|
|
46
|
+
You can install the packages you need in a **production environment** by running `npm install npm-package-name` or `npm add npm-package-name`, or you can manually write the packages you need to install and the corresponding [semantic version](https://docs.npmjs.com/about-semantic-versioning) in `"dependencies"` in the `package.json` file, and run the `npm install` command to.
|
|
47
|
+
|
|
48
|
+
``` json
|
|
49
|
+
{
|
|
50
|
+
"name": "your-npm-project",
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"npm-package-name": "0.1.0"
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Similarly, you can install **only packages needed for local development and testing** by running `npm install npm-package-name --save-dev` or `npm add npm-package-name --save-dev`, or you can manually write the packages to be installed and the corresponding [semantic version](https://docs.npmjs.com/about-semantic-versioning) in `"devDependencies"` in the `package.json` file, and run the `npm install` command as follows
|
|
58
|
+
|
|
59
|
+
``` json
|
|
60
|
+
{
|
|
61
|
+
"name": "your-npm-project",
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"npm-package-name": "0.1.0"
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**When installing or using third-party npm packages be sure to determine what they are for and whether they should be placed in `"dependencies"` or `"devDependencies"` by distinguishing between their types.**
|
|
69
|
+
:::tip
|
|
70
|
+
In general, packages that need to be used in source code are `dependencies` dependencies. Unless you are exporting dependent code locally via packaging, in which case it can be treated as a `devDependencies` dependency.
|
|
71
|
+
:::
|
|
72
|
+
|
|
73
|
+
## Other npm bits and pieces to know
|
|
74
|
+
|
|
75
|
+
### Program entry for npm packages
|
|
76
|
+
|
|
77
|
+
There is a `"main"` attribute in `package.json` that corresponds to a module ID or, more intuitively, a NodeJS file path, which is the main entry point for your application.
|
|
78
|
+
|
|
79
|
+
For example, if your package is named `foo` and the user installs it, and then executes the `require("foo")` code, then the file corresponding to the `"main"` field of the npm package `foo` will be exported.
|
|
80
|
+
|
|
81
|
+
**It is recommended to set the `"main"` field in your npm package**. If `"main"` is not set, the default entry will be the `index.js` file in the root of the package.
|
|
82
|
+
|
|
83
|
+
In addition to the `"main"` attribute, the `"module"` attribute is usually set. It is similar to the `"main"` attribute in that it is mainly used in webpack scenarios. webpack reads the npm package entry (file) in most cases in the order **"module" -> "main "**.
|
|
84
|
+
|
|
85
|
+
> To learn more about how webpack does this, check out this [link](https://webpack.js.org/configuration/resolve/#resolvemainfields).
|
|
86
|
+
|
|
87
|
+
### `"scripts"`
|
|
88
|
+
|
|
89
|
+
The `"scripts"` attribute of the `package.json` file supports a number of built-in scripts and npm-preset lifecycle events, as well as arbitrary scripts.
|
|
90
|
+
|
|
91
|
+
These can be executed by running `npm run-script <stage>` or simply `npm run <stage>`.
|
|
92
|
+
|
|
93
|
+
Name matching [pre and post commands](https://docs.npmjs.com/cli/v9/using-npm/scripts#pre--post-scripts) will also be run (e.g. `premyscript`, `myscript`, `postmyscript` ).
|
|
94
|
+
|
|
95
|
+
``` json
|
|
96
|
+
{
|
|
97
|
+
"scripts": {
|
|
98
|
+
"premyscript": "",
|
|
99
|
+
"myscript": "",
|
|
100
|
+
"postmyscript": "",
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
When `npm run myscripts` is executed, the script corresponding to `premyscripts` will be executed before it, and the script corresponding to `postmyscripts` will be executed after it.
|
|
106
|
+
|
|
107
|
+
Script commands from dependencies can be run with `npm explore <pkg> -- npm run <stage>`.
|
|
108
|
+
|
|
109
|
+
There are also special lifecycle scripts that occur only under certain circumstances. Here are a few that are usually necessary to know.
|
|
110
|
+
|
|
111
|
+
#### `npm install`
|
|
112
|
+
|
|
113
|
+
When you run `npm install -g <pkg-name>`, the following scripts will run.
|
|
114
|
+
|
|
115
|
+
- `preinstall`
|
|
116
|
+
- `install`
|
|
117
|
+
- `postinstall`
|
|
118
|
+
- `prepublish`
|
|
119
|
+
- `preprepare`
|
|
120
|
+
- `prepare`
|
|
121
|
+
- `postprepare`
|
|
122
|
+
|
|
123
|
+
If your package root has a `binding.gyp` file and you don't define an `install` or `preinstall` script, then npm will build with `node-gyp rebuild` as the default install command, using [node-gyp](https://github.com/nodejs/node-gyp).
|
|
124
|
+
|
|
125
|
+
#### `npm publish`
|
|
126
|
+
|
|
127
|
+
When publishing a project, executing this command will trigger the following script.
|
|
128
|
+
|
|
129
|
+
- `prepublishOnly`
|
|
130
|
+
- `prepack`
|
|
131
|
+
- `prepare`
|
|
132
|
+
- `postpack`
|
|
133
|
+
- `publish`
|
|
134
|
+
- `postpublish`
|
|
135
|
+
|
|
136
|
+
When running in [`-dry-run`](https://docs.npmjs.com/cli/v7/commands/npm-publish#dry-run) mode, the script corresponding to `prepare` will not be executed.
|
|
137
|
+
|
|
138
|
+
### peerDependencies
|
|
139
|
+
|
|
140
|
+
In some cases, your npm project has a compatibility relationship with its host tool or library (e.g. a webpack plugin project and webpack), and your npm project does not want to use the host as a necessary dependency, which usually means that your project is probably a plugin for that host tool or library. Your npm project will have certain requirements for the version of the host package, as only the APIs required by the npm project will be exposed under a specific version.
|
|
141
|
+
|
|
142
|
+
For more explanation of `peerDependencies`, you can learn about the different ways npm, pnpm, and Yarn handle it at the following links.
|
|
143
|
+
|
|
144
|
+
- [npm's explanation of peerDependencies](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#peerdependencies)
|
|
145
|
+
- [pnpm vs npm vs Yarn](https://pnpm.io/feature-comparison)
|
|
146
|
+
|
|
147
|
+
## npm package manager
|
|
148
|
+
|
|
149
|
+
In addition to the standard package manager like npm, the mainstream ones are **pnpm** and **Yarn**, both of which are good alternatives to npm cli.
|
|
150
|
+
|
|
151
|
+
It is recommended to use [pnpm](https://pnpm.io/installation) to manage project dependencies, which can be installed as follows.
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
npm install -g pnpm
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Module Tools configuration file
|
|
158
|
+
|
|
159
|
+
A configuration file for Module Tools is provided in the initial module project directory -- `modern.config.(j|t)s`. By default, no configuration is required, so the `modern.config` configuration file is not required to exist.
|
|
160
|
+
|
|
161
|
+
The contents of the initial configuration file are as follows.
|
|
162
|
+
|
|
163
|
+
``` typescript
|
|
164
|
+
// modern.config.ts
|
|
165
|
+
import { defineConfig } from '@modern-js/module-tools';
|
|
166
|
+
|
|
167
|
+
export default defineConfig({});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
``` js
|
|
171
|
+
// modern.config.js
|
|
172
|
+
const { defineConfig } = require('@modern-js/module-tools');
|
|
173
|
+
|
|
174
|
+
module.exports = defineConfig({});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**We recommend using the `defineConfig` function**, but it is not mandatory to use it. So you can also return an object directly in the configuration file: the:
|
|
178
|
+
|
|
179
|
+
``` typescript
|
|
180
|
+
// modern.config.ts
|
|
181
|
+
export default {};
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
``` js
|
|
185
|
+
// modern.config.js
|
|
186
|
+
module.exports = {};
|
|
187
|
+
```
|