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 ES6, CommonJS.
While Rspack supports multiple module syntaxes, we recommend following a single syntax for consistency and to avoid odd behaviors/bugs.
Rspack support ES6 module syntax natively, you can use static import
, export
and import()
syntax.
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. 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 older browsers, remember to shim Promise
using a polyfill such as 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 name our chunk or select different modes. For a full list of these magic comments see the code below followed by an explanation of what these comments do.
webpackChunkName
: A name for the new chunk.
webpackPrefetch
: Tells the browser that the resource is probably needed for some navigation in the future (Available since 0.4.5).
webpackPreload
: Tells the browser that the resource might be needed during the current navigation (Available since 0.4.5).
Rspack is also support CommonJS
syntax natively, you can use require
and module.exports
methods.
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.
Aside from the module syntaxes described above, Rspack also support some webpack-specific methods.
require.context
is a special function in webpack that allows you to dynamically require a set of modules.
Rspack parses for require.context()
in the code while building.
The arguments passed to
require.context()
must be literals.
The context returned by require.context()
is a function that takes a request
argument (module path).
This function has three properties: resolve
, keys
, and id
.
resolve
is a function and returns the module id of the parsed request.keys
is a function that returns an array of all possible requests that the context module can handle.id
is the module id of the context module. This may be useful for module.hot.accept
.This can be useful if you want to require all files in a directory or matching a pattern.
Consider a scenario where you have a folder structure like this:
You can use require.context()
to dynamically import all component files in the folder:
require.context()
streamlines the process of module importation especially when you have a lot of files to manage. When using it, please avoid matching unnecessary files, as this might lead to significantly increased build time and output size.