This section describes some output related configurations in Modern.js Builder.
string
'/'
When using CDN in the production environment, you can use this option to set the URL prefix of static assets.
assetPrefix
will affect the URLs of most of the static assets, including JavaScript files, CSS files, images, videos, etc. If an incorrect value is specified, you'll receive 404 errors while loading these resources.
This config is only used in the production environment. In the development environment, please use the dev.assetPrefix
to set the URL prefix.
After setting, the URLs of JavaScript, CSS and other static files will be prefixed with output.assetPrefix
:
After building, you can see that the JS files are loaded from:
output.assetPrefix
corresponds to the following native configurations:
The differences from the native configuration are as follows:
output.assetPrefix
only takes effect in the production environment.
output.assetPrefix
automatically appends a trailing /
by default.
The value of output.assetPrefix
is written to the process.env.ASSET_PREFIX
environment variable.
Object
output.assetsRetry
is used to configure the retry of assets.The type of AssetsRetryOptions
is as follows:
Since the ability will inject some extra runtime code into HTML, we have disabled this ability by default. If you need to enable it, you can configure it in the form of an object, for example:
When you enable this ability, the default config of assetsRetry
is as follows:
At the same time, you can also customize your retry logic using the following configurations.
string[]
[]
Specifies the retry domain when assets fail to load. In the domain
array, the first item is the currently used domain, and the following items are backup domains. When a asset request for a domain fails, Builder will find that domain in the array and replace it with the next domain in the array.
For example:
After adding the above configuration, when assets fail to load from the cdn1.com
domain, the request domain will automatically fallback to cdn2.com
.
If the assets request for cdn2.com
also fails, the request will fallback to cdn3.com
.
string[]
['script', 'link', 'img']
Used to specify the HTML tag types that need to be retried. By default, script tags, link tags, and img tags are processed, corresponding to JS code, CSS code, and images.
For example, only script tags and link tags are processed:
number
3
The maximum number of retries for a single asset. For example:
string | ((url: string) => boolean) | undefined
undefined
The test function of the asset to be retried. For example:
undefined | boolean | 'anonymous' | 'use-credentials'
same as html.crossorigin
When initiating a retry for assets, Builder will recreate the <script>
tags. This option allows you to set the crossorigin
attribute for these tags.
By default, the value of assetsRetry.crossOrigin
will be consistent with the html.crossorigin
configuration, so no additional configuration is required. If you need to configure the recreated tags separately, you can use this option, for example:
undefined | (options: AssetsRetryHookContext) => void
The callback function when the asset is being retried. For example:
undefined | (options: AssetsRetryHookContext) => void
The callback function when the asset is successfully retried. For example:
undefined | (options: AssetsRetryHookContext) => void
The callback function when the asset is failed to be retried. For example:
boolean
true
Whether to inline the runtime JavaScript code of assetsRetry
into the HTML file.
If you don't want to insert the code in the HTML file, you can set assetsRetry.inlineScript
to false
:
After adding the above configuration, the runtime code of assetsRetry
will be extracted into a separate assets-retry.[version].js
file and output to the dist directory.
The downside is that assets-retry.[version].js
itself may fail to load. If this happens, the assets retry will not work. Therefore, we prefer to inline the runtime code into the HTML file.
When you use assetsRetry
, the Builder injects some runtime code into the HTML and serializes the assetsRetry
config, inserting it into the runtime code. Therefore, you need to be aware of the following:
assetsRetry
, such as internal tokens.onRetry
, onSuccess
, and onFail
.onRetry
, onSuccess
and onFail
as these functions are inlined directly into the HTML.Here's an example of incorrect usage:
assetsRetry
may not work in the following scenarios:
If your project is a micro-frontend application (such as a Garfish sub-application), the assets retry may not work because micro-frontend sub-applications are typically not loaded directly based on the <script>
tag.
If you need to retry assets in micro-frontend scenarios, please contact the developers of the micro-frontend framework to find a solution.
Currently, assetsRetry
cannot work on dynamically imported resources. This feature is being supported.
assetsRetry
listens to the page error event to know whether the current resource fails to load and needs to be retried. Therefore, if the resource in the custom template is executed earlier than assetsRetry
, then assetsRetry
cannot listen to the event that the resource fails to load, so it cannot retry.
If you want assetsRetry
to work on resources in custom templates, you can refer to Custom Insertion Example to modify html.inject configuration and custom template.
'ascii' | 'utf8'
'ascii'
By default, Builder's output is ASCII-only and will escape all non-ASCII characters.
If you want to output the original characters without using escape sequences, you can set output.charset
to utf8
.
Builder will automatically add <meta charset="utf-8">
to the generated HTML files if output.charset
is utf8
.
boolean
true
Whether to clean all files in the dist path before starting compilation.
By default, Builder clean up the dist file, you can disable this behavior by setting cleanDistPath
to false
.
boolean | object
false
By setting output.convertToRem
, the Builder can do the following things:
If output.convertToRem
is set to true
, Rem processing capability will be turned on.
At this point, the rem configuration defaults as follows:
When the value of output.convertToRem
is object
type, The Builder will perform Rem processing based on the current configuration.
options:
Name | Type | Default | Description |
---|---|---|---|
enableRuntime | boolean |
true |
Whether to generate runtime code to calculate and set the font size of the root element |
inlineRuntime | boolean |
true |
Whether to inline the runtime code to HTML. If set to false , the runtime code will be extracted into a separate convert-rem.[version].js file and output to the dist directory |
rootFontSize | number |
50 |
The root element font size |
maxRootFontSize | number |
64 |
The root element max font size |
widthQueryKey | string |
'' |
Get clientWidth from the url query based on widthQueryKey |
screenWidth | number |
375 |
The screen width for UI design drawings (Usually, fontSize = (clientWidth * rootFontSize) / screenWidth ) |
excludeEntries | string[] |
[] |
To exclude some page entries from injecting runtime code, it is usually used with pxtorem.exclude |
supportLandscape | boolean |
false |
Use height to calculate rem in landscape |
useRootFontSizeBeyondMax | boolean |
false |
Whether to use rootFontSize when large than maxRootFontSize |
pxtorem | object |
|
postcss-pxtorem options |
CopyPluginOptions | CopyPluginOptions['patterns']
undefined
Copies the specified file or directory to the dist directory.
For example, copy the files under src/assets
to the dist directory:
For more detailed configuration, please refer to: copy-webpack-plugin.
Setup css modules configuration.
The auto
configuration option allows CSS modules to be automatically enabled based on their filenames.
Type: boolean | RegExp | ((resourcePath: string) => boolean)
Default: undefined
Type description:
undefined
: According to the output.disableCssModuleExtension configuration option to determine which style files to enable CSS modules.true
: enable CSS modules for all files matching /\.module\.\w+$/i.test(filename)
regexp.false
: disables CSS Modules.RegExp
: enable CSS modules for all files matching /RegExp/i.test(filename)
regexp.function
: enable CSS Modules for files based on the filename satisfying your filter function check.Style of exported class names.
Type: 'asIs' | 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly'
Default: 'camelCase'
Type description:
asIs
Class names will be exported as is.camelCase
Class names will be camelized, the original class name will not to be removed from the locals.camelCaseOnly
Class names will be camelized, the original class name will be removed from the locals.dashes
Only dashes in class names will be camelized.dashesOnly
Dashes in class names will be camelized, the original class name will be removed from the locals.string
Sets the format of the className generated by CSS Modules after compilation.
cssModuleLocalIdentName
has different default values in development and production.
In a production, Builder will generate shorter class names to reduce the bundle size.
You can use the following template strings in cssModuleLocalIdentName
:
[name]
- the basename of the asset.[local]
- original class.[hash]
- the hash of the string.[folder]
- the folder relative path.[path]
- the relative path.[file]
- filename and path.[ext]
- extension with leading dot.[hash:<hashDigest>:<hashDigestLength>]
: hash with hash settings.When using Rspack as the bundler, currently does not support custom <hashDigest>
.
Set cssModuleLocalIdentName
to other value:
Set the size threshold to inline static assets such as images and fonts.
By default, static assets will be Base64 encoded and inline into the page if the size is less than 10KB.
You can adjust the threshold by setting the dataUriLimit
config.
Detail:
svg
: The threshold of the SVG image.font
: The threshold of the font file.image
: The threshold of non-SVG images.media
: The threshold of media assets such as videos.Set the threshold of images to 5000 Bytes, and set media assets not to be inlined:
boolean
false
Whether to disable CSS extract and inline CSS files into JS files.
By default, Builder will extract CSS into a separate .css
file and output it to the dist directory. When this option is set to true
, CSS files will be inlined into JS files and inserted on the page at runtime via <style>
tags.
boolean
false
Whether to treat all .css
files in the source directory as CSS Modules.
By default, only the *.module.css
files are treated as CSS Modules. After enabling this config, all *.css
style files in the source directory will be regarded as CSS Modules.
.sass
, .scss
and .less
files are also affected by disableCssModuleExtension
.
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.
The following is a detailed explanation of the CSS Modules rules:
The following files are treated as CSS Modules:
*.module.css
filesThe following files are treated as normal CSS:
*.css
files (excluding .module
)*.global.css
filesThe following files are treated as CSS Modules:
*.css
and *.module.css
files in the source directory*.module.css
files under node_modulesThe following files are treated as normal CSS:
*.global.css
files*.css
files under node_modules (without .module
)For CSS Modules files inside node_modules, please always use the *.module.css
suffix.
Set the directory of the dist files. Builder will output files to the corresponding subdirectory according to the file type.
Detail:
root
: The root directory of all files.html
: The output directory of HTML files.js
: The output directory of JavaScript files.css
: The output directory of CSS style files.svg
: The output directory of SVG images.font
: The output directory of font files.wasm
: The output directory of WebAssembly files.image
: The output directory of non-SVG images.media
: The output directory of media assets, such as videos.server
: The output directory of server bundles when target is node
.worker
: The output directory of worker bundles when target is service-worker
.The root
is the root directory of the build artifacts and can be specified as a relative or absolute path. If the value of root
is a relative path, it will be appended to the project's root directory to form an absolute path.
Other directories can only be specified as relative paths and will be output relative to the root
directory.
The JavaScript files will be output to the distPath.root
+ distPath.js
directory, which is dist/static/js
.
If you need to output JavaScript files to the build/resource/js
directory, you can add following config:
boolean
false
Whether to disable code minification on production builds.
By default, JS and CSS code is minified during the production build to improve the page performance. If you do not want to the code to be minified, you can set disableMinimize
to true
.
This configuration is usually used for debugging and troubleshooting. It is not recommended to disable code minification in production environments, as it will significantly degrade the page performance.
Whether to disable Source Map generation.
Source Map is an information file that saves the source code mapping relationship. It records each location of the compiled code and the corresponding pre-compilation location. With Source Map, you can directly view the source code when debugging the compiled code.
By default, Builder's Source Map generation rules are:
If the project does not need Source Map, you can turned off it to speed up the compile speed.
If you want to enable Source Map in development and disable it in the production, you can set to:
If you need to individually control the Source Map of JS files or CSS files, you can refer to the following settings:
boolean
false
Whether to transform SVGs into React components. If true, will treat all .svg files as assets.
By default, when an SVG resource is referenced in a JS file, Builder 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
to true to improve build performance.
boolean
false
Whether to disable TypeScript type checker during compilation.
By default, Builder will run the TypeScript type checker in a separate process during the build process. Its checking logic is consistent with TypeScript's native tsc
command. You can use tsconfig.json
or Builder's tools.tsChecker
config to customize the checking behavior.
Disable TypeScript type checker:
Disable type checker in development:
Disable type checker in production:
It is not recommended to disable type checker in production, which will reduce the stability of the production code, please use it with caution.
boolean
false
Remove the hash from the name of static files after production build.
After the production build, there will be a hash in the middle of the filename by default. You can disable this behavior through the output.disableFilenameHash
config.
Deprecated: This configuration is deprecated, please use the boolean usage of output.filenameHash
instead.
By default, the filename is:
Add output.disableFilenameHash
config:
After rebuild, the filenames become:
boolean
false
Used to control whether to inline the bundler's runtime code into HTML.
Builder will generate a builder-runtime.js
file in the dist directory, which is the runtime code of webpack or Rspack.
runtimeChunk is a piece of runtime code, which is provided by webpack or Rspack, that contains the necessary module processing logic, such as module loading, module parsing, etc. See Runtime for details.
In the production environment, Builder will inline the runtimeChunk file into the HTML file by default instead of writing it to the dist directory. This is done to reduce the number of file requests.
If you don't want the runtimeChunk file to be inlined into the HTML file, you can set disableInlineRuntimeChunk
to true
and a separate builder-runtime.js
file will be generated.
If you don't want to generate a separate runtimeChunk file, but want the runtimeChunk code to be bundled into the page chunk, you can set the config like this:
boolean
false
Whether to generate a manifest file that contains information of all assets.
Enable asset manifest:
After compiler, there will be a dist/manifest.json
file:
If the current project has multiple types of build artifacts, such as including SSR build artifacts, multiple manifest.json files will be generated.
web artifact: asset-manifest.json
node artifact: asset-manifest-node.json
boolean
false
If this option is enabled, all unrecognized files will be emitted to the dist directory; otherwise, an exception will be thrown.
Enable the config:
Import a module of unknown type in code:
After compilation, foo.xxx
will be output to the dist/static/media
directory.
You can control the output path and filename after fallback through the output.distPath.media
and output.filename.media
configs.
Enabling this config will change the rules structure in the webpack config. In most cases, we do not recommend using this config.
boolean
false
Whether to use the new decorator proposal.
By default, Builder uses the legacy decorator proposal when compiling decorators.
When output.enableLatestDecorators
is set to true
, the Builder will compile with the new decorator proposal (version 2018-09).
Deprecated: This configuration is deprecated, please use source.decorators
instead.
boolean
false
Whether to generate a TypeScript declaration file for CSS modules.
Enable CSS module TypeScript declaration:
After building, there will be a .d.ts
file for each CSS module file. For example
false
Whether to inline output scripts files (.js files) into HTML with <script>
tags in production mode.
Note that, with this option on, the scripts files will no longer be written in dist directory, they will only exist inside the HTML file instead.
When using convention-based routing, you need to set output.splitRouteChunks to false if this option is turned on.
By default, we have following output files:
After turn on the output.enableInlineScripts
option:
The output files will become:
And dist/static/js/main.js
will be inlined in index.html
:
If you need to inline part of the JS files, you can set enableInlineScripts
to a regular expression that matches the URL of the JS file that needs to be inlined.
For example, to inline main.js
into HTML, you can add the following configuration:
The production filename includes a hash value by default, such as static/js/main.18a568e5.js
. Therefore, in regular expressions, \w+
is used to match the hash.
You can also set output.enableInlineScripts
to a function that accepts the following parameters:
name
: The filename, such as static/js/main.18a568e5.js
.size
: The file size in bytes.For example, if we want to inline assets that are smaller than 10KB, we can add the following configuration:
false
Whether to inline output style files (.css files) into HTML with <style>
tags in production mode.
Note that, with this option on, the style files will no longer be written in dist directory, they will only exist inside the HTML file instead.
When using convention-based routing, you need to set output.splitRouteChunks to false if this option is turned on.
By default, we have following output files:
After turn on the output.enableInlineStyles
option:
The output files will become:
And dist/static/css/style.css
will be inlined in index.html
:
If you need to inline part of the CSS files, you can set enableInlineStyles
to a regular expression that matches the URL of the CSS file that needs to be inlined.
For example, to inline main.css
into HTML, you can add the following configuration:
The production filename includes a hash value by default, such as static/css/main.18a568e5.css
. Therefore, in regular expressions, \w+
is used to match the hash.
You can also set output.enableInlineStyles
to a function that accepts the following parameters:
name
: The filename, such as static/css/main.18a568e5.css
.size
: The file size in bytes.For example, if we want to inline assets that are smaller than 10KB, we can add the following configuration:
Type: string | object | function | RegExp
Default: undefined
At build time, prevent some import
dependencies from being packed into bundles in your code, and instead fetch them externally at runtime.
For more information, please see: webpack Externals
Exclude the react-dom
dependency from the build product. To get this module at runtime, the value of react-dom
will globally retrieve the ReactDOM
variable.
When the build target is Web Worker, externals will not take effect. This is because the Worker environment can not access global variables.
Sets the filename of dist files.
After the production build, there will be a hash in the middle of the filename by default. This behavior can be disabled through the output.disableFilenameHash
config.
The following are the details of each filename:
js
: The name of the JavaScript file.css
: The name of the CSS style file.svg
: The name of the SVG image.font
: The name of the font file.image
: The name of a non-SVG image.media
: The name of a media asset, such as a video.To set the name of the JavaScript file to [name]_script.js
, use the following configuration:
Usually, we only set the filename hash in the production mode (i.e., when process.env.NODE_ENV === 'production'
).
If you set the filename hash in the development mode, it may cause HMR to fail (especially for CSS files). This is because every time the file content changes, the hash value changes, preventing tools like mini-css-extract-plugin from reading the latest file content.
When you import a module via dynamic import, the module will be bundled into a single file, and its default naming rules are as follows:
dist/static/js/async/src_add_ts.js
.dist/static/js/async/798.27e3083e.js
. This is to avoid leaking the source code path in the production environment, and the number of characters is also less.If you want to specify a fixed name for the async module, you can use the magic comments provided by the bundler to achieve this, using webpackChunkName
to specify the module name:
After specifying the module name as above, the generated file will be dist/static/js/async/my-chunk-name.js
.
'linked' | 'inline' | 'none'
'linked'
Configure how to handle the legal comment.
A "legal comment" is considered to be any statement-level comment in JS or rule-level comment in CSS that contains @license or @preserve or that starts with //! or /*!. These comments are preserved in output files by default since that follows the intent of the original authors of the code.
This behavior can be configured by using one of the following options:
linked
: Extract all legal comments to a .LEGAL.txt file and link to them with a comment.inline
: Preserve all legal comments in original position.none
: Remove all legal comments.Remove all legal comments:
string[] | Record<BuilderTarget, string[]
undefined
Specifies the range of target browsers that the project is compatible with. This value will be used by @babel/preset-env and autoprefixer to identify the JavaScript syntax that need to be transformed and the CSS browser prefixes that need to be added.
The overrideBrowserslist
config will override the .browserslistrc
config file in the project and the browserslist
field in package.json.
In most cases, it is recommended to use the .browserslistrc
file rather than the overrideBrowserslist
config. Because the .browserslistrc
file is the official config file, it is more general and can be recognized by other libraries in the community.
If there is no browserslist
configs defined in the project, nor overrideBrowserslist
defined, then Builder will set the default browserslist to:
An example compatible with mobile scenarios:
Check out the browserslist documentation to learn more about browserslist.
When you build multiple targets at the same time, you can set different browser ranges for different targets. At this point, you need to set overrideBrowserslist
to an object whose key is the corresponding build target.
For example to set different ranges for web
and node
:
'entry' | 'usage' | 'ua' | 'off'
'entry'
Via output.polyfill
you can configure how the polyfill is injected.
Polyfill is injected in every entry file when output.polyfill
is configured as 'entry'
.
Equivalent to useBuiltIns: 'entry'
configuration in @babel/preset-env
.
Polyfill is injected in each file based on the API used in the code.
Equivalent to useBuiltIns: 'usage'
configuration in @babel/preset-env
.
The Polyfill code is dynamically delivered according to the currently requested UA information.
The dynamic delivery feature needs to be used with the upper-level framework. For more details, please refer to Modern.js - Polyfill At Runtime.
Polyfill is not injected. When using this option, you need to ensure code compatibility yourself.
'url' | 'component'
'url'
output.svgDefaultExport
is used to configure the default export type of SVG files.
When output.svgDefaultExport
is set to url
, the default export of SVG files is the URL of the file. For example:
When output.svgDefaultExport
is set to component
, the default export of SVG files is the React component of the file. For example:
At this time, you can also specify the ?url
query to import the URL, for example: