@modern-js/main-doc 2.42.0 → 2.42.2
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/guides/advanced-features/inline-assets.mdx +161 -0
- package/docs/en/guides/advanced-features/optimize-bundle.mdx +7 -6
- package/docs/en/guides/basic-features/alias.mdx +80 -2
- package/docs/en/guides/basic-features/css-modules.mdx +228 -0
- package/docs/en/guides/basic-features/json-files.mdx +106 -0
- package/docs/en/guides/basic-features/output-files.mdx +173 -0
- package/docs/en/guides/basic-features/static-assets.mdx +165 -0
- package/docs/en/guides/basic-features/svg-assets.mdx +155 -0
- package/docs/en/guides/basic-features/wasm-assets.mdx +66 -0
- package/docs/en/guides/get-started/quick-start.mdx +1 -1
- package/docs/zh/guides/advanced-features/inline-assets.mdx +162 -0
- package/docs/zh/guides/advanced-features/optimize-bundle.mdx +7 -6
- package/docs/zh/guides/basic-features/alias.mdx +80 -2
- package/docs/zh/guides/basic-features/css-modules.mdx +229 -0
- package/docs/zh/guides/basic-features/json-files.mdx +106 -0
- package/docs/zh/guides/basic-features/output-files.mdx +173 -0
- package/docs/zh/guides/basic-features/static-assets.mdx +165 -0
- package/docs/zh/guides/basic-features/svg-assets.mdx +157 -0
- package/docs/zh/guides/basic-features/wasm-assets.mdx +66 -0
- package/docs/zh/guides/get-started/quick-start.mdx +1 -1
- package/package.json +7 -7
@@ -0,0 +1,173 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# Output Files
|
6
|
+
|
7
|
+
This chapter will introduces the directory structure of output files and how to control the output directory of different types of files.
|
8
|
+
|
9
|
+
## Default Directory Structure
|
10
|
+
|
11
|
+
The following is a basic directory for output files. By default, the compiled files will be output in the `dist` directory of current project.
|
12
|
+
|
13
|
+
```bash
|
14
|
+
dist
|
15
|
+
├── static
|
16
|
+
│ ├── css
|
17
|
+
│ │ ├── [name].[hash].css
|
18
|
+
│ │ └── [name].[hash].css.map
|
19
|
+
│ │
|
20
|
+
│ └── js
|
21
|
+
│ ├── [name].[hash].js
|
22
|
+
│ ├── [name].[hash].js.LICENSE.txt
|
23
|
+
│ └── [name].[hash].js.map
|
24
|
+
│
|
25
|
+
└── html
|
26
|
+
└── [name]
|
27
|
+
└── index.html
|
28
|
+
```
|
29
|
+
|
30
|
+
The most common output files are HTML files, JS files, and CSS files:
|
31
|
+
|
32
|
+
- HTML files: default output to the `html` directory.
|
33
|
+
- JS files: default output to `static/js` directory,
|
34
|
+
- CSS files: default output to the `static/css` directory.
|
35
|
+
|
36
|
+
In addition, JS files and CSS files sometimes generate some related files:
|
37
|
+
|
38
|
+
- License files: contains open source license, which is output to the same level directory of the JS file, and adds `.LICENSE.txt` suffix.
|
39
|
+
- Source Map files: contains the source code mappings, which is output to the same level directory of JS files and CSS files, and adds a `.map` suffix.
|
40
|
+
|
41
|
+
In the filename, `[name]` represents the entry name corresponding to this file, such as `index`, `main`. `[hash]` is the hash value generated based on the content of the file.
|
42
|
+
|
43
|
+
## Modify the Directory
|
44
|
+
|
45
|
+
Builder provides some configs to modify the directory or filename, you can:
|
46
|
+
|
47
|
+
- Modify the filename through [output.filename](/configure/app/output/filename).
|
48
|
+
- Modify the output path of through [output.distPath](/configure/app/output/dist-path).
|
49
|
+
- Modify the license file through [output.legalComments](/configure/app/output/legal-comments).
|
50
|
+
- Remove Source Map file through [output.disableSourceMap](/configure/app/output/disable-source-map).
|
51
|
+
- Remove the folder corresponding to the HTML files through [html.disableHtmlFolder](/configure/app/html/disable-html-folder).
|
52
|
+
|
53
|
+
## Static Assets
|
54
|
+
|
55
|
+
When you import static assets such as images, SVG, fonts, media, etc. in the code, they will also be output to the `dist/static` directory, and automatically assigned to the corresponding subdirectories according to the file type:
|
56
|
+
|
57
|
+
```bash
|
58
|
+
dist
|
59
|
+
└── static
|
60
|
+
├── image
|
61
|
+
│ └── foo.[hash].png
|
62
|
+
│
|
63
|
+
├── svg
|
64
|
+
│ └── bar.[hash].svg
|
65
|
+
│
|
66
|
+
├── font
|
67
|
+
│ └── baz.[hash].woff2
|
68
|
+
│
|
69
|
+
└── media
|
70
|
+
└── qux.[hash].mp4
|
71
|
+
```
|
72
|
+
|
73
|
+
You can use the [output.distPath](/configure/app/output/dist-path) config to uniformly input these static assets into a directory, for example, output to the `assets` directory:
|
74
|
+
|
75
|
+
```ts
|
76
|
+
export default {
|
77
|
+
output: {
|
78
|
+
distPath: {
|
79
|
+
image: 'assets',
|
80
|
+
svg: 'assets',
|
81
|
+
font: 'assets',
|
82
|
+
media: 'assets',
|
83
|
+
},
|
84
|
+
},
|
85
|
+
};
|
86
|
+
```
|
87
|
+
|
88
|
+
The above config produces the following directory structure:
|
89
|
+
|
90
|
+
```bash
|
91
|
+
dist
|
92
|
+
└── assets
|
93
|
+
├── foo.[hash].png
|
94
|
+
├── bar.[hash].svg
|
95
|
+
├── baz.[hash].woff2
|
96
|
+
└── qux.[hash].mp4
|
97
|
+
```
|
98
|
+
|
99
|
+
## Node.js Output Directory
|
100
|
+
|
101
|
+
When you enable SSR or SSG features in Modern.js, Modern.js will generate a Node.js bundle after the build and output it to the `bundles` directory.
|
102
|
+
|
103
|
+
```bash
|
104
|
+
dist
|
105
|
+
├── bundles
|
106
|
+
│ └── [name].js
|
107
|
+
├── static
|
108
|
+
└── html
|
109
|
+
```
|
110
|
+
|
111
|
+
Node.js files usually only contain JS files, no HTML or CSS. Also, JS file names will not contain hash.
|
112
|
+
|
113
|
+
You can modify the output path of Node.js files through the [output.distPath.server](/configure/app/output/dist-path) config.
|
114
|
+
|
115
|
+
For example, output Node.js files to the `server` directory:
|
116
|
+
|
117
|
+
```ts
|
118
|
+
export default {
|
119
|
+
output: {
|
120
|
+
distPath: {
|
121
|
+
server: 'server',
|
122
|
+
},
|
123
|
+
},
|
124
|
+
};
|
125
|
+
```
|
126
|
+
|
127
|
+
## Flatten the Directory
|
128
|
+
|
129
|
+
Sometimes you don't want the dist directory to have too many levels, you can set the directory to an empty string to flatten the generated directory.
|
130
|
+
|
131
|
+
See the example below:
|
132
|
+
|
133
|
+
```ts
|
134
|
+
export default {
|
135
|
+
output: {
|
136
|
+
distPath: {
|
137
|
+
js: '',
|
138
|
+
css: '',
|
139
|
+
html: '',
|
140
|
+
},
|
141
|
+
},
|
142
|
+
html: {
|
143
|
+
disableHtmlFolder: true,
|
144
|
+
}
|
145
|
+
};
|
146
|
+
```
|
147
|
+
|
148
|
+
The above config produces the following directory structure:
|
149
|
+
|
150
|
+
```bash
|
151
|
+
dist
|
152
|
+
├── [name].[hash].css
|
153
|
+
├── [name].[hash].css.map
|
154
|
+
├── [name].[hash].js
|
155
|
+
├── [name].[hash].js.map
|
156
|
+
└── [name].html
|
157
|
+
```
|
158
|
+
|
159
|
+
## Not Written to Disk
|
160
|
+
|
161
|
+
By default, Modern.js will write the generated files to disk, so developers can view the file content or configure proxy rules for static assets.
|
162
|
+
|
163
|
+
In development, you can choose to keep the generated files in the Dev Server's memory to reduce the overhead of file operations.
|
164
|
+
|
165
|
+
Just set the `dev.writeToDisk` config to `false`:
|
166
|
+
|
167
|
+
```ts
|
168
|
+
export default {
|
169
|
+
dev: {
|
170
|
+
writeToDisk: false,
|
171
|
+
},
|
172
|
+
};
|
173
|
+
```
|
@@ -0,0 +1,165 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# Import Static Assets
|
6
|
+
|
7
|
+
Modern.js supports import static assets, including images, fonts, and medias.
|
8
|
+
|
9
|
+
:::tip What is Static Assets
|
10
|
+
Static assets are files that are part of a web application and do not change, even when the application is being used. Examples of static assets include images, fonts, medias, stylesheets, and JavaScript files. These assets are typically stored on a web server or CDN, and delivered to the user's web browser when the Web application is accessed. Because they do not change, static assets can be cached by the browser, which helps to improve the performance of the Web application.
|
11
|
+
:::
|
12
|
+
|
13
|
+
## Assets Format
|
14
|
+
|
15
|
+
The following are the formats supported by Modern.js by default:
|
16
|
+
|
17
|
+
- **Image**: png, jpg, jpeg, gif, svg, bmp, webp, ico, apng, avif, tiff.
|
18
|
+
- **Fonts**: woff, woff2, eot, ttf, otf, ttc.
|
19
|
+
- **Media**: mp4, webm, ogg, mp3, wav, flac, aac, mov.
|
20
|
+
|
21
|
+
If you need to import assets in other formats, please refer to [Extend Asset Types](#extend-asset-types).
|
22
|
+
|
23
|
+
:::tip SVG images
|
24
|
+
SVG image is a special case. Modern.js support convert SVG to React components, so SVG is processed separately. For details, see [Import SVG Assets](/guides/basic-features/svg-assets.html).
|
25
|
+
:::
|
26
|
+
|
27
|
+
## Import Assets in JS file
|
28
|
+
|
29
|
+
In JS files, you can directly import static assets in relative paths:
|
30
|
+
|
31
|
+
```tsx
|
32
|
+
// Import the logo.png image in the static directory
|
33
|
+
import logo from './static/logo.png';
|
34
|
+
|
35
|
+
export default = () => <img src={logo} />;
|
36
|
+
```
|
37
|
+
|
38
|
+
Import with [alias](/guides/basic-features/alias.html) are also supported:
|
39
|
+
|
40
|
+
```tsx
|
41
|
+
import logo from '@/static/logo.png';
|
42
|
+
|
43
|
+
export default = () => <img src={logo} />;
|
44
|
+
```
|
45
|
+
|
46
|
+
## Import Assets in CSS file
|
47
|
+
|
48
|
+
In CSS files, you can reference static assets in relative paths:
|
49
|
+
|
50
|
+
```css
|
51
|
+
.logo {
|
52
|
+
background-image: url('../static/logo.png');
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
56
|
+
Import with [alias](/guides/basic-features/alias.html) are also supported:
|
57
|
+
|
58
|
+
```css
|
59
|
+
.logo {
|
60
|
+
background-image: url('@/static/logo.png');
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
## Import Results
|
65
|
+
|
66
|
+
The result of importing static assets depends on the file size:
|
67
|
+
|
68
|
+
- When the file size is greater than 10KB, a URL will be returned, and the file will be output to the dist directory.
|
69
|
+
- When the file size is less than 10KB, it will be automatically inlined to Base64 format.
|
70
|
+
|
71
|
+
```js
|
72
|
+
import largeImage from './static/largeImage.png';
|
73
|
+
import smallImage from './static/smallImage.png';
|
74
|
+
|
75
|
+
console.log(largeImage); // "/static/largeImage.6c12aba3.png"
|
76
|
+
console.log(smallImage); // "data:image/png;base64,iVBORw0KGgo..."
|
77
|
+
```
|
78
|
+
|
79
|
+
For a more detailed introduction to asset inlining, please refer to the [Static Asset Inlining](/guides/advanced-features/inline-assets) chapter.
|
80
|
+
|
81
|
+
## Output Files
|
82
|
+
|
83
|
+
When static assets are imported, they will be output to the dist directory. You can:
|
84
|
+
|
85
|
+
- Modify the output filename through [output.filename](/configure/app/output/filename).
|
86
|
+
- Modify the output path through [output.distPath](/configure/app/output/dist-path).
|
87
|
+
|
88
|
+
Please read [Output Files](/guides/basic-features/output-files) for details.
|
89
|
+
|
90
|
+
## URL Prefix
|
91
|
+
|
92
|
+
The URL returned after importing a asset will automatically include the path prefix:
|
93
|
+
|
94
|
+
- In development, using [dev.assetPrefix](/configure/app/dev/asset-prefix) to set the path prefix.
|
95
|
+
- In production, using [output.assetPrefix](/configure/app/output/asset-prefix) to set the path prefix.
|
96
|
+
|
97
|
+
For example, you can set `output.assetPrefix` to `https://modern.com`:
|
98
|
+
|
99
|
+
```js
|
100
|
+
import logo from './static/logo.png';
|
101
|
+
|
102
|
+
console.log(logo); // "https://modern.com/static/logo.6c12aba3.png"
|
103
|
+
```
|
104
|
+
|
105
|
+
## Add Type Declaration
|
106
|
+
|
107
|
+
When you import static assets in TypeScript code, TypeScript may prompt that the module is missing a type definition:
|
108
|
+
|
109
|
+
```
|
110
|
+
TS2307: Cannot find module './logo.png' or its corresponding type declarations.
|
111
|
+
```
|
112
|
+
|
113
|
+
To fix this, you need to add a type declaration file for the static assets, please create a `src/global.d.ts` file, and add the corresponding type declaration. Taking png images as an example, you need to add the following declarations:
|
114
|
+
|
115
|
+
```ts title="src/global.d.ts"
|
116
|
+
declare module '*.png' {
|
117
|
+
const content: string;
|
118
|
+
export default content;
|
119
|
+
}
|
120
|
+
```
|
121
|
+
|
122
|
+
After adding the type declaration, if the type error still exists, you can try to restart the current IDE, or adjust the directory where `global.d.ts` is located, making sure the TypeScript can correctly identify the type definition.
|
123
|
+
|
124
|
+
## Extend Asset Types
|
125
|
+
|
126
|
+
If the built-in asset types in Modern.js cannot meet your requirements, you can modify the built-in webpack/Rspack configuration and extend the asset types you need using [tools.bundlerChain](/configure/app/tools/bundler-chain).
|
127
|
+
|
128
|
+
For example, if you want to treat `*.pdf` files as assets and directly output them to the dist directory, you can add the following configuration:
|
129
|
+
|
130
|
+
```ts
|
131
|
+
export default {
|
132
|
+
tools: {
|
133
|
+
bundlerChain(chain) {
|
134
|
+
chain.module
|
135
|
+
.rule('pdf')
|
136
|
+
.test(/\.pdf$/)
|
137
|
+
.type('asset/resource');
|
138
|
+
},
|
139
|
+
},
|
140
|
+
};
|
141
|
+
```
|
142
|
+
|
143
|
+
After adding the above configuration, you can import `*.pdf` files in your code, for example:
|
144
|
+
|
145
|
+
```js
|
146
|
+
import myFile from './static/myFile.pdf';
|
147
|
+
|
148
|
+
console.log(myFile); // "/static/myFile.6c12aba3.pdf"
|
149
|
+
```
|
150
|
+
|
151
|
+
For more information about asset modules, please refer to:
|
152
|
+
|
153
|
+
- [Rspack Documentation - Asset modules](https://www.rspack.dev/guide/asset-module.html#asset-modules)
|
154
|
+
- [webpack Documentation - Asset modules](https://webpack.js.org/guides/asset-modules/)
|
155
|
+
|
156
|
+
## Image Format
|
157
|
+
|
158
|
+
When using image assets, you can choose a appropriate image format according to the pros and cons in the table below.
|
159
|
+
|
160
|
+
| Format | Pros | Cons | Scenarios |
|
161
|
+
| ------ | --------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
162
|
+
| PNG | Lossless compression, no loss of picture details, no distortion, support for translucency | Not suitable for pictures with complex color tables | Suitable for pictures with few colors and well-defined borders, suitable for logos, icons, transparent images and other scenes |
|
163
|
+
| JPG | Rich colors | Lossy compression, which will cause image distortion, does not support transparency | Suitable for pictures with a large number of colors, gradients, and overly complex pictures, suitable for portrait photos, landscapes and other scenes |
|
164
|
+
| WebP | Supports both lossy and lossless compression, supports transparency, and is much smaller than PNG and JPG | iOS compatibility is not good | Pixel images of almost any scene, and the hosting environment that supports WebP, should prefer WebP image format |
|
165
|
+
| SVG | Lossless format, no distortion, supports transparency | Not suitable for complex graphics | Suitable for vector graphics, suitable for icons |
|
@@ -0,0 +1,155 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 11
|
3
|
+
---
|
4
|
+
|
5
|
+
# Import SVG Assets
|
6
|
+
|
7
|
+
Modern.js supports import SVG assets and transform SVG into React components or URLs.
|
8
|
+
|
9
|
+
:::tip What is SVG
|
10
|
+
SVG stands for Scalable Vector Graphics. It is a type of image format that uses vector graphics to represent images. Vector graphics are different from raster graphics, which are made up of pixels. Instead, vector graphics use geometric shapes, lines, and curves to represent images. Because vector graphics are not made up of pixels, they can be scaled to any size without losing resolution or quality.
|
11
|
+
:::
|
12
|
+
|
13
|
+
## Import SVG in JS file
|
14
|
+
|
15
|
+
When import an SVG in a JS file, if you import `ReactComponent`, Modern.js will call [SVGR](https://react-svgr.com/) to convert the SVG into a React component.
|
16
|
+
|
17
|
+
```tsx title="src/component/Logo.tsx"
|
18
|
+
import { ReactComponent as Logo } from './static/logo.svg';
|
19
|
+
|
20
|
+
export default () => <Logo />;
|
21
|
+
```
|
22
|
+
|
23
|
+
If you use the default import, then the SVG will be treated as a normal static asset and you will get a URL:
|
24
|
+
|
25
|
+
```tsx title="src/component/Logo.tsx"
|
26
|
+
import logoURL from './static/logo.svg';
|
27
|
+
|
28
|
+
console.log(logoURL); // => "/static/logo.6c12aba3.png"
|
29
|
+
```
|
30
|
+
|
31
|
+
## Modify the Default Export
|
32
|
+
|
33
|
+
You can modify the default export of SVG files through the [output.svgDefaultExport](/configure/app/output/svg-default-export) config. For example, set the default export as a React component:
|
34
|
+
|
35
|
+
```ts
|
36
|
+
export default {
|
37
|
+
output: {
|
38
|
+
svgDefaultExport: 'component',
|
39
|
+
},
|
40
|
+
};
|
41
|
+
```
|
42
|
+
|
43
|
+
Then import the SVG, you'll get a React component instead of a URL:
|
44
|
+
|
45
|
+
```tsx title="src/component/Logo.tsx"
|
46
|
+
import Logo from './static/logo.svg';
|
47
|
+
|
48
|
+
export default () => <Logo />;
|
49
|
+
```
|
50
|
+
|
51
|
+
## Import SVG in CSS file
|
52
|
+
|
53
|
+
When import an SVG in a CSS file, the SVG is treated as a normal static asset and you will get a URL:
|
54
|
+
|
55
|
+
```css
|
56
|
+
.logo {
|
57
|
+
background-image: url('../static/logo.svg');
|
58
|
+
}
|
59
|
+
```
|
60
|
+
|
61
|
+
## Assets Processing
|
62
|
+
|
63
|
+
When SVG is imported not as a React component but as a normal static asset, it is processed exactly the same as other static assets, and it is also affected by rules such as assets inlining and URL prefixing.
|
64
|
+
|
65
|
+
Please read the [Import Static Assets](/guides/basic-features/static-assets) section to understand the processing rules for static assets.
|
66
|
+
|
67
|
+
## Disable SVGR Processing
|
68
|
+
|
69
|
+
By default, when an SVG resource is referenced in a JS file, Modern.js will call SVGR to convert the SVG into a React component. If you are sure that all SVG resources in your project are not being used as React components, you can turn off this conversion by setting [disableSvgr](/configure/app/output/disable-svgr) to true to improve build performance.
|
70
|
+
|
71
|
+
```js
|
72
|
+
export default {
|
73
|
+
output: {
|
74
|
+
disableSvgr: true,
|
75
|
+
},
|
76
|
+
};
|
77
|
+
```
|
78
|
+
|
79
|
+
## Add type declaration
|
80
|
+
|
81
|
+
When you reference an SVG asset in TypeScript code, TypeScript may prompt that the module is missing a type definition:
|
82
|
+
|
83
|
+
```
|
84
|
+
TS2307: Cannot find module './logo.svg' or its corresponding type declarations.
|
85
|
+
```
|
86
|
+
|
87
|
+
To fix this, you need to add a type declaration file for the SVG asset, please create a `src/global.d.ts` file, and add the following type declaration:
|
88
|
+
|
89
|
+
```ts
|
90
|
+
declare module '*.svg' {
|
91
|
+
export const ReactComponent: React.FunctionComponent<
|
92
|
+
React.SVGProps<SVGSVGElement>
|
93
|
+
>;
|
94
|
+
|
95
|
+
const content: string;
|
96
|
+
export default content;
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
If you set `svgDefaultExport` to `'component'`, then change the type declaration to:
|
101
|
+
|
102
|
+
```ts
|
103
|
+
declare module '*.svg' {
|
104
|
+
export const ReactComponent: React.FunctionComponent<
|
105
|
+
React.SVGProps<SVGSVGElement>
|
106
|
+
>;
|
107
|
+
|
108
|
+
export default ReactComponent;
|
109
|
+
}
|
110
|
+
```
|
111
|
+
|
112
|
+
After adding the type declaration, if the type error still exists, you can try to restart the current IDE, or adjust the directory where `global.d.ts` is located, making sure the TypeScript can correctly identify the type definition.
|
113
|
+
|
114
|
+
## Modify the SVGR configuration
|
115
|
+
|
116
|
+
When SVGR is enabled, its default configuration is as follows:
|
117
|
+
|
118
|
+
```js
|
119
|
+
{
|
120
|
+
svgo: true,
|
121
|
+
svgoConfig: {
|
122
|
+
plugins: [
|
123
|
+
{
|
124
|
+
name: 'preset-default',
|
125
|
+
params: {
|
126
|
+
overrides: {
|
127
|
+
removeViewBox: false,
|
128
|
+
},
|
129
|
+
},
|
130
|
+
},
|
131
|
+
'prefixIds',
|
132
|
+
],
|
133
|
+
},
|
134
|
+
}
|
135
|
+
```
|
136
|
+
|
137
|
+
If you need to modify the SVGR configuration, you can do the following:
|
138
|
+
|
139
|
+
```js
|
140
|
+
export default {
|
141
|
+
tools: {
|
142
|
+
bundlerChain: (chain, { CHAIN_ID }) => {
|
143
|
+
chain.module
|
144
|
+
.rule(CHAIN_ID.RULE.SVG)
|
145
|
+
.oneOf(CHAIN_ID.ONE_OF.SVG)
|
146
|
+
.use(CHAIN_ID.USE.SVGR)
|
147
|
+
.tap(options => {
|
148
|
+
// modify svgoConfig
|
149
|
+
options.svgoConfig.plugins[0].params.overrides.removeUselessDefs = false;
|
150
|
+
return options;
|
151
|
+
});
|
152
|
+
},
|
153
|
+
},
|
154
|
+
};
|
155
|
+
```
|
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 13
|
3
|
+
---
|
4
|
+
|
5
|
+
# Import Wasm Assets
|
6
|
+
|
7
|
+
Modern.js supports import WebAssembly assets in code.
|
8
|
+
|
9
|
+
:::tip What is WebAssembly
|
10
|
+
WebAssembly (Wasm) is a portable, high-performance binary format designed to execute CPU-intensive computing tasks in modern web browsers, bringing performance and reliability similar to native compiled code to the web platform.
|
11
|
+
:::
|
12
|
+
|
13
|
+
## Import
|
14
|
+
|
15
|
+
You can import a WebAssembly module directly in a JavaScript file:
|
16
|
+
|
17
|
+
```js title="index.js"
|
18
|
+
import { add } from './add.wasm';
|
19
|
+
|
20
|
+
console.log(add); // [native code]
|
21
|
+
console.log(add(1, 2)); // 3
|
22
|
+
```
|
23
|
+
|
24
|
+
WebAssembly modules can also be imported via dynamic import:
|
25
|
+
|
26
|
+
```js title="index.js"
|
27
|
+
import('./add.wasm').then(({ add }) => {
|
28
|
+
console.log('---- Async Wasm Module');
|
29
|
+
console.log(add); // [native code]
|
30
|
+
console.log(add(1, 2)); // 3
|
31
|
+
});
|
32
|
+
```
|
33
|
+
|
34
|
+
You can also get the path of a WebAssembly module using the `new URL` syntax:
|
35
|
+
|
36
|
+
```js title="index.js"
|
37
|
+
const wasmURL = new URL('./add.wasm', import.meta.url);
|
38
|
+
|
39
|
+
console.log(wasmURL).pathname; // "/static/wasm/[hash].module.wasm"
|
40
|
+
```
|
41
|
+
|
42
|
+
## Output Directory
|
43
|
+
|
44
|
+
When a `.wasm` asset is imported, it will be output by Modern.js to the `dist/static/wasm` directory by default.
|
45
|
+
|
46
|
+
You can change the output directory of `.wasm` files via [output.distPath](/configure/app/output/dist-path) config.
|
47
|
+
|
48
|
+
```ts
|
49
|
+
export default {
|
50
|
+
output: {
|
51
|
+
distPath: {
|
52
|
+
wasm: 'resource/wasm',
|
53
|
+
},
|
54
|
+
},
|
55
|
+
};
|
56
|
+
```
|
57
|
+
|
58
|
+
## Add Type Declaration
|
59
|
+
|
60
|
+
When you import a Wasm file in TypeScript code, you usually need to add the corresponding type declaration.
|
61
|
+
|
62
|
+
For example, the `add.wasm` file exports an `add()` method, then you can create an `add.wasm.d.ts` file in the same directory and add the corresponding type declaration:
|
63
|
+
|
64
|
+
```ts title="add.wasm.d.ts"
|
65
|
+
export const add = (num1: number, num2: number) => number;
|
66
|
+
```
|
@@ -116,7 +116,7 @@ dist
|
|
116
116
|
└── js
|
117
117
|
```
|
118
118
|
|
119
|
-
> If you need to customize the directory of the build artifacts, please refer to [Output files](
|
119
|
+
> If you need to customize the directory of the build artifacts, please refer to [Output files](/guides/basic-features/output-files).
|
120
120
|
|
121
121
|
## Verify
|
122
122
|
|