CC 4.0 License
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.
Module variables
This section covers all variables available in code compiled with Rspack. Modules will be able to access specific data from the compilation process through module and other variables.
CommonJS
module.loaded
false means that the module is being executed, true the synchronous execution has been completed.
module.id
Current module ID.
module.hot
Indicates whether or not hot module replacement is enabled and provides an interface to the process. See the HMR API page for details.
global
See node.js global for details.
Rspack will replace the global with a proxy object and handle compatibility issues in it.
__filename
Depends on the configuration node.__filename.
false: undefinedmock: equal to'/index.js'true: NodeJs __filename
If used inside an expression that is parsed by the Parser, the configuration option is treated as true.
__dirname
Depends on the configuration node.__dirname.
false: undefinedmock: equal to'/index.js'true: NodeJs __dirname
If used inside an expression that is parsed by the Parser, the configuration option is treated as true.
import.meta (ESM)
The import.meta exposes context-specific metadata to a JavaScript module, such as the URL of the module. It is only available in ESM.
Please note that Rspack does not support direct access to
import.meta. Instead, you should access its properties or use destructuring assignment. E.g.,
import.meta.url
Returns the absolute file: URL of the module.
import.meta.webpackContext
import.meta.webpackContext is a function specific to webpack that allows you to dynamically import a set of modules.
You can use import.meta.webpackContext in your code, and Rspack will parse and reference the matching modules during the build process.
- Type:
- Example:
Rspack uses static analysis to parse the parameters of import.meta.webpackContext() during compilation. Therefore, the parameters must be literals.
For example, the value of regExp cannot be a variable, nor can it be the value generated by new RegExp(). It can only be a regular expression literal.
context API
The context returned by import.meta.webpackContext() is a function that takes a request argument (module path).
This function has three properties: resolve, keys, and id.
resolveis a function and returns the module id of the parsed module specifier.keysis a function that returns an array of all possible requests that the context module can handle.idis the module id of the context module. This may be useful formodule.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 import.meta.webpackContext() to dynamically import all component files in the folder:
import.meta.webpackContext() 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.
import.meta.webpackHot
An alias for module.hot, however import.meta.webpackHot can be used in strict ESM while module.hot can't.
Runtime
__webpack_hash__
It provides access to the hash of the compilation.
__webpack_runtime_id__
Access the runtime id of current entry.
__webpack_public_path__
Equals the configuration option's output.publicPath.
See Dynamically set publicPath for more information about the usage of
__webpack_public_path__.
__webpack_base_uri__
Get or change base URI at runtime.
__webpack_nonce__
Rspack is capable of adding a nonce to all scripts that it loads.
To activate this feature, set a __webpack_nonce__ variable and include it in your entry script.
Modules
__webpack_modules__
Access to the internal object of all modules.
__webpack_module__
It provides access to the the current module. module is not available in strict ESM.
__webpack_module__.id
It provides access to the ID of current module (module.id). module is not available in strict ESM.
__webpack_require__
The raw require function. This expression isn't parsed by the Parser for dependencies.
__non_webpack_require__
Generates a require function that is not parsed by webpack.
Can be used to do cool stuff with a global require function if available.
__webpack_is_included__
Test whether or not the given module is bundled by webpack.
__resourceQuery
The resource query of the current module.
If the following require call was made, then the query string would be available in file.js.
__webpack_exports_info__
In modules, __webpack_exports_info__ is available to allow exports introspection:
__webpack_exports_info__is alwaystrue__webpack_exports_info__.<exportName>.usedisfalsewhen the export is known to be unused,trueotherwise__webpack_exports_info__.<exportName>.useInfoisfalsewhen the export is known to be unusedtruewhen the export is known to be usednullwhen the export usage could depend on runtime conditionsundefinedwhen no info is available
__webpack_exports_info__.<exportName>.provideInfoisfalsewhen the export is known to be not providedtruewhen the export is known to be providednullwhen the export provision could depend on runtime conditionsundefinedwhen no info is available
- Accessing the info from nested exports is possible: i. e.
__webpack_exports_info__.<exportName>.<exportProperty1>.<exportProperty2>.used - Check whether exports can be mangled with
__webpack_exports_info__.<exportName>.canMangle
Chunks
__webpack_chunkname__
Get current chunk name.
__webpack_chunk_load__
The internal chunk loading function. Takes one argument:
chunkId: the id for the chunk to load.
Example to load chunks from alternate public path when one failed:
__webpack_get_script_filename__
It provides filename of the chunk by its id.
It is assignable, which allows changing the filename used by the runtime. For example, it can be used to determine the final path when loading chunks.
Module Federation
__webpack_share_scopes__
This object is used as a shared scope in the remote container and is filled with the provided modules from a host
__webpack_init_sharing__
This method is used to initialize modules of a shared scope in the host container.
System.js
__system_context__
Context from System.js when output.libraryTarget="system"
Rspack
__rspack_version__
Current Rspack version, default to version in @rspack/core/package.json, can
be modified through experiments.rspackFuture.bundlerInfo.version.
__rspack_unique_id__
The ID of the current bundler, the value is bundler={bundler}@{version}:
bundler: Default to"rspack"and can be modified through experiments.rspackFuture.bundlerInfo.bundler.version: Default to version in@rspack/core/package.jsonand can be modified through experiments.rspackFuture.bundlerInfo.version.

