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 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.
The arguments passed to
require.context()
must be literals.