Movatterモバイル変換


[0]ホーム

URL:


webpack logo
ag grid
ag charts

Module Variables

This section covers allvariables available in code compiled with webpack. Modules will have access to certain data from the compilation process throughmodule and other variables.

module.loaded (NodeJS)

This isfalse if the module is currently executing, andtrue if the sync execution has finished.

module.hot (webpack-specific)

Indicates whether or notHot Module Replacement is enabled and provides an interface to the process. See theHMR API page for details.

module.id (CommonJS)

The ID of the current module.

module.id=== require.resolve('./file.js');

module.exports (CommonJS)

Defines the value that will be returned when a consumer makes arequire call to the module (defaults to a new object).

module.exports=functiondoSomething(){// Do something...};
warning

This CANNOT be used in an asynchronous function.

exports (CommonJS)

This variable is equal to the default value ofmodule.exports (i.e. an object). Ifmodule.exports gets overwritten,exports will no longer be exported.

exports.someValue=42;exports.anObject={  x:123,};exports.aFunction=functiondoSomething(){// Do something};

global (NodeJS)

Seenode.js global.

For compatibility reasons webpack polyfills theglobal variable by default.

__dirname (NodeJS)

Depending on the configuration optionnode.__dirname:

If used inside an expression that is parsed by the Parser, the configuration option is treated astrue.

import.meta

Theimport.meta exposes context-specific metadata to a JavaScript module, such as the URL of the module. It is only available inESM.

Please note that webpack does not support direct access toimport.meta. Instead, you should access its properties or use destructuring assignment. E.g.,

// webpack will warn about thisObject.keys(import.meta);// fine to useconsole.log(import.meta.url);const{ url}=import.meta;

import.meta.url

Returns the absolutefile: URL of the module.

src/index.js

console.log(import.meta.url);// output something like `file:///path/to/your/project/src/index.js`

import.meta.webpack

Returns the webpack version.

src/index.js

console.log(import.meta.webpack);// output `5` for webpack 5

import.meta.webpackHot

Webpack specific. An alias formodule.hot, howeverimport.meta.webpackHot can be used instrict ESM whilemodule.hot can't.

import.meta.webpackContext

Returns the same value asrequire.context but only forjavascript/auto andjavascript/esm.

  • Type:

    (  request:string,  options?:{    recursive?:boolean;    regExp?: RegExp;    include?: RegExp;    exclude?: RegExp;    preload?:boolean|number;    prefetch?:boolean|number;    chunkName?:string;    exports?:string|string[][];    mode?:'sync'|'eager'|'weak'|'lazy'|'lazy-once';})=> webpack.Context;
  • Available: 5.70.0+

  • Example:

    const contextRequire=import.meta.webpackContext('.',{  recursive:false,  regExp:/two/,  mode:'weak',  exclude:/three/,});

__filename (NodeJS)

Depending on the configuration optionnode.__filename:

If used inside an expression that is parsed by the Parser, the configuration option is treated astrue.

__resourceQuery (webpack-specific)

The resource query of the current module. If the followingrequire call was made, then the query string would be available infile.js.

require('file.js?test');

file.js

__resourceQuery==='?test';

__webpack_public_path__ (webpack-specific)

Equals the configuration option'soutput.publicPath.

__webpack_require__ (webpack-specific)

The raw require function. This expression isn't parsed by the Parser for dependencies.

__webpack_chunk_load__ (webpack-specific)

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:

const originalLoad= __webpack_chunk_load__;const publicPaths=['a','b','c'];__webpack_chunk_load__=async(id)=>{let error;for(const pathof publicPaths){    __webpack_public_path__= path;try{returnawaitoriginalLoad(id);}catch(e){      error= e;}}throw error;};import('./module-a').then((moduleA)=>{// now webpack will use the custom __webpack_chunk_load__ to load chunk});

__webpack_module__ (webpack-specific)

5.68.0+

It provides access to the the currentmodule.module is not available in strict ESM.

__webpack_module__.id (webpack-specific)

5.68.0+

It provides access to the ID of currentmodule (module.id).module is not available in strict ESM.

__webpack_modules__ (webpack-specific)

Access to the internal object of all modules.

__webpack_hash__ (webpack-specific)

It provides access to the hash of the compilation.

__webpack_get_script_filename__ (webpack-specific)

function (chunkId)

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.

const oldFn= __webpack_get_script_filename__;__webpack_get_script_filename__=(chunkId)=>{const filename=oldFn(chunkId);return filename+'.changed';};

__non_webpack_require__ (webpack-specific)

Generates arequire function that is not parsed by webpack. Can be used to do cool stuff with a global require function if available.

__webpack_exports_info__ (webpack-specific)

In modules,__webpack_exports_info__ is available to allow exports introspection:

  • __webpack_exports_info__ is alwaystrue

  • __webpack_exports_info__.<exportName>.used isfalse when the export is known to be unused,true otherwise

  • __webpack_exports_info__.<exportName>.useInfo is

    • false when the export is known to be unused
    • true when the export is known to be used
    • null when the export usage could depend on runtime conditions
    • undefined when no info is available
  • __webpack_exports_info__.<exportName>.provideInfo is

    • false when the export is known to be not provided
    • true when the export is known to be provided
    • null when the export provision could depend on runtime conditions
    • undefined when no info is available
  • Accessing the info from nested exports is possible: i. e.__webpack_exports_info__.<exportName>.<exportName>.<exportName>.used

  • Check whether exports can be mangled with__webpack_exports_info__.<name>.canMangle

__webpack_is_included__ (webpack-specific)

5.16.0+

Test whether or not the given module is bundled by webpack.

if(__webpack_is_included__('./module-a.js')){// do something}

__webpack_base_uri__ (webpack-specific)

Change base URI at runtime.

  • Type:string

  • Available: 5.21.0+

  • Example:

    __webpack_base_uri__='https://example.com';

__webpack_runtime_id__

Access theruntime id of current entry.

This is a webpack specific feature and it's available since webpack 5.25.0.

src/index.js

console.log(__webpack_runtime_id__==='main');

DEBUG (webpack-specific)

Equals the configuration optiondebug.

11 Contributors

skipjacksokraahmehritbroadleybyzykEugeneHlushkowizardofhogwartsanikethsahachenxsanjamesgeorge007snitin315

[8]ページ先頭

©2009-2025 Movatter.jp