@modern-js/main-doc 2.42.1 → 2.43.0
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/apis/app/runtime/web-server/middleware.mdx +3 -3
- package/docs/en/configure/app/server/ssr.mdx +2 -0
- package/docs/en/guides/advanced-features/inline-assets.mdx +161 -0
- package/docs/en/guides/advanced-features/optimize-bundle.mdx +8 -7
- package/docs/en/guides/advanced-features/ssr/_category_.json +8 -0
- package/docs/en/guides/advanced-features/ssr/cache.mdx +186 -0
- package/docs/en/guides/advanced-features/ssr/index.mdx +22 -0
- package/docs/en/guides/advanced-features/ssr/stream.mdx +236 -0
- package/docs/en/guides/advanced-features/ssr/usage.mdx +341 -0
- 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/css.mdx +2 -13
- 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/en/guides/get-started/tech-stack.mdx +1 -1
- package/docs/en/guides/topic-detail/framework-plugin/introduction.mdx +63 -16
- package/docs/zh/apis/app/runtime/web-server/middleware.mdx +4 -4
- package/docs/zh/configure/app/server/ssr.mdx +2 -0
- package/docs/zh/guides/advanced-features/inline-assets.mdx +162 -0
- package/docs/zh/guides/advanced-features/optimize-bundle.mdx +8 -7
- package/docs/zh/guides/advanced-features/ssr/_category_.json +8 -0
- package/docs/zh/guides/advanced-features/ssr/cache.mdx +189 -0
- package/docs/zh/guides/advanced-features/ssr/index.mdx +22 -0
- package/docs/zh/guides/advanced-features/ssr/stream.mdx +240 -0
- package/docs/zh/guides/advanced-features/{ssr.mdx → ssr/usage.mdx} +7 -225
- 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/css.mdx +2 -13
- package/docs/zh/guides/basic-features/data/data-write.mdx +1 -1
- 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/docs/zh/guides/get-started/tech-stack.mdx +1 -1
- package/docs/zh/guides/topic-detail/framework-plugin/introduction.mdx +61 -16
- package/package.json +7 -7
@@ -0,0 +1,228 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 14
|
3
|
+
---
|
4
|
+
|
5
|
+
# Use CSS Modules
|
6
|
+
|
7
|
+
[CSS Modules](https://github.com/css-modules/css-modules) allows us to write CSS code in a modular way, and these styles can be imported and used in JavaScript files. Using CSS Modules can automatically generate unique class names, isolate styles between different modules, and avoid class name conflicts.
|
8
|
+
|
9
|
+
Builder supports CSS Modules by default, you don't need to add additional configuration. Our convention is to use the `[name].module.css` filename to enable CSS Modules.
|
10
|
+
|
11
|
+
The following style files are considered CSS Modules:
|
12
|
+
|
13
|
+
- `*.module.scss`
|
14
|
+
- `*.module.less`
|
15
|
+
- `*.module.css`
|
16
|
+
|
17
|
+
## Example
|
18
|
+
|
19
|
+
- Write style:
|
20
|
+
|
21
|
+
```css
|
22
|
+
/* button.module.css */
|
23
|
+
.error {
|
24
|
+
background: red;
|
25
|
+
}
|
26
|
+
```
|
27
|
+
|
28
|
+
- Using styles:
|
29
|
+
|
30
|
+
```tsx
|
31
|
+
// Button.tsx
|
32
|
+
import React, { Component } from 'react';
|
33
|
+
// import style file
|
34
|
+
import styles from './button.module.css';
|
35
|
+
|
36
|
+
export default () => {
|
37
|
+
return <button className={styles.error}>Error Button</button>;
|
38
|
+
};
|
39
|
+
```
|
40
|
+
|
41
|
+
## Enable CSS Modules for all CSS files
|
42
|
+
|
43
|
+
By default, only files ending in `*.module.css` are treated CSS Modules.
|
44
|
+
|
45
|
+
If you want to treat all CSS files in the source directory as CSS Modules, you can enable the [output.disableCssModuleExtension](/configure/app/output/disable-css-module-extension) config, for example:
|
46
|
+
|
47
|
+
```ts
|
48
|
+
export default {
|
49
|
+
output: {
|
50
|
+
disableCssModuleExtension: true,
|
51
|
+
},
|
52
|
+
};
|
53
|
+
```
|
54
|
+
|
55
|
+
When set, the following two files are treated as CSS Modules:
|
56
|
+
|
57
|
+
```ts
|
58
|
+
import styles1 from './foo.module.css';
|
59
|
+
import styles2 from './bar.css';
|
60
|
+
```
|
61
|
+
|
62
|
+
:::tip
|
63
|
+
We do not recommend enabling this config, because after enabling disableCssModuleExtension, CSS Modules files and ordinary CSS files cannot be clearly distinguished, which is not conducive to long-term maintenance.
|
64
|
+
:::
|
65
|
+
|
66
|
+
## Enable CSS Modules for the specified style file
|
67
|
+
|
68
|
+
By default, only files ending in `*.module.css` are treated CSS Modules.
|
69
|
+
|
70
|
+
If you want to enable CSS Modules only for specified style files, you can configure [output.cssModules](/configure/app/output/css-modules), for example:
|
71
|
+
|
72
|
+
```ts
|
73
|
+
export default {
|
74
|
+
output: {
|
75
|
+
cssModules: {
|
76
|
+
auto: resource => {
|
77
|
+
return resource.includes('.module.') || resource.includes('shared/');
|
78
|
+
},
|
79
|
+
},
|
80
|
+
},
|
81
|
+
};
|
82
|
+
```
|
83
|
+
|
84
|
+
## Custom Class Names
|
85
|
+
|
86
|
+
Customizing the class names generated by CSS Modules is also a commonly used function, you can use [output.cssModuleLocalIdentName](/configure/app/output/css-modules.html#cssmodulesexportlocalsconvention) to configure it.
|
87
|
+
|
88
|
+
```ts
|
89
|
+
export default {
|
90
|
+
output: {
|
91
|
+
cssModuleLocalIdentName: '[hash:base64:4]',
|
92
|
+
},
|
93
|
+
};
|
94
|
+
```
|
95
|
+
|
96
|
+
If you need to customize other configs of CSS Modules, you can set them via [tools.cssLoader](/configure/app/tools/css-loader).
|
97
|
+
|
98
|
+
## Add Type Declaration
|
99
|
+
|
100
|
+
When you import CSS Modules in TypeScript code, TypeScript may prompt that the module is missing a type definition:
|
101
|
+
|
102
|
+
```
|
103
|
+
TS2307: Cannot find module './index.module.css' or its corresponding type declarations.
|
104
|
+
```
|
105
|
+
|
106
|
+
To fix this, you need to add a type declaration file for the CSS Modules, please create a `src/global.d.ts` file, and add the corresponding type declaration:
|
107
|
+
|
108
|
+
```ts title="src/global.d.ts"
|
109
|
+
declare module '*.module.css' {
|
110
|
+
const classes: { readonly [key: string]: string };
|
111
|
+
export default classes;
|
112
|
+
}
|
113
|
+
|
114
|
+
declare module '*.module.scss' {
|
115
|
+
const classes: { readonly [key: string]: string };
|
116
|
+
export default classes;
|
117
|
+
}
|
118
|
+
|
119
|
+
declare module '*.module.sass' {
|
120
|
+
const classes: { readonly [key: string]: string };
|
121
|
+
export default classes;
|
122
|
+
}
|
123
|
+
|
124
|
+
declare module '*.module.less' {
|
125
|
+
const classes: { readonly [key: string]: string };
|
126
|
+
export default classes;
|
127
|
+
}
|
128
|
+
|
129
|
+
declare module '*.module.styl' {
|
130
|
+
const classes: { readonly [key: string]: string };
|
131
|
+
export default classes;
|
132
|
+
}
|
133
|
+
```
|
134
|
+
|
135
|
+
If you enabled the `disableCssModuleExtension` config, you also need to add the following types:
|
136
|
+
|
137
|
+
```ts title="src/global.d.ts"
|
138
|
+
declare module '*.css' {
|
139
|
+
const classes: { readonly [key: string]: string };
|
140
|
+
export default classes;
|
141
|
+
}
|
142
|
+
|
143
|
+
declare module '*.scss' {
|
144
|
+
const classes: { readonly [key: string]: string };
|
145
|
+
export default classes;
|
146
|
+
}
|
147
|
+
|
148
|
+
declare module '*.sass' {
|
149
|
+
const classes: { readonly [key: string]: string };
|
150
|
+
export default classes;
|
151
|
+
}
|
152
|
+
|
153
|
+
declare module '*.less' {
|
154
|
+
const classes: { readonly [key: string]: string };
|
155
|
+
export default classes;
|
156
|
+
}
|
157
|
+
|
158
|
+
declare module '*.styl' {
|
159
|
+
const classes: { readonly [key: string]: string };
|
160
|
+
export default classes;
|
161
|
+
}
|
162
|
+
```
|
163
|
+
|
164
|
+
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.
|
165
|
+
|
166
|
+
## Generate exact type definitions
|
167
|
+
|
168
|
+
Although the above method can provide the type of CSS Modules, it cannot accurately prompt which classNames are exported by a certain CSS file.
|
169
|
+
|
170
|
+
Builder supports generating accurate type declarations for CSS Modules, you only need to enable the [output.enableCssModuleTSDeclaration](/configure/app/output/enable-css-module-tsdeclaration) config, and then execute the build, Modern.js will generate type declaration files for all CSS Modules.
|
171
|
+
|
172
|
+
```ts
|
173
|
+
export default {
|
174
|
+
output: {
|
175
|
+
enableCssModuleTSDeclaration: true,
|
176
|
+
},
|
177
|
+
};
|
178
|
+
```
|
179
|
+
|
180
|
+
### Example
|
181
|
+
|
182
|
+
For example, there are two files `src/index.ts` and `src/index.module.scss` under a certain folder:
|
183
|
+
|
184
|
+
```tsx title="src/index.ts"
|
185
|
+
import styles from './index.module.scss';
|
186
|
+
|
187
|
+
export default () => {
|
188
|
+
<div>
|
189
|
+
<div className={styles.pageHeader}>Page Header</div>
|
190
|
+
</div>;
|
191
|
+
};
|
192
|
+
```
|
193
|
+
|
194
|
+
```scss title="src/index.module.scss"
|
195
|
+
.page-header {
|
196
|
+
color: black;
|
197
|
+
}
|
198
|
+
```
|
199
|
+
|
200
|
+
After executing the build, the `src/index.module.scss.d.ts` type declaration file will be automatically generated:
|
201
|
+
|
202
|
+
```ts title="src/index.module.scss.d.ts"
|
203
|
+
// This file is automatically generated.
|
204
|
+
// Please do not change this file!
|
205
|
+
interface CssExports {
|
206
|
+
'page-header': string;
|
207
|
+
pageHeader: string;
|
208
|
+
}
|
209
|
+
export const cssExports: CssExports;
|
210
|
+
export default cssExports;
|
211
|
+
```
|
212
|
+
|
213
|
+
Then open the `src/index.ts` file again, you will see that the `styles` object already has a exact type.
|
214
|
+
|
215
|
+
### Related configuration
|
216
|
+
|
217
|
+
In the above example, `src/index.module.scss.d.ts` is generated by compilation, you can choose to commit them to the Git repository, or you can choose to ignore them in the `.gitignore` file:
|
218
|
+
|
219
|
+
```bash
|
220
|
+
# Ignore auto generated CSS declarations
|
221
|
+
*.module.css.d.ts
|
222
|
+
*.module.sass.d.ts
|
223
|
+
*.module.scss.d.ts
|
224
|
+
*.module.less.d.ts
|
225
|
+
*.module.styl.d.ts
|
226
|
+
```
|
227
|
+
|
228
|
+
In addition, if the generated code causes ESLint to report errors, you can also add the above configuration to the `.eslintignore` file.
|
@@ -22,7 +22,7 @@ Please refer to [Modern.js Builder - Using PostCSS](https://modernjs.dev/builder
|
|
22
22
|
|
23
23
|
## Using CSS Modules
|
24
24
|
|
25
|
-
Please read the [Using CSS Modules](
|
25
|
+
Please read the [Using CSS Modules](/guides/basic-features/css-modules) section to learn about the complete usage of CSS Modules.
|
26
26
|
|
27
27
|
## Using CSS-in-JS
|
28
28
|
|
@@ -72,18 +72,7 @@ To use [Tailwind CSS](https://tailwindcss.com/) in Modern.js, you can follow the
|
|
72
72
|
? Please select the feature name: Enable Tailwind CSS
|
73
73
|
```
|
74
74
|
|
75
|
-
After successful initialization, you will see
|
76
|
-
|
77
|
-
```json title="./package.json"
|
78
|
-
{
|
79
|
-
"dependencies": {
|
80
|
-
"tailwindcss": "^3.0.0"
|
81
|
-
},
|
82
|
-
"devDependencies": {
|
83
|
-
"@modern-js/plugin-tailwindcss": "^2.0.0"
|
84
|
-
}
|
85
|
-
}
|
86
|
-
```
|
75
|
+
After successful initialization, you will see that the `package.json` has added dependencies for `tailwindcss` and `@modern-js/plugin-tailwindcss`.
|
87
76
|
|
88
77
|
2. Register the Tailwind plugin in `modern.config.ts`:
|
89
78
|
|
@@ -0,0 +1,106 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 12
|
3
|
+
---
|
4
|
+
|
5
|
+
# Import JSON Files
|
6
|
+
|
7
|
+
Modern.js supports import JSON files in code, and also supports import [YAML](https://yaml.org/) and [Toml](https://toml.io/en/) files and converting them to JSON format.
|
8
|
+
|
9
|
+
## JSON file
|
10
|
+
|
11
|
+
You can import JSON files directly in JavaScript files.
|
12
|
+
|
13
|
+
### Example
|
14
|
+
|
15
|
+
```json title="example.json"
|
16
|
+
{
|
17
|
+
"name": "foo",
|
18
|
+
"items": [1, 2]
|
19
|
+
}
|
20
|
+
```
|
21
|
+
|
22
|
+
```js title="index.js"
|
23
|
+
import example from './example.json';
|
24
|
+
|
25
|
+
console.log(example.name); // 'foo';
|
26
|
+
console.log(example.items); // [1, 2];
|
27
|
+
```
|
28
|
+
|
29
|
+
### Named Import
|
30
|
+
|
31
|
+
Modern.js does not support importing JSON files via named import yet:
|
32
|
+
|
33
|
+
```js
|
34
|
+
import { name } from './example.json';
|
35
|
+
```
|
36
|
+
|
37
|
+
## YAML file
|
38
|
+
|
39
|
+
YAML is a data serialization language commonly used for writing configuration files.
|
40
|
+
|
41
|
+
You can directly import `.yaml` or `.yml` files in JavaScript and they will be automatically converted to JSON format.
|
42
|
+
|
43
|
+
### Example
|
44
|
+
|
45
|
+
```yaml title="example.yaml"
|
46
|
+
---
|
47
|
+
hello: world
|
48
|
+
foo:
|
49
|
+
bar: baz
|
50
|
+
```
|
51
|
+
|
52
|
+
```js
|
53
|
+
import example from './example.yaml';
|
54
|
+
|
55
|
+
console.log(example.hello); // 'world';
|
56
|
+
console.log(example.foo); // { bar: 'baz' };
|
57
|
+
```
|
58
|
+
|
59
|
+
### Add type declaration
|
60
|
+
|
61
|
+
When you import a YAML file in your TypeScript code, please create a `src/global.d.ts` file in your project and add the corresponding type declaration:
|
62
|
+
|
63
|
+
```ts title="src/global.d.ts"
|
64
|
+
declare module '*.yaml' {
|
65
|
+
const content: Record<string, any>;
|
66
|
+
export default content;
|
67
|
+
}
|
68
|
+
|
69
|
+
declare module '*.yml' {
|
70
|
+
const content: Record<string, any>;
|
71
|
+
export default content;
|
72
|
+
}
|
73
|
+
```
|
74
|
+
|
75
|
+
## Toml file
|
76
|
+
|
77
|
+
Toml is a semantically explicit, easy-to-read configuration file format.
|
78
|
+
|
79
|
+
You can directly import `.toml` files in JavaScript and it will be automatically converted to JSON format.
|
80
|
+
|
81
|
+
### Example
|
82
|
+
|
83
|
+
```toml title="example.toml"
|
84
|
+
hello = "world"
|
85
|
+
|
86
|
+
[foo]
|
87
|
+
bar = "baz"
|
88
|
+
```
|
89
|
+
|
90
|
+
```js
|
91
|
+
import example from './example.toml';
|
92
|
+
|
93
|
+
console.log(example.hello); // 'world';
|
94
|
+
console.log(example.foo); // { bar: 'baz' };
|
95
|
+
```
|
96
|
+
|
97
|
+
### Add type declaration
|
98
|
+
|
99
|
+
When you import Toml files in TypeScript code, please create a `src/global.d.ts` file in your project and add the corresponding type declarations:
|
100
|
+
|
101
|
+
```ts title="src/global.d.ts"
|
102
|
+
declare module '*.toml' {
|
103
|
+
const content: Record<string, any>;
|
104
|
+
export default content;
|
105
|
+
}
|
106
|
+
```
|
@@ -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 |
|