Skip to content

Commit 5649383

Browse files
authored
docs: add documentation for webpack v5.103.0, v5.104.0, and v5.105.0 features (#7788)
1 parent 21e0573 commit 5649383

9 files changed

Lines changed: 463 additions & 3 deletions

File tree

src/content/api/module-variables.mdx

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,94 @@ Returns the same value as `require.context` but only for `javascript/auto` and `
155155
});
156156
```
157157
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+
const webpack = 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
196+
new webpack.EnvironmentPlugin({
197+
API_URL: "https://api.example.com",
198+
}),
199+
// DefinePlugin allows custom definitions
200+
new webpack.DefinePlugin({
201+
"import.meta.env.CUSTOM_VAR": JSON.stringify("custom_value"),
202+
}),
203+
],
204+
};
205+
```
206+
207+
**src/index.js**
208+
209+
```js
210+
// Access environment variables
211+
console.log(import.meta.env.NODE_ENV); // "production" (from mode)
212+
console.log(import.meta.env.API_URL); // Value from EnvironmentPlugin or process.env
213+
console.log(import.meta.env.CUSTOM_VAR); // "custom_value" (from DefinePlugin)
214+
215+
// Type checking
216+
console.log(typeof import.meta.env); // "object"
217+
218+
// Truthy check
219+
if (import.meta.env) {
220+
// import.meta.env is truthy
221+
}
222+
223+
// Accessing non-existent properties returns undefined
224+
if (!import.meta.env.NOT_EXIST) {
225+
// NOT_EXIST is undefined/falsy
226+
}
227+
228+
// Destructuring assignment
229+
const { NODE_ENV, API_URL } = import.meta.env;
230+
```
231+
232+
**Sources of environment variables:**
233+
234+
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+
158246
## \_\_filename (NodeJS)
159247
160248
Depending on the configuration option `node.__filename`:
@@ -187,6 +275,8 @@ Equals the configuration option's `output.publicPath`.
187275
188276
The raw require function. This expression isn't parsed by the Parser for dependencies.
189277
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+
190280
## \_\_webpack_chunk_load\_\_ (webpack-specific)
191281
192282
The internal chunk loading function. Takes one argument:
@@ -256,6 +346,18 @@ __webpack_get_script_filename__ = (chunkId) => {
256346
257347
Generates a `require` function that is not parsed by webpack. Can be used to do cool stuff with a global require function if available.
258348
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.
354+
355+
**src/module.js**
356+
357+
```js
358+
__webpack_global__.myProperty = "value";
359+
```
360+
259361
## \_\_webpack_exports_info\_\_ (webpack-specific)
260362
261363
In modules, `__webpack_exports_info__` is available to allow exports introspection:

src/content/configuration/devtool.mdx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,34 @@ Use the [`SourceMapDevToolPlugin`](/plugins/source-map-dev-tool-plugin) for a mo
2222

2323
## devtool
2424

25-
`string = 'eval'` `false`
25+
`string = 'eval'` `Array<{ type: "all" | "javascript" | "css", use: string }>` `false`
2626

2727
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.
2828

29+
<Badge text="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+
2953
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.
3054

3155
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.

src/content/configuration/experiments.mdx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,38 @@ Enable native CSS support. Note that it's an experimental feature still under de
190190
Experimental features:
191191

192192
- CSS Modules support: webpack will generate a unique name for each CSS class. Use the `.module.css` extension for CSS Modules.
193+
194+
<Badge text="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+
193225
- <Badge text="5.87.0+" /> Style-specific fields resolution in `package.json`
194226
files: webpack will look for `style` field in `package.json` files and use
195227
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
256288

257289
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.
258290

259-
#### Not implemented
291+
<Badge text="5.105.0+" />
260292

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.
262294

263295
### experiments.futureDefaults
264296

src/content/configuration/module.mdx

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,144 @@ import { footer, header } from "./styles.module.css";
446446

447447
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.
448448

449+
#### module.parser.css.animation
450+
451+
<Badge text="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+
<Badge text="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+
<Badge text="5.104.0+" />
498+
499+
Enable or disable renaming of custom identifiers in CSS modules.
500+
501+
- Type: `boolean`
502+
- Default: `true`
503+
504+
**webpack.config.js**
505+
506+
```js
507+
module.exports = {
508+
module: {
509+
parser: {
510+
css: {
511+
customIdents: true, // Enable custom identifier renaming
512+
},
513+
},
514+
},
515+
};
516+
```
517+
518+
#### module.parser.css.dashedIdents
519+
520+
<Badge text="5.104.0+" />
521+
522+
Enable or disable renaming of dashed identifiers, such as CSS custom properties (e.g., `--my-variable`).
523+
524+
- Type: `boolean`
525+
- Default: `true`
526+
527+
**webpack.config.js**
528+
529+
```js
530+
module.exports = {
531+
module: {
532+
parser: {
533+
css: {
534+
dashedIdents: true, // Enable dashed identifier renaming
535+
},
536+
},
537+
},
538+
};
539+
```
540+
541+
#### module.parser.css.function
542+
543+
<Badge text="5.104.0+" />
544+
545+
Enable or disable renaming of `@function` names in CSS modules.
546+
547+
- Type: `boolean`
548+
- Default: `true`
549+
550+
**webpack.config.js**
551+
552+
```js
553+
module.exports = {
554+
module: {
555+
parser: {
556+
css: {
557+
function: true, // Enable @function renaming
558+
},
559+
},
560+
},
561+
};
562+
```
563+
564+
#### module.parser.css.grid
565+
566+
<Badge text="5.104.0+" />
567+
568+
Enable or disable renaming of grid identifiers in CSS modules.
569+
570+
- Type: `boolean`
571+
- Default: `true`
572+
573+
**webpack.config.js**
574+
575+
```js
576+
module.exports = {
577+
module: {
578+
parser: {
579+
css: {
580+
grid: true, // Enable grid identifier renaming
581+
},
582+
},
583+
},
584+
};
585+
```
586+
449587
### module.parser.css.exportType
450588

451589
Configure how CSS content will be exported.

src/content/configuration/output.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,31 @@ module.exports = {
744744

745745
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.
746746

747+
<Badge text="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
768+
},
769+
};
770+
```
771+
747772
## output.hashDigestLength
748773

749774
`number = 20`

0 commit comments

Comments
 (0)