Import Static Assets
Modern.js supports import static assets, including images, fonts, and medias.
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.
Assets Format
The following are the formats supported by Modern.js by default:
- Image: png, jpg, jpeg, gif, svg, bmp, webp, ico, apng, avif, tiff.
- Fonts: woff, woff2, eot, ttf, otf, ttc.
- Media: mp4, webm, ogg, mp3, wav, flac, aac, mov.
If you need to import assets in other formats, please refer to Extend Asset Types.
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.
Import Assets in JS file
In JS files, you can directly import static assets in relative paths:
Import with alias are also supported:
Import Assets in CSS file
In CSS files, you can reference static assets in relative paths:
Import with alias are also supported:
Import Results
The result of importing static assets depends on the file size:
- When the file size is greater than 10KB, a URL will be returned, and the file will be output to the dist directory.
- When the file size is less than 10KB, it will be automatically inlined to Base64 format.
For a more detailed introduction to asset inlining, please refer to the Static Asset Inlining chapter.
Output Files
When static assets are imported, they will be output to the dist directory. You can:
- Modify the output filename through output.filename.
- Modify the output path through output.distPath.
Please read Output Files for details.
URL Prefix
The URL returned after importing a asset will automatically include the path prefix:
- In development, using dev.assetPrefix to set the path prefix.
- In production, using output.assetPrefix to set the path prefix.
For example, you can set output.assetPrefix to https://modern.com:
Add Type Declaration
When you import static assets in TypeScript code, TypeScript may prompt that the module is missing a type definition:
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:
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.
Extend Asset Types
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.
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:
After adding the above configuration, you can import *.pdf files in your code, for example:
For more information about asset modules, please refer to:
Image Format
When using image assets, you can choose a appropriate image format according to the pros and cons in the table below.