@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,132 @@
|
|
|
1
|
+
# Handle static resource files
|
|
2
|
+
|
|
3
|
+
The module project will handle static resources used in the code. If configuration is required, then the [`buildConfig.asset`](/en/api/build-config#asset) API can be used.
|
|
4
|
+
|
|
5
|
+
## Default behavior
|
|
6
|
+
|
|
7
|
+
By default, module projects are processed for the following static resources:
|
|
8
|
+
|
|
9
|
+
* `'.svg'`、`'.png'`、`'.jpg'`、`'.jpeg'`、`'.gif'`、`'.webp'`
|
|
10
|
+
* `'.ttf'`、`'.otf'`、`'.woff'`、`'.woff2'`、`'.eot'`
|
|
11
|
+
* `'.mp3'`、`'.mp4'`、`'.webm'`、`'.ogg'`、`'.wav'`、`'.flac'`、`'.aac'`、`'.mov'`
|
|
12
|
+
|
|
13
|
+
For the handling of static files, the module project currently supports the following functions.
|
|
14
|
+
|
|
15
|
+
* Set the static resource path to `. /assets`.
|
|
16
|
+
* For files over **10kb** they are inlined into the code.
|
|
17
|
+
* Use [SVGR](https://react-svgr.com/) for files with the `.svg` suffix.
|
|
18
|
+
|
|
19
|
+
Let us look at the following example:
|
|
20
|
+
|
|
21
|
+
<CH.Spotlight>
|
|
22
|
+
|
|
23
|
+
``` ts ./src/asset.ts
|
|
24
|
+
import img from './bg.png';
|
|
25
|
+
//...
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
Project source code.
|
|
30
|
+
|
|
31
|
+
``` ts ./src/asset.ts
|
|
32
|
+
import img from './bg.png';
|
|
33
|
+
//...
|
|
34
|
+
```
|
|
35
|
+
---
|
|
36
|
+
If the size of `bg.png` is less than 10 kb, then the product directory structure and product content are.
|
|
37
|
+
|
|
38
|
+
<CH.Code>
|
|
39
|
+
|
|
40
|
+
``` bash
|
|
41
|
+
./dist
|
|
42
|
+
└── asset.js
|
|
43
|
+
```
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
``` js ./dist/asset.js
|
|
47
|
+
var bg_default = "data:image/png;base64,";
|
|
48
|
+
console.info(bg_default);
|
|
49
|
+
```
|
|
50
|
+
</CH.Code>
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
If the size of `bg.png` is larger than 10 kb, then the product directory structure and product content are.
|
|
54
|
+
|
|
55
|
+
<CH.Code>
|
|
56
|
+
|
|
57
|
+
``` bash
|
|
58
|
+
./dist
|
|
59
|
+
├── asset.js
|
|
60
|
+
└── assets
|
|
61
|
+
└── bg.13e2aba2.png
|
|
62
|
+
```
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
``` js ./dist/asset.js
|
|
66
|
+
var bg_default = "assets/bg.13e2aba2.png";
|
|
67
|
+
console.info(bg_default);
|
|
68
|
+
```
|
|
69
|
+
</CH.Code>
|
|
70
|
+
|
|
71
|
+
</CH.Spotlight>
|
|
72
|
+
|
|
73
|
+
When wanting to modify the default behavior, the following API can be used:
|
|
74
|
+
|
|
75
|
+
* `asset.path`: modify the output path of the static resource file.
|
|
76
|
+
* `asset.limit`: modify the threshold value for inline static resource files.
|
|
77
|
+
|
|
78
|
+
## Setting CDN Prefix
|
|
79
|
+
|
|
80
|
+
If the project is running in a browser environment, we may need to host static resources to a CDN and then use them in the production environment.
|
|
81
|
+
|
|
82
|
+
Then we need to add the CDN base path in front of the path to the static resource file in the default generated build product. Again, see an example:
|
|
83
|
+
|
|
84
|
+
<CH.Spotlight>
|
|
85
|
+
|
|
86
|
+
``` ts ./src/asset.ts
|
|
87
|
+
import img from './bg.png';
|
|
88
|
+
//...
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
Project source code.
|
|
93
|
+
|
|
94
|
+
``` ts ./src/asset.ts
|
|
95
|
+
```
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
`modern.config` config file。
|
|
99
|
+
|
|
100
|
+
``` ts ./modern.config.ts
|
|
101
|
+
export default defineConfig({
|
|
102
|
+
buildConfig: {
|
|
103
|
+
format: 'umd',
|
|
104
|
+
asset: {
|
|
105
|
+
publicPath: 'https://cdn/',
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
If the size of `bg.png` is larger than 10 kb, then the product content will be.
|
|
114
|
+
|
|
115
|
+
``` js ./dist/asset.js focus=7:7
|
|
116
|
+
(function(global, factory) {
|
|
117
|
+
if (typeof module === "object" && typeof module.exports === "object") factory();
|
|
118
|
+
else if (typeof define === "function" && define.amd) define([], factory);
|
|
119
|
+
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
|
|
120
|
+
})(this, function() {
|
|
121
|
+
"use strict";
|
|
122
|
+
var bg_default = "http://cdn/assets/bg.13e2aba2.png";
|
|
123
|
+
// src/asset.tsx
|
|
124
|
+
console.info(bg_default);
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
</CH.Spotlight>
|
|
129
|
+
|
|
130
|
+
## SVGR
|
|
131
|
+
|
|
132
|
+
WIP
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# Build umd products
|
|
2
|
+
|
|
3
|
+
The full name of umd is **Universal Module Definition**, and JS files in this format can run in multiple runtime environments: the
|
|
4
|
+
|
|
5
|
+
* Browser environment: module loading based on AMD specification
|
|
6
|
+
* Node.js environment: module loading based on CommonJS
|
|
7
|
+
* Other cases: mount the module on a global object.
|
|
8
|
+
|
|
9
|
+
We can therefore specify the build product of the project as an umd product in the following way:
|
|
10
|
+
|
|
11
|
+
``` ts
|
|
12
|
+
export default defineConfig({
|
|
13
|
+
buildConfig: {
|
|
14
|
+
format: 'umd',
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Third-party dependency handling for umd products
|
|
20
|
+
|
|
21
|
+
In the [[How to handle third-party dependencies]](/en/guide/advance/external-dependency) chapter, we know that we can control whether or not the project packages third-party dependencies via the [`autoExternals`](/en/api/build-config#autoexternal) and [`externals`](/en/api/build-config#externals) APIs.
|
|
22
|
+
So when building umd products, we can also use it like this:
|
|
23
|
+
|
|
24
|
+
<CH.Spotlight>
|
|
25
|
+
|
|
26
|
+
```json package.json
|
|
27
|
+
{
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"react": "^17"
|
|
30
|
+
//...other dependencies
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
If the project depends on `react`.
|
|
38
|
+
|
|
39
|
+
```json package.json
|
|
40
|
+
{
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"react": "^17"
|
|
43
|
+
//...other dependencies
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
`modern.config.ts`.
|
|
51
|
+
|
|
52
|
+
``` ts
|
|
53
|
+
export default defineConfig({
|
|
54
|
+
buildConfig: {
|
|
55
|
+
format: 'umd',
|
|
56
|
+
autoExternal: false,
|
|
57
|
+
externals: ['react'],
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
When a `react` dependency is used in the source code.
|
|
65
|
+
|
|
66
|
+
``` tsx src/index.ts
|
|
67
|
+
import React from "react";
|
|
68
|
+
console.info(React);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
The `react` code is not packaged into the product at this point.
|
|
74
|
+
|
|
75
|
+
``` js dist/index.js focus=1:7
|
|
76
|
+
(function(global, factory) {
|
|
77
|
+
if (typeof module === "object" && typeof module.exports === "object") factory(exports, require("react"));
|
|
78
|
+
else if (typeof define === "function" && define.amd) define([
|
|
79
|
+
"exports",
|
|
80
|
+
"react"
|
|
81
|
+
], factory);
|
|
82
|
+
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.index = {}, global.react);
|
|
83
|
+
})(this, function(exports, _react) {
|
|
84
|
+
"use strict";
|
|
85
|
+
Object.defineProperty(exports, "__esModule", {
|
|
86
|
+
value: true
|
|
87
|
+
});
|
|
88
|
+
_react = /*#__PURE__*/ _interopRequireDefault(_react);
|
|
89
|
+
function _interopRequireDefault(obj) {
|
|
90
|
+
return obj && obj.__esModule ? obj : {
|
|
91
|
+
default: obj
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
console.info(_react.default);
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
</CH.Spotlight>
|
|
99
|
+
|
|
100
|
+
We know from the above example that when using the `autoExternal` and `externals` APIs.
|
|
101
|
+
|
|
102
|
+
* In a Node.js environment, you can get the react dependency with `require('react')`.
|
|
103
|
+
* In a browser environment, you can get the react dependency via `global.react`.
|
|
104
|
+
|
|
105
|
+
However, in the browser environment, when getting third-party dependencies, **global variable names are not necessarily identical to the dependency names**, so you have to use the [`buildConfig.umdGlobals`](/en/api/build-config#umdglobals) API.
|
|
106
|
+
|
|
107
|
+
Again using the previous example, when the `react` dependency exists in the browser environment as a `windows.React` or `global.React` global variable, then:
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
<CH.Spotlight>
|
|
111
|
+
|
|
112
|
+
```json package.json
|
|
113
|
+
{
|
|
114
|
+
"devDependencies": {
|
|
115
|
+
"react": "^17"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
If the project depends on `react`.
|
|
123
|
+
|
|
124
|
+
```json package.json
|
|
125
|
+
{
|
|
126
|
+
"devDependencies": {
|
|
127
|
+
"react": "^17"
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
`modern.config.ts` config file。
|
|
135
|
+
|
|
136
|
+
``` ts
|
|
137
|
+
export default defineConfig({
|
|
138
|
+
buildConfig: {
|
|
139
|
+
format: 'umd',
|
|
140
|
+
umdGlobals: {
|
|
141
|
+
react: 'React',
|
|
142
|
+
},
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
When a `react` dependency is used in the source code.
|
|
150
|
+
|
|
151
|
+
``` tsx src/index.ts
|
|
152
|
+
import React from "react";
|
|
153
|
+
console.info(React);
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
At this point we will see the product code like this.
|
|
159
|
+
|
|
160
|
+
``` js dist/index.js focus=7:12
|
|
161
|
+
(function(global, factory) {
|
|
162
|
+
if (typeof module === "object" && typeof module.exports === "object") factory();
|
|
163
|
+
else if (typeof define === "function" && define.amd) define([], factory);
|
|
164
|
+
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
|
|
165
|
+
})(this, function() {
|
|
166
|
+
// ...
|
|
167
|
+
// libuild:globals:react
|
|
168
|
+
var require_react = __commonJS({
|
|
169
|
+
"libuild:globals:react" (exports, module1) {
|
|
170
|
+
module1.exports = Function("return this")()["React"];
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
// src/index.ts
|
|
174
|
+
var import_react = __toESM(require_react());
|
|
175
|
+
console.info(import_react.default);
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
</CH.Spotlight>
|
|
180
|
+
|
|
181
|
+
The project can then run in the browser and use the `React` variables that exist on the global object.
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
## Changing the name of a global variable in a project
|
|
185
|
+
|
|
186
|
+
When we package the following code into an umd product and run it in the browser, we can use the module via `window.index`.
|
|
187
|
+
|
|
188
|
+
``` ts ./src/index.ts
|
|
189
|
+
export default () => {
|
|
190
|
+
console.info('hello world');
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
** By default, the name of the source file is used as the name of the module's global variable in the browser. **For the above example, the product would read as follows:
|
|
195
|
+
|
|
196
|
+
``` js ./dist/index.js focus=6:6
|
|
197
|
+
(function(global, factory) {
|
|
198
|
+
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
|
|
199
|
+
else if (typeof define === "function" && define.amd) define([
|
|
200
|
+
"exports"
|
|
201
|
+
], factory);
|
|
202
|
+
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.index = {});
|
|
203
|
+
})(this, function(exports) {
|
|
204
|
+
//...
|
|
205
|
+
});
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
If you need to modify it, you need to use the [`buildConfig.umdModuleName`](/en/api/build-config#umdmodulename) API.
|
|
209
|
+
|
|
210
|
+
When this API is used:
|
|
211
|
+
|
|
212
|
+
``` ts
|
|
213
|
+
export default defineConfig({
|
|
214
|
+
buildConfig: {
|
|
215
|
+
format: 'umd',
|
|
216
|
+
umdModuleName: 'myLib',
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
The construction products at this point are:
|
|
222
|
+
|
|
223
|
+
``` js ./dist/index.js focus=6:6
|
|
224
|
+
(function(global, factory) {
|
|
225
|
+
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
|
|
226
|
+
else if (typeof define === "function" && define.amd) define([
|
|
227
|
+
"exports"
|
|
228
|
+
], factory);
|
|
229
|
+
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.myLib = {});
|
|
230
|
+
})(this, function(exports) {
|
|
231
|
+
//...
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# Use the Copy Tools
|
|
2
|
+
|
|
3
|
+
The Module Project provides the Copy utility for copying already existing individual files or entire directories into the product directory. Next we learn how to use it.
|
|
4
|
+
|
|
5
|
+
## Understanding the Copy API
|
|
6
|
+
|
|
7
|
+
We can use the Copy tool via the [`buildConfig.copy`](/en/api/build-config#copy) API, which contains the following two main configurations.
|
|
8
|
+
|
|
9
|
+
* [`patterns`](/en/api/build-config#copypatterns)
|
|
10
|
+
* [`options`](/en/api/build-config#copyoptions)
|
|
11
|
+
|
|
12
|
+
It is recommended to spend some time getting to know them before you start learning.
|
|
13
|
+
|
|
14
|
+
## API Description
|
|
15
|
+
|
|
16
|
+
`copy.patterns` 用于寻找复制的文件以及配置输出的路径。
|
|
17
|
+
|
|
18
|
+
其中 `patterns.from` 用于指定要复制的文件或者目录。它接收 Glob 形式字符串或具体路径。Glob 形式字符串是指 [fast-glob pattern-syntax](https://github.com/mrmlnc/fast-glob#pattern-syntax)。因此我们可以按照如下两种方式使用它:
|
|
19
|
+
|
|
20
|
+
``` ts
|
|
21
|
+
export default defineConfig({
|
|
22
|
+
buildConfig: {
|
|
23
|
+
copy: {
|
|
24
|
+
patterns: [
|
|
25
|
+
{ from: './index.html', to: ''},
|
|
26
|
+
{ from: './*.html', to: ''},
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`patterns.context` 一般和 `patterns.from` 配合使用,默认情况下它的值与 [`buildConfig.sourceDir`](/zh/api/build-config#sourcedir) 相同,因此我们可以按照如下方式指定 `src/data.json` 文件为要复制的文件:
|
|
34
|
+
> 默认情况下,`buildConfig.sourceDir` 为 `src`
|
|
35
|
+
|
|
36
|
+
``` ts
|
|
37
|
+
export default defineConfig({
|
|
38
|
+
buildConfig: {
|
|
39
|
+
copy: {
|
|
40
|
+
patterns: [
|
|
41
|
+
{ from: './data.json' to: ''},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
当指定的文件不在源码目录的时候,可以修改 `context` 配置。例如指定项目目录下的 `temp/index.html` 为要复制的文件:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import path from 'path';
|
|
52
|
+
|
|
53
|
+
export default defineConfig({
|
|
54
|
+
buildConfig: {
|
|
55
|
+
copy: {
|
|
56
|
+
patterns: [
|
|
57
|
+
{
|
|
58
|
+
from: './index.html',
|
|
59
|
+
context: path.join(__dirname, './temp')
|
|
60
|
+
to: '',
|
|
61
|
+
}
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`patterns.to` 用于指定复制文件的输出路径,默认情况下它的值为 [`buildConfig.outdir`](zh/api/build-config#outdir)对应的值。因此我们按照如下方式将 `src/index.html` 复制到 `dist` 目录下:
|
|
69
|
+
|
|
70
|
+
``` ts
|
|
71
|
+
export default defineConfig({
|
|
72
|
+
buildConfig: {
|
|
73
|
+
copy: {
|
|
74
|
+
patterns: [
|
|
75
|
+
{ from: './index.html', }
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
当我们配置了 `patterns.to` 的时候:
|
|
83
|
+
|
|
84
|
+
* 如果是相对路径,则该路径会相对于 `buildConfig.outdir` 计算出复制文件输出的绝对路径。
|
|
85
|
+
* 如果是绝对路径,则会直接使用该值。
|
|
86
|
+
|
|
87
|
+
最后 `patterns.globOptions` 用于配置寻找复制文件 [globby](https://github.com/sindresorhus/globby) 对象,其配置可参考:
|
|
88
|
+
|
|
89
|
+
* [globby.options](https://github.com/sindresorhus/globby#options)
|
|
90
|
+
|
|
91
|
+
## 不同场景使用示例
|
|
92
|
+
|
|
93
|
+
### 将文件复制文件
|
|
94
|
+
|
|
95
|
+
``` ts
|
|
96
|
+
export default defineConfig({
|
|
97
|
+
buildConfig: [
|
|
98
|
+
{
|
|
99
|
+
outdir: 'dist',
|
|
100
|
+
copy: {
|
|
101
|
+
patterns: [
|
|
102
|
+
/**
|
|
103
|
+
* copy file to file
|
|
104
|
+
*/
|
|
105
|
+
{
|
|
106
|
+
from: './temp-1/a.png',
|
|
107
|
+
context: __dirname,
|
|
108
|
+
to: './temp-1/b.png',
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
]
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### 将文件复制到目录
|
|
118
|
+
|
|
119
|
+
``` ts
|
|
120
|
+
export default defineConfig({
|
|
121
|
+
buildConfig: [
|
|
122
|
+
{
|
|
123
|
+
outdir: 'dist',
|
|
124
|
+
copy: {
|
|
125
|
+
patterns: [
|
|
126
|
+
/**
|
|
127
|
+
* copy file to dir
|
|
128
|
+
*/
|
|
129
|
+
{
|
|
130
|
+
from: './temp-2/a.png',
|
|
131
|
+
context: __dirname,
|
|
132
|
+
to: './temp-2',
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 从目录复制到目录
|
|
142
|
+
|
|
143
|
+
``` ts
|
|
144
|
+
export default defineConfig({
|
|
145
|
+
buildConfig: [
|
|
146
|
+
{
|
|
147
|
+
outdir: 'dist',
|
|
148
|
+
copy: {
|
|
149
|
+
patterns: [
|
|
150
|
+
/**
|
|
151
|
+
* copy dir to dir
|
|
152
|
+
*/
|
|
153
|
+
{
|
|
154
|
+
from: './temp-3/',
|
|
155
|
+
to: './temp-3',
|
|
156
|
+
context: __dirname,
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
options: {
|
|
160
|
+
enableCopySync: true,
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### 从目录到文件
|
|
169
|
+
|
|
170
|
+
``` ts
|
|
171
|
+
export default defineConfig({
|
|
172
|
+
buildConfig: [
|
|
173
|
+
{
|
|
174
|
+
outdir: 'dist',
|
|
175
|
+
copy: {
|
|
176
|
+
patterns: [
|
|
177
|
+
/**
|
|
178
|
+
* copy dir to file
|
|
179
|
+
*/
|
|
180
|
+
{
|
|
181
|
+
from: './temp-4/',
|
|
182
|
+
context: __dirname,
|
|
183
|
+
to: './temp-4/_index.html',
|
|
184
|
+
},
|
|
185
|
+
],
|
|
186
|
+
options: {
|
|
187
|
+
enableCopySync: true,
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
});
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### 使用 Glob
|
|
196
|
+
|
|
197
|
+
``` ts
|
|
198
|
+
export default defineConfig({
|
|
199
|
+
buildConfig: [
|
|
200
|
+
{
|
|
201
|
+
outdir: 'dist',
|
|
202
|
+
copy: {
|
|
203
|
+
patterns: [
|
|
204
|
+
/**
|
|
205
|
+
* copy glob to dir
|
|
206
|
+
*/
|
|
207
|
+
{
|
|
208
|
+
from: './*.html',
|
|
209
|
+
to: './temp-5',
|
|
210
|
+
},
|
|
211
|
+
],
|
|
212
|
+
options: {
|
|
213
|
+
enableCopySync: true,
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
copy: {
|
|
219
|
+
patterns: [
|
|
220
|
+
/**
|
|
221
|
+
* copy glob to file
|
|
222
|
+
*/
|
|
223
|
+
{
|
|
224
|
+
from: './*.html',
|
|
225
|
+
to: './temp-6/index.html',
|
|
226
|
+
},
|
|
227
|
+
],
|
|
228
|
+
options: {
|
|
229
|
+
enableCopySync: true,
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
});
|
|
235
|
+
```
|