Skip to content

Commit 885a773

Browse files
committed
blog: add experimental JavaScript parsing example with oxc-parser in Webpack 5.106 blog post
1 parent 31582b2 commit 885a773

1 file changed

Lines changed: 47 additions & 8 deletions

File tree

src/content/blog/2026-03-13-webpack-5-106.mdx

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ module.exports = {
8282
Webpack can now statically analyze **destructuring assignments directly from CommonJS `require`** (and `module.require`) and treat only the destructured properties as “referenced exports”, instead of conservatively assuming the whole `exports` object is used. This improves dead-code elimination in optimized builds and can reduce bundle size in codebases that still consume CommonJS modules.
8383

8484
**Examples**
85+
8586
```js
8687
// Only `a` is considered referenced
8788
const { a } = require("./module");
89+
```
8890

91+
```js
8992
// Also supported
9093
const { a, b } = module.require("./module");
9194
```
@@ -96,12 +99,12 @@ const { a, b } = module.require("./module");
9699

97100
This makes virtual modules behave more like real files: code such as `import "./utils"` resolves consistently instead of falling back to `compiler.context` and potentially resolving incorrectly.
98101

99-
`context` can be set per virtual module (inside the module definition) or as a plugin level default. It defaults to `"auto"`, which tries to infer the context from the virtual module id or path; otherwise it falls back to `compiler.context`. Conceptually, when you set `context` for a module, webpack treats that virtual module *as if it lived inside that directory* for resolving relative paths.
102+
`context` can be set per virtual module (inside the module definition) or as a plugin level default. It defaults to `"auto"`, which tries to infer the context from the virtual module id or path; otherwise it falls back to `compiler.context`. Conceptually, when you set `context` for a module, webpack treats that virtual module _as if it lived inside that directory_ for resolving relative paths.
100103

101104
For example, if you define a virtual module id `virtual/table.js` with `context: path.join(__dirname, "src/components")`, then its internal `import "./utils"` is resolved as if the file were `src/components/table.js` importing `src/components/utils.js`.
102105

103106
```js
104-
const path = require("path");
107+
const path = require("node:path");
105108
const webpack = require("webpack");
106109

107110
module.exports = {
@@ -112,17 +115,53 @@ module.exports = {
112115
context: "auto",
113116
source() {
114117
return "import { trim } from './utils'; export const button = trim('button ');";
115-
}
118+
},
116119
},
117120
"virtual/table.js": {
118121
context: path.join(__dirname, "src/components"),
119122
source() {
120123
return "import { trim } from './utils'; export const table = trim('table ');";
121-
}
122-
}
124+
},
125+
},
123126
},
124-
{ context: "auto" }
125-
)
126-
]
127+
{ context: "auto" },
128+
),
129+
],
127130
};
128131
```
132+
133+
## Experimental JavaScript Parsing with `oxc-parser`
134+
135+
Webpack now includes an example demonstrating how to replace the default JavaScript parser with **`oxc-parser`**. This integration should be considered **purely experimental** and is **not recommended for production use**.
136+
137+
Instead, it is intended for **development environments** or **benchmark branches**, allowing the community to experiment with alternative parsing strategies in real projects. This helps evaluate potential improvements in **parse time and build performance**, as well as identify possible **compatibility issues**.
138+
139+
**Example**
140+
141+
The following configuration limits the custom parser to `.js` files:
142+
143+
```js
144+
"use strict";
145+
146+
const oxcParse = require("./internals/oxc-parse");
147+
148+
/** @type {import("webpack").Configuration} */
149+
module.exports = {
150+
mode: "production",
151+
entry: "./src/index.js",
152+
module: {
153+
rules: [
154+
{
155+
// Apply the custom parser only to JavaScript files
156+
test: /\.js$/,
157+
parser: {
158+
parse: oxcParse,
159+
},
160+
},
161+
],
162+
},
163+
};
164+
```
165+
166+
You can find the full example in the webpack repository:
167+
[https://github.com/webpack/webpack/blob/main/examples/custom-javascript-parser/webpack.config.js](https://github.com/webpack/webpack/blob/main/examples/custom-javascript-parser/webpack.config.js)

0 commit comments

Comments
 (0)