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.
This section covers all methods available in code compiled with Rspack. When using Rspack to bundle your application, you can pick from a variety of module syntax styles including ES modules and CommonJS.
While Rspack supports multiple module syntaxes, we recommend following a single syntax for consistency and to avoid odd behaviors or bugs.
Actually Rspack would enforce the recommendation for .mjs
files, .cjs
files or .js
files when their nearest parent package.json
file contains a "type" field with a value of either "module"
or "commonjs"
. Please pay attention to these enforcements before you read on:
.mjs
or .js
with "type": "module"
in package.json
require
, module.exports
or exports
.cjs
or .js
with "type": "commonjs" in package.json
Rspack support ES modules syntax natively, you can use static import
, export
and import()
syntax.
Keep in mind that you will still probably need SWC or Babel for other ES6+ features.
Statically import
the export
s of another module.
You can also import
Data URI:
Export anything as a default
or named export.
Dynamically load modules, see Dynamic import for more details.
Calls to import()
are treated as split points, meaning the requested module and its children are split out into a separate chunk.
This feature relies on Promise
internally. If you use import()
with legacy browsers, remember to shim Promise
using a polyfill such as core-js, es6-promise or promise-polyfill.
It is not possible to use a fully dynamic import statement, such as import(foo)
. Because foo
could potentially be any path to any file in your system or project.
The import()
must contain at least some information about where the module is located. Bundling can be limited to a specific directory or set of files so that when you are using a dynamic expression, every module that could potentially be requested on an import()
call is included.
For example, import(
./locale/${language}.json)
will cause every .json
file in the ./locale
directory to be bundled into the new chunk. At run time, when the variable language
has been computed, any file like english.json
or german.json
will be available for consumption.
Inline comments to make features work. By adding comments to the import, we can do things such as specify chunk name or select different loading modes. For a full list of these magic comments see the code below followed by an explanation of what these comments do.
boolean
Disables dynamic import parsing when set to true.
Note that setting webpackIgnore to true opts out of code splitting.
"eager" | "lazy" | "weak" | "lazy-once"
'lazy'
Different modes for resolving dynamic imports can be specified. The following options are supported:
'lazy'
(default): Generates a lazy-loadable chunk for each import()
ed module.'lazy-once'
: Generates a single lazy-loadable chunk that can satisfy all calls to import()
. The chunk will be fetched on the first call to import()
, and subsequent calls to import()
will use the same network response. Note that this only makes sense in the case of a partially dynamic statement, e.g. import("./locales/${language}.json")
, where multiple module paths that can potentially be requested.'eager'
: Generates no extra chunk. All modules are included in the current chunk and no additional network requests are made. A Promise is still returned but is already resolved. In contrast to a static import, the module isn't executed until the call to import()
is made.'weak'
: Tries to load the module if the module function has already been loaded in some other way (e.g. another chunk imported it or a script containing the module was loaded). A Promise is still returned, but only successfully resolves if the chunks are already on the client. If the module is not available, the Promise is rejected. A network request will never be performed. This is useful for universal rendering when required chunks are always manually served in initial requests (embedded within the page), but not in cases where app navigation will trigger an import not initially served.number
: chunk prefetch priorityboolean
: false
means not to prefetch, true
means priority is 0
Tells the browser that the resource is probably needed for some navigation in the future, see Prefetching/Preloading modules for more details.
number
: chunk preload priorityboolean
: false
means not to preload, true
means priority is 0
Tells the browser that the resource might be needed during the current navigation, , see Prefetching/Preloading modules for more details.
string
A name for the new chunk.
"low" | "high" | "auto"
Set fetchPriority
for specific dynamic imports. It's also possible to set a global default value for all dynamic imports by using the module.parser.javascript.dynamicImportFetchPriority
option.
Regexp
A regular expression that will be matched against during import resolution. Only modules that match will be bundled.
Regexp
A regular expression that will be matched against during import resolution. Any module that matches will not be bundled.
Note that webpackInclude
and webpackExclude
options do not interfere with the prefix. eg: ./locale
.
string | string[]
Tells webpack to only bundle the specified exports of a dynamically import()
ed module. It can decrease the output size of a chunk.
Rspack is also support CommonJS
syntax natively, you can use require
and module.exports
methods.
Synchronously retrieve the exports from another module.
Synchronously retrieve a module's ID. It is recommended to treat it as an opaque value which can only be used with require.cache[id]
or __webpack_require__(id)
(best to avoid such usage).
Module ID's type can be a number or a string depending on the optimization.moduleIds
configuration.
Multiple requires of the same module result in only one module execution and only one export. Therefore a cache in the runtime exists. Removing values from this cache causes new module execution and a new export.
require.context
is a function specific to webpack that allows you to dynamically require a set of modules.
You can use require.context
in your code, and Rspack will parse and reference the matching modules during the build process.
The return value of require.context
is the same as import.meta.webpackContext. We recommend using import.meta.webpackContext
, which is more powerful.
Rspack uses static analysis to parse the parameters of require.context
during compilation. Therefore, the parameters must be literals.
For example, the value of filter
cannot be a variable, nor can it be the value generated by new RegExp()
. It can only be a regular expression literal.
require.ensure()
is specific to rspack/webpack and superseded by import()
.
Split out the given dependencies
to a separate bundle that will be loaded asynchronously. When using CommonJS module syntax, this is the only way to dynamically load dependencies
. Meaning, this code can be run within execution, only loading the dependencies if certain conditions are met.
This feature relies on Promise internally. If you use require.ensure
with older browsers, remember to shim Promise using a polyfill such as es6-promise or promise-polyfill.
Rspack supports importing Data URI modules using the import
and require
syntax.
import
require
In addition, Base64 encoded requests are also supported:
The Data URI module can be used as a method to implement virtual modules, such as combining with a Loader to dynamically load custom modules at runtime.