You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/api/module-variables.mdx
+102Lines changed: 102 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -155,6 +155,94 @@ Returns the same value as `require.context` but only for `javascript/auto` and `
155
155
});
156
156
```
157
157
158
+
### import.meta.main
159
+
160
+
<Badge text="5.103.0+" />
161
+
162
+
Returns a boolean indicating whether the current module is an entry point.
163
+
164
+
**src/index.js** (entry point)
165
+
166
+
```javascript
167
+
console.log(import.meta.main); // true
168
+
```
169
+
170
+
**src/utils.js** (not an entry point)
171
+
172
+
```javascript
173
+
console.log(import.meta.main); // false
174
+
```
175
+
176
+
This is useful for conditional logic that should only run in entry modules, similar to Node.js's `require.main===module` check.
177
+
178
+
### import.meta.env
179
+
180
+
<Badge text="5.103.0+" />
181
+
182
+
Access environment variables from multiple sources. `import.meta.env` is an object that behaves similarly to `process.env`, allowing you to access environment variables defined through various webpack plugins and configuration options.
183
+
184
+
**webpack.config.js**
185
+
186
+
```js
187
+
constwebpack=require("webpack");
188
+
189
+
module.exports= {
190
+
mode:"production", // NODE_ENV is automatically set to "production"
191
+
dotenv: {
192
+
prefix:"WEBPACK_", // Only expose variables with WEBPACK_ prefix
193
+
},
194
+
plugins: [
195
+
// EnvironmentPlugin exposes variables from process.env
1. **`mode` option**: Automatically sets `import.meta.env.NODE_ENV` based on the webpack mode (`development`, `production`, or `none`).
235
+
236
+
2. **[`EnvironmentPlugin`](/plugins/environment-plugin)**: Exposes variables from `process.env` or provides default values.
237
+
238
+
3. **[`DotenvPlugin`](/configuration/dotenv)**: Loads variables from `.env` files. Only variables with the specified prefix (default: `WEBPACK_`) are exposed.
239
+
240
+
4. **[`DefinePlugin`](/plugins/define-plugin)**: Allows custom definitions using the `"import.meta.env.*"` pattern.
241
+
242
+
T> Variables from different sources are merged together. If the same variable is defined in multiple sources, the order of precedence depends on plugin execution order.
243
+
244
+
T> `import.meta.env` behaves like `process.env` - it's an object, supports `typeof` checks, and accessing non-existent properties returns `undefined`.
245
+
158
246
## \_\_filename (NodeJS)
159
247
160
248
Depending on the configuration option `node.__filename`:
@@ -187,6 +275,8 @@ Equals the configuration option's `output.publicPath`.
187
275
188
276
The raw require function. This expression isn't parsed by the Parser for dependencies.
189
277
278
+
W> When webpack bundles are bundled again (e.g., when a previously bundled output is used as input), the [`CompatibilityPlugin`](/plugins/internal-plugins/#compatibilityplugin) will rename `__webpack_require__` from the previous bundle to avoid conflicts (e.g., `__nested_webpack_require_*`). In such cases, use [`__webpack_global__`](#__webpack_global__-webpack-specific) instead, which ensures you can always access the current compilation's `__webpack_require__` function.
279
+
190
280
## \_\_webpack_chunk_load\_\_ (webpack-specific)
191
281
192
282
The internal chunk loading function. Takes one argument:
Generates a `require` function that is not parsed by webpack. Can be used to do cool stuff with a global require function if available.
258
348
349
+
## \_\_webpack_global\_\_ (webpack-specific)
350
+
351
+
<Badge text="5.104.0+" />
352
+
353
+
An alias for [`__webpack_require__`](#__webpack_require__-webpack-specific). When webpack bundles are bundled again (e.g., when a previously bundled output is used as input), the [`CompatibilityPlugin`](/plugins/internal-plugins/#compatibilityplugin) will rename `__webpack_require__` from the previous bundle to avoid conflicts. `__webpack_global__` ensures you can always access the current compilation's `__webpack_require__` function.
Choose a style of [source mapping](http://blog.teamtreehouse.com/introduction-source-maps) to enhance the debugging process. These values can affect build and rebuild speed dramatically.
28
28
29
+
<Badgetext="5.105.0+" />
30
+
31
+
You can also provide an array of objects to configure different source map styles for different asset types:
32
+
33
+
**webpack.config.js**
34
+
35
+
```js
36
+
module.exports= {
37
+
// ...
38
+
devtool: [
39
+
{ type:"javascript", use:"source-map" },
40
+
{ type:"css", use:"inline-source-map" },
41
+
],
42
+
};
43
+
```
44
+
45
+
The `type` field specifies which asset type should receive the devtool value:
46
+
47
+
-`"all"` - applies to all asset types (JavaScript and CSS)
48
+
-`"javascript"` - applies only to JavaScript files
49
+
-`"css"` - applies only to CSS files
50
+
51
+
When using an array, each entry will be processed in order. If you provide a string value, it will be treated as `{ type: "all", use: "your-string-value" }`.
52
+
29
53
T> The webpack repository contains an [example showing the effect of all `devtool` variants](https://github.com/webpack/webpack/tree/master/examples/source-map). Those examples will likely help you to understand the differences.
30
54
31
55
T> Instead of using the `devtool` option you can also use `SourceMapDevToolPlugin`/`EvalSourceMapDevToolPlugin` directly as it has more options. Never use both the `devtool` option and plugin together. The `devtool` option adds the plugin internally so you would end up with the plugin applied twice.
Copy file name to clipboardExpand all lines: src/content/configuration/experiments.mdx
+34-2Lines changed: 34 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -190,6 +190,38 @@ Enable native CSS support. Note that it's an experimental feature still under de
190
190
Experimental features:
191
191
192
192
- CSS Modules support: webpack will generate a unique name for each CSS class. Use the `.module.css` extension for CSS Modules.
193
+
194
+
<Badgetext="5.103.0+" />
195
+
196
+
Webpack natively supports the CSS Modules `composes` property, allowing you to compose classes from the same file, other CSS modules, or global classes:
197
+
198
+
```css
199
+
/* styles.module.css */
200
+
.base {
201
+
color: blue;
202
+
}
203
+
204
+
.button {
205
+
composes: base;
206
+
padding: 10px;
207
+
}
208
+
209
+
.primary {
210
+
composes: button;
211
+
background: blue;
212
+
}
213
+
214
+
/* Compose from another CSS module */
215
+
.composed {
216
+
composes: className from "./other.module.css";
217
+
}
218
+
219
+
/* Compose from global classes */
220
+
.globalComposed {
221
+
composes: global-class from global;
222
+
}
223
+
```
224
+
193
225
- <Badgetext="5.87.0+" /> Style-specific fields resolution in `package.json`
194
226
files: webpack will look for `style` field in `package.json` files and use
195
227
that if it exists for imports inside CSS files.
@@ -256,9 +288,9 @@ TypeScript, Babel, SWC, and Flow.js can be configured to preserve the magic comm
256
288
257
289
Esbuild is _not_ compatible with this feature (see [evanw/esbuild#1439](https://github.com/evanw/esbuild/issues/1439) and [evanw/esbuild#309](https://github.com/evanw/esbuild/issues/309)), but it may support this in the future.
258
290
259
-
#### Not implemented
291
+
<Badgetext="5.105.0+" />
260
292
261
-
import.defer() is not yet supported for ContextModule (the import path is a dynamic expression).
293
+
`import.defer()` is now supported for ContextModule (the import path is a dynamic expression). See the [lazy loading guide](/guides/lazy-loading/#defer-import-example) for examples.
By enabling `namedExports`, you adopt a more modular and maintainable approach to managing CSS in JavaScript projects, leveraging ES module syntax for clearer and more explicit imports.
448
448
449
+
#### module.parser.css.animation
450
+
451
+
<Badgetext="5.104.0+" />
452
+
453
+
Enable or disable renaming of `@keyframes` animation names in CSS modules.
454
+
455
+
- Type: `boolean`
456
+
- Default: `true`
457
+
458
+
**webpack.config.js**
459
+
460
+
```js
461
+
module.exports= {
462
+
module: {
463
+
parser: {
464
+
css: {
465
+
animation:true, // Enable @keyframes renaming
466
+
},
467
+
},
468
+
},
469
+
};
470
+
```
471
+
472
+
#### module.parser.css.container
473
+
474
+
<Badgetext="5.104.0+" />
475
+
476
+
Enable or disable renaming of `@container` names in CSS modules.
477
+
478
+
- Type: `boolean`
479
+
- Default: `true`
480
+
481
+
**webpack.config.js**
482
+
483
+
```js
484
+
module.exports= {
485
+
module: {
486
+
parser: {
487
+
css: {
488
+
container:true, // Enable @container renaming
489
+
},
490
+
},
491
+
},
492
+
};
493
+
```
494
+
495
+
#### module.parser.css.customIdents
496
+
497
+
<Badgetext="5.104.0+" />
498
+
499
+
Enable or disable renaming of custom identifiers in CSS modules.
Copy file name to clipboardExpand all lines: src/content/configuration/output.mdx
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -744,6 +744,31 @@ module.exports = {
744
744
745
745
The encoding to use when generating the hash. All encodings from Node.JS' [`hash.digest`](https://nodejs.org/api/buffer.html#buffers-and-character-encodings) are supported. Using `'base64'` for filenames might be problematic since it has the character `/` in its alphabet. Likewise `'latin1'` could contain any character.
746
746
747
+
<Badgetext="5.104.0+" />
748
+
749
+
In addition to the standard Node.js encodings, webpack also supports the following custom digest algorithms for generating shorter or URL-safe hash values:
750
+
751
+
-`'base64url'` - URL-safe base64 encoding (RFC 4648), recommended for filenames
752
+
-`'base62'` - Base62 encoding (0-9, A-Z, a-z)
753
+
-`'base58'` - Base58 encoding (Bitcoin alphabet)
754
+
-`'base52'` - Base52 encoding (A-Z, a-z)
755
+
-`'base49'` - Base49 encoding
756
+
-`'base36'` - Base36 encoding (0-9, A-Z)
757
+
-`'base32'` - Base32 encoding (RFC 4648)
758
+
-`'base25'` - Base25 encoding
759
+
760
+
These custom encodings are particularly useful for generating shorter hash values in filenames while maintaining URL safety.
761
+
762
+
**webpack.config.js**
763
+
764
+
```js
765
+
module.exports= {
766
+
output: {
767
+
hashDigest:"base64url", // URL-safe encoding for filenames
0 commit comments