The content of this section is derived from the content of the following links and is subject to the CC BY 4.0 license.
The following contents can be assumed to be the result of modifications and deletions based on the original contents if not specifically stated.
The top-level output key contains a set of options instructing Rspack on how and where it should output your bundles, assets, and anything else you bundle or load with Rspack.
Object
string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)
'[hash][ext][query]'
The same as output.filename
but for Asset Modules.
[name]
, [file]
, [query]
, [fragment]
, [base]
, and [path]
are set to an empty string for the assets built from data URI replacements.
The name of the file to be output by the Asset module. This value can be overridden by Rule.generator.filename.
'asset'
and asset is set to satisfy Rule.parser.dataUrlCondition'asset/resource'
boolean
true
Create async chunks that are loaded on demand.
boolean
true
Add charset="utf-8"
to the HTML <script>
tag.
Although the charset
attribute for <script>
tag was deprecated, rspack still adds it by default for compatibility with non-modern browsers.
string = '[id].js' | (pathData: PathData, assetInfo?: JsAssetInfo) => string
output.filename
when it is not a function, otherwise '[id].js'
.This option determines the name of non-initial chunk files. See output.filename
option for details on the possible values.
Note that these filenames need to be generated at runtime to send the requests for chunks. Because of this, placeholders like [name]
and [chunkhash]
need to add a mapping from chunk id to placeholder value to the output bundle with the Rspack runtime. This increases the size and may invalidate the bundle when placeholder value for any chunk changes.
By default [id].js
is used or a value inferred from output.filename
([name]
is replaced with [id]
or [id].
is prepended).
Usage as a function:
false | 'array-push' | 'commonjs' | 'module' | string
target
and output.module
The format of chunks (formats included by default are 'array-push'
(web/webworker), 'commonjs'
(node.js), 'module'
(ESM), but others might be added by plugins).
The default value of this option depends on the target
and output.module
setting. For more details, search for "chunkFormat" in the Rspack defaults.
number
120000
The Number of milliseconds before chunk request timed out.
string
output.uniqueName
The global variable is used by Rspack for loading chunks.
false | 'jsonp' | 'import-scripts' | 'require' | 'async-node' | 'import'
The method to load chunks (methods included by default are 'jsonp'
(web), 'import'
(ESM), 'importScripts'
(webworker), 'require'
(sync node.js), 'async-node'
(async node.js), but others might be added by plugins). The default value will be determined based on the configuration of target
and chunkFormat
.
The default value of this option depends on the target
and chunkFormat
setting. For more details, search for "chunkLoading"
in the Rspack defaults.
boolean
false
Before generating the products, delete all files in the output directory.
boolean
true
Tells Rspack to check if to be emitted file already exists and has the same content before writing to the output file system.
Rspack will not write output file when file already exists on disk with the same content.
false | 'anonymous' | 'use-credentials'
false
The crossOriginLoading
config allows you to set the crossorigin attribute for dynamically loaded chunks.
If target
is 'web'
, Rspack will dynamically create <script>
and <link>
tags to load asynchronous JavaScript and CSS resources. Rspack will add the crossorigin
attribute to the <script>
and <link>
tags if the URLs of these resources are on other domains and crossOriginLoading
is not false
.
Optional values
crossOriginLoading
has the following optional values:
false
: Do not set the crossorigin attribute.'anonymous'
: Set crossorigin
to 'anonymous'
to enable cross-origin without user credentials.'use-credentials'
: Set crossorigin
to 'use-credentials'
enable cross-origin with user credentials.Example
For example, set output.publicPath
to https://example.com/
and output.crossOriginLoading
to 'anonymous'
:
When Rspack dynamically loads JavaScript resources, it will generate the following HTML:
string | (pathData: PathData, assetInfo?: JsAssetInfo) => string
output.chunkFilename
when it is not a function, otherwise '[id].css'
.This option determines the name of non-initial CSS output files on disk. See output.filename
option for details on the possible values.
You must not specify an absolute path here. However, feel free to include folders separated by '/'
. This specified path combines with the output.path
value to pinpoint the location on the disk.
string | (pathData: PathData, assetInfo?: JsAssetInfo) => string
output.filename
This option determines the name of CSS output files on disk. See output.filename
option for details on the possible values.
You must not specify an absolute path here. However, feel free to include folders separated by '/'
. This specified path combines with the output.path
value to pinpoint the location on the disk.
string
| function (info)
undefined
A fallback is used when the template string or function above yields duplicates.
See output.devtoolModuleFilenameTemplate
.
string = 'webpack://[namespace]/[resource-path]?[loaders]'
| function (info) => string
undefined
This option is only used when devtool
uses an option that requires module names.
Customize the names used in each source map's sources
array. This can be done by passing a template string or function. For example, when using devtool: 'eval'
.
The following substitutions are available in template strings
Template | Description |
---|---|
[absolute-resource-path] | The absolute filename |
[all-loaders] | Automatic and explicit loaders and params up to the name of the first loader |
[hash] | The hash of the module identifier |
[id] | The module identifier |
[loaders] | Explicit loaders and params up to the name of the first loader |
[resource] | The path used to resolve the file and any query params used on the first loader |
[resource-path] | The path used to resolve the file without any query params |
[namespace] | The modules namespace. This is usually the library name when building as a library, empty otherwise |
When using a function, the same options are available camel-cased via the info
parameter:
If multiple modules would result in the same name, output.devtoolFallbackModuleFilenameTemplate
is used instead for these modules.
string
undefined
This option determines the module's namespace used with the output.devtoolModuleFilenameTemplate
. When not specified, it will default to the value of: output.uniqueName
. It's used to prevent source file path collisions in sourcemaps when loading multiple libraries built with Rspack.
For example, if you have 2 libraries, with namespaces library1
and library2
, which both have a file ./src/index.js
(with potentially different contents), they will expose these files as webpack://library1/./src/index.js
and webpack://library2/./src/index.js
.
('jsonp' | 'import-scripts' | 'require' | 'async-node' | string)[]
output.chunkLoading
, output.workerChunkLoading
and Entry's chunkLoading config.List of chunk loading types enabled for use by entry points. Will be automatically filled by Rspack. Only needed when using a function as entry option and returning chunkLoading option from there.
string[]
List of library types enabled for use by entry points.
('fetch-streaming' | 'fetch' | 'async-node' | string | false)[]
output.wasmLoading
and output.workerWasmLoading
List of Wasm loading types enabled for use by entry points.
Tell Rspack what kind of ES-features may be used in the generated runtime-code.
string | (pathData: PathData, assetInfo?: JsAssetInfo) => string
[output.module](#outputmodule)
is true
, it is '[name].mjs'
, otherwise it is '[name].js'
.This option determines the name of each output bundle. The bundle is written to the directory specified by the output.path
option.
For a single entry
point, this can be a static name.
However, when creating multiple bundles via more than one entry point, code splitting, or various plugins, you should use one of the following substitutions to give each bundle a unique name...
Rspack performs code splitting optimizations on user input code, which may include, but are not limited to, code splitting, bundle splitting, or splitting implemented through other plugins. These splitting actions can result in multiple bundles being generated, so the filenames of the bundles need to be generated dynamically.
Use Entry name:
Using internal chunk id:
Using hashes generated from the generated content:
Combining multiple substitutions:
Using the function to return the filename:
Note this option is called filename but you are still allowed to use something like 'js/[name]/bundle.js'
to create a folder structure.
Note this option does not affect output files for on-demand-loaded chunks. It only affects output files that are initially loaded. For on-demand-loaded chunk files, the output.chunkFilename
option is used. Files created by loaders also aren't affected. In this case, you would have to try the specific loader's available options.
The template string below can be used to replace the corresponding file name. Different contexts correspond to different replaceable content, e.g. output.assetModuleFilename supports the use of File Context and Module Context.
Content that can be replaced at the compilation level.
Template | Description |
---|---|
[fullhash] | full hash of compilation |
Content that can be replaced at the chunk level.
template | description |
---|---|
[id] | The current chunk id |
[name] | Use name when chunk name exists, otherwise use chunk id |
[chunkhash] | The hash value of the chunk, computed from all elements of type in the current chunk |
[contenthash] | The hash value of the chunk, computed from the elements that contain only the content of that type. For example, if a module of type JavaScript is generated, only the hash of all JavaScript-typed modules in the current chunk is used. |
Content that can be replaced at the module level.
Template | Description |
---|---|
[id] | The id of the module |
[hash] | The hash of the module |
[contenthash] | hash of module content |
Content that can be replaced at the file level.
Template | Description |
---|---|
[file] | Filename and path, without query or fragment |
[query] | Query with leading ? |
[fragment] | Fragment with leading # |
[base] | Only filename (including extensions), without path |
[filebase] | Same, but deprecated |
[path] | Only path, without filename |
[name] | Only filename without extension or path |
[ext] | Extension with leading . (not available for output.filename) |
Substitutions available on URL-level:
Template | Description |
---|---|
[url] | URL |
[file]
equals [path][base]
. [base]
equals [name][ext]
. The full path is [path][name][ext][query][fragment]
or [path][base][query][fragment]
or [file][query][fragment]
.
The length of hashes ([hash]
, [contenthash]
or [chunkhash]
) can be specified using [hash:12]
(defaults to 16). Alternatively, specify output.hashDigestLength
to configure the length globally.
It is possible to filter out placeholder replacement when you want to use one of the placeholders in the actual file name. For example, to output a file [name].js
, you have to escape the [name]
placeholder by adding backslashes between the brackets. So that [\name\]
generates [name]
instead of getting replaced with the name
of the asset.
Example: [\id\]
generates [id]
instead of getting replaced with the id
.
If using a function for this option, the function will be passed an object containing data for the substitutions in the table above. Substitutions will be applied to the returned string too. The passed object will have this type: (properties available depending on context)
string
'self'
When targeting a library, especially when library.type
is 'umd'
, this option indicates what global object will be used to mount the library. To make UMD build available on both browsers and Node.js, set output.globalObject
option to 'this'
. Defaults to self
for Web-like targets.
The return value of your entry point will be assigned to the global object using the value of output.library.name
. Depending on the value of the type
option, the global object could change respectively, e.g., self
, global
, or globalThis
.
For example:
string
'hex'
The encoding to use when generating the hash. Using 'base64'
for filenames might be problematic since it has the character /
in its alphabet. Likewise 'latin1'
could contain any character.
number
16
The prefix length of the hash digest to use.
'md4' | 'xxhash64'
'xxhash64'
The hashing algorithm to use.
Rspack uses the faster xxhash64
algorithm by default since v1.1.
string
undefined
An optional salt to update the hash.
string
"[id].[fullhash].hot-update.js"
Customize the filenames of hot update chunks. See output.filename
option for details on the possible values.
The only placeholders allowed here are [id]
and [fullhash]
, the default being:
Typically you don't need to change output.hotUpdateChunkFilename
.
string
"webpackHotUpdate" + output.uniqueName
Only used when target
is set to 'web'
, which uses JSONP for loading hot updates.
A JSONP function is used to asynchronously load hot-update chunks.
For details see output.chunkLoadingGlobal
.
string
"[runtime].[fullhash].hot-update.json"
Customize the main hot update filename. [fullhash]
and [runtime]
are available as placeholder.
Typically you don't need to change output.hotUpdateMainFilename
.
boolean
true
Tells Rspack to add IIFE wrapper around emitted code.
string
'import'
The name of the native import()
function. Can be used for polyfilling, e.g. with dynamic-import-polyfill
.
string
'import.meta'
The name of the native import.meta
object (can be exchanged for a polyfill).
Output a library exposing the exports of your entry point.
string | string[] | object
Let's take a look at an example.
Say you have exported a function in your src/index.js
entry:
Now the variable MyLibrary
will be bound with the exports of your entry file, and here's how to consume the Rspack bundled library:
In the above example, we're passing a single entry file to entry
, however, Rspack can accept many kinds of entry point, e.g., an array
, or an object
.
If you provide an array
as the entry
point, only the last one in the array will be exposed.
If an object
is provided as the entry
point, all entries can be exposed using the array
syntax of library
:
Assuming that both a.js
and b.js
export a function hello
, here's how to consume the libraries:
string
Use a container(defined in global space) for calling define
/require
functions in an AMD module.
Note that the value of amdContainer
must be set as a global variable.
Which will result in the following bundle:
Specify a name for the library.
string | string[] | {amd?: string, commonjs?: string, root?: string | string[]}
Configure how the library will be exposed.
Type: string
Types included by default are 'var'
, 'module'
, 'system'
, 'assign'
, 'assign-properties'
, 'this'
, 'window'
, 'self'
, 'global'
, 'commonjs'
, 'commonjs2'
, 'commonjs-module'
, 'commonjs-static'
, 'amd'
, 'amd-require'
, 'umd'
, 'umd2'
, but others might be added by plugins.
For the following examples, we'll use _entry_return_
to indicate the values returned by the entry point.
These options assign the return value of the entry point (e.g. whatever the entry point exported) to the name provided by output.library.name
at whatever scope the bundle was included at.
When your library is loaded, the return value of your entry point will be assigned to a variable:
This will generate an implied global which has the potential to reassign an existing value (use with caution):
Be aware that if MyLibrary
isn't defined earlier your library will be set in global scope.
Similar to type: 'assign'
but a safer option as it will reuse MyLibrary
if it already exists:
These options assign the return value of the entry point (e.g. whatever the entry point exported) to a specific object under the name defined by output.library.name
.
The return value of your entry point will be assigned to this
under the property named by output.library.name
. The meaning of this
is up to you:
The return value of your entry point will be assigned to the window
object using the output.library.name
value.
The return value of your entry point will be assigned to the global object using the output.library.name
value. Depending on the target
value, the global object could change respectively, e.g., self
, global
or globalThis
.
The return value of your entry point will be assigned to the exports
object using the output.library.name
value. As the name implies, this is used in CommonJS environments.
Note that not setting a output.library.name
will cause all properties returned by the entry point to be assigned to the given object; there are no checks against existing property names.
These options will result in a bundle that comes with a complete header to ensure compatibility with various module systems. The output.library.name
option will take on a different meaning under the following output.library.type
options.
Output ES modules.
However this feature is still experimental and not fully supported yet, so make sure to enable experiments.outputModule
beforehand. In addition, you can track the development progress in this thread.
The return value of your entry point will be assigned to the module.exports
. As the name implies, this is used in Node.js (CommonJS) environments:
If we specify output.library.name
with type: commmonjs2
, the return value of your entry point will be assigned to the module.exports.[output.library.name]
.
Wondering the difference between CommonJS and CommonJS2 is? While they are similar, there are some subtle differences between them that are not usually relevant in the context of Rspack. (For further details, please read this issue.)
Individual exports will be set as properties on module.exports
. The "static" in the name refers to the output being statically analysable, and thus named exports are importable into ESM via Node.js:
Input:
Output:
Consumption (CommonJS):
Consumption (ESM):
This is useful when source code is written in ESM and the output should be compatible with both CJS and ESM. For further details, please read this issue or this article (specifically, this section).
This will expose your library as an AMD module.
AMD modules require that the entry chunk (e.g. the first script loaded by the <script>
tag) be defined with specific properties, such as to define
and require
which is typically provided by RequireJS or any compatible loaders (such as almond). Otherwise, loading the resulting AMD bundle directly will result in an error like define is not defined
.
With the following configuration:
The generated output will be defined with the name "MyLibrary"
, i.e.:
The bundle can be included as part of a script tag, and the bundle can be invoked like so:
If output.library.name
is undefined, the following is generated instead.
This bundle will not work as expected, or not work at all (in the case of the almond loader) if loaded directly with a <script>
tag. It will only work through a RequireJS compatible asynchronous module loader through the actual path to that file, so in this case, the output.path
and output.filename
may become important for this particular setup if these are exposed directly on the server.
This packages your output with an immediately executed AMD require(dependencies, factory)
wrapper.
The 'amd-require'
type allows for the use of AMD dependencies without needing a separate later invocation. As with the 'amd'
type, this depends on the appropriate require
function being available in the environment in which the Rspack output is loaded.
With this type, the library name can't be used.
This exposes your library under all the module definitions, allowing it to work with CommonJS, AMD, and as global variable. Take a look at the UMD Repository to learn more.
In this case, you need the library.name
property to name your module:
And finally the output is:
Note that omitting library.name
will result in the assignment of all properties returned by the entry point be assigned directly to the root object, as documented under the object assignment section. Example:
The output will be:
You may specify an object for library.name
for differing names per targets:
This will expose your library as a System.register
module.
System modules require that a global variable System
is present in the browser when the Rspack bundle is executed. Compiling to System.register
format allows you to System.import('/bundle.js')
without additional configuration and has your Rspack bundle loaded into the System module registry.
Output:
By adding output.library.name
to configuration in addition to having output.library.type
set to system
, the output bundle will have the library name as an argument to System.register
:
type: 'jsonp'
This will wrap the return value of your entry point into a jsonp wrapper.
The dependencies for your library will be defined by the externals
config.
Specify which export should be exposed as a library.
string | string[]
undefined
It is undefined
by default, which will export the whole (namespace) object. The examples below demonstrate the effect of this configuration when using output.library.type: 'var'
.
The default export of your entry point will be assigned to the library name:
You can pass an array to output.library.export
as well, it will be interpreted as a path to a module to be assigned to the library name:
And here's the library code:
Add a comment in the UMD wrapper.
string | { amd?: string, commonjs?: string, commonjs2?: string, root?: string }
undefined
To insert the same comment for each umd
type, set auxiliaryComment
to a string:
which will yield the following:
For fine-grained control, pass an object:
boolean
undefined
When using output.library.type: "umd"
, setting output.library.umdNamedDefine
to true
will name the AMD module of the UMD build. Otherwise, an anonymous define
is used.
The AMD module will be:
boolean
false
Output JavaScript files as module type. Disabled by default as it's an experimental feature. To use it, you must set experiments.outputModule
to true
.
When enabled, Rspack will set output.iife
to false
, output.scriptType
to 'module'
and terserOptions.module
to true
internally.
If you're using Rspack to compile a library to be consumed by others, make sure to set output.libraryTarget
to 'module'
when output.module
is true
.
string
path.resolve(process.cwd(), 'dist')
The output directory as an absolute path.
Note that [fullhash]
in this parameter will be replaced with a hash of the compilation.
The path must not contain an exclamation mark (!
) as it is reserved by Rspack for loader syntax).
boolean | 'verbose'
true
Tells Rspack to include comments in bundles with information about the contained modules. This option defaults to true
in development
and false
in production
mode respectively. 'verbose'
shows more information like exports, runtime requirements and bailouts.
While the data these comments can provide is useful during development when reading the generated code, it should not be used in production.
It also adds some info about tree shaking to the generated bundle.
'auto' | string | ((pathData: PathData, assetInfo?: JsAssetInfo) => string)
'auto'
when targets
is 'web'
or 'webworker'
. undefined
otherwiseThis option determines the URL prefix of the referenced resource, such as: image, file, etc.
For example, given a configuration file.
For a non-initial chunk file, its request URL looks like this: /assets/1.chunk.js
.
For a reference to an image, the request URL looks like this: /assets/logo.png
.
Also, it is useful when you deploy the product using a CDN
For all asset resources, their request URLs look like this: https://cdn.example.com/assets/logo.png
.
Dynamically set publicPath
You can set publicPath
dynamically using __webpack_public_path__
in the runtime code, and the __webpack_public_path__
will override the publicPath
in the Rspack config, but it will only take effect for dynamically loaded resources.
First create a publicPath.js
:
Then import it into the first line of the entry file:
'module' | 'text/javascript' | boolean
false
This option allows loading asynchronous chunks with a custom script type, such as <script type="module" ...>
.
If output.module
is set to true
, output.scriptType
will default to 'module'
instead of false
.
string
'[file].map[query]'
Configure how source maps are named. Only takes effect when devtool
is set to 'source-map'
, which writes an output file.
The [name]
, [id]
, [fullhash]
and [chunkhash]
substitutions from output.filename
can be used. In addition to those, you can use substitutions listed under Filename-level in Template strings.
boolean
false
Handle error in module loading as per EcmaScript Modules spec at a performance cost.
Tell Rspack to remove a module from the module instance cache (require.cache) if it throws an exception when it is required.
true | string | object
undefined
Controls Trusted Types compatibility. When enabled, Rspack will detect Trusted Types support and, if they are supported, use Trusted Types policies to create script URLs it loads dynamically. Use when the application runs under a require-trusted-types-for
Content Security Policy directive.
It is disabled by default (no compatibility, script URLs are strings).
true
, Rspack will use output.uniqueName
as the Trusted Types policy name.policyName
property."stop" | "continue"
"stop"
Determine whether to proceed with loading in anticipation that require-trusted-types-for 'script'
has not been enforced or to immediately fail when the call to trustedTypes.createPolicy(...)
fails due to the policy name being absent from the CSP trusted-types
list or being a duplicate.
string
output.library
name or the package name from package.json
in the context, if both aren't found, it is set to an ''
.A unique name of the Rspack build to avoid multiple Rspack runtimes to conflict when using globals.
output.uniqueName
will be used to generate unique globals for:
false | 'fetch', 'async-node'
'fetch'
Option to set the method of loading WebAssembly Modules. Methods included by default are 'fetch'
(web/webworker), 'async-node'
(Node.js), but others might be added by plugins.
The default value can be affected by different target
:
'fetch'
if target
is set to 'web'
, 'webworker'
, 'electron-renderer'
or 'node-webkit'
.'async-node'
if target
is set to 'node'
, 'async-node'
, 'electron-main'
or 'electron-preload'
.string
'[hash].module.wasm'
Specifies the filename of WebAssembly modules. It should be provided as a relative path within the output.path
directory
false | 'jsonp' | 'import-scripts' | 'require' | 'async-node' | 'import'
false
The new option workerChunkLoading
controls the chunk loading of workers.
The default value of this option depends on the target
setting. For more details, search for "workerChunkLoading"
in the Rspack defaults.
string
""
Set a public path for Worker, defaults to value of output.publicPath. Only use this option if your worker scripts are located in a different path from your other scripts.
false | 'fetch-streaming' | 'fetch' | 'async-node' | string
false
Option to set the method of loading WebAssembly Modules in workers, defaults to the value of output.wasmLoading.
Prefer to use output.library.auxiliaryComment
instead.
We might drop support for this, so prefer to use output.library.export which works the same as libraryExport
.
Please use output.library.type
instead as we might drop support for output.libraryTarget
in the future.
Prefer to use output.library.umdNamedDefine
instead.
boolean
false
for development mode, true
for production modeRspack adds some metadata in CSS to parse CSS modules, and this configuration determines whether to compress these metadata.
For example
After compress 👇