8000 feat: implement `/* webpackIgnore: true */` for `require.resolve` (#1… · webpack/webpack@80826c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 80826c5

Browse files
feat: implement /* webpackIgnore: true */ for require.resolve (#19201)
1 parent ac6ffca commit 80826c5

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

lib/dependencies/CommonJsImportsParserPlugin.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,36 @@ class CommonJsImportsParserPlugin {
464464
* @returns {boolean | void} true when handled
465465
*/
466466
const processResolve = (expr, weak) => {
467+
if (!weak && options.commonjsMagicComments) {
468+
const { options: requireOptions, errors: commentErrors } =
469+
parser.parseCommentOptions(/** @type {Range} */ (expr.range));
470+
471+
if (commentErrors) {
472+
for (const e of commentErrors) {
473+
const { comment } = e;
474+
parser.state.module.addWarning(
475+
new CommentCompilationWarning(
476+
`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
477+
/** @type {DependencyLocation} */ (comment.loc)
478+
)
479+
);
480+
}
481+
}
482+
if (requireOptions && requireOptions.webpackIgnore !== undefined) {
483+
if (typeof requireOptions.webpackIgnore !== "boolean") {
484+
parser.state.module.addWarning(
485+
new UnsupportedFeatureWarning(
486+
`\`webpackIgnore\` expected a boolean, but received: ${requireOptions.webpackIgnore}.`,
487+
/** @type {DependencyLocation} */ (expr.loc)
488+
)
489+
);
490+
} else if (requireOptions.webpackIgnore) {
491+
// Do not instrument `require()` if `webpackIgnore` is `true`
492+
return true;
493+
}
494+
}
495+
}
496+
467497
if (expr.arguments.length !== 1) return;
468498
const param = parser.evaluateExpression(expr.arguments[0]);
469499
if (param.isConditional()) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("should be able to ignore require.resolve()", () => {
5+
const source = fs.readFileSync(path.join(__dirname, "bundle1.js"), "utf-8");
6+
expect(source).toMatch(`require.resolve(/* webpackIgnore: true */ "./non-exists")`);
7+
expect(source).toMatch(`createRequire(import.meta.url).resolve(/* webpackIgnore: true */ "./non-exists")`);
8+
expect(source).toMatch(`require.resolve(/* webpackIgnore: true */ "./non-exists")`);
9+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createRequire } from 'node:module';
2+
3+
const resolve = require.resolve(/* webpackIgnore: true */ "./non-exists");
4+
const createRequireResolve1 = createRequire(import.meta.url).resolve(/* webpackIgnore: true */ "./non-exists");
5+
const require = createRequire(import.meta.url);
6+
const createRequireResolve2 = require.resolve(/* webpackIgnore: true */ "./non-exists");
7+
8+
export { resolve, createRequireResolve1, createRequireResolve2 }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/** @type {import("../../../../").Configuration} */
2+
module.exports = {
3+
entry: {
4+
bundle0: "./index.js",
5+
bundle1: "./other.js"
6+
},
7+
module: {
8+
parser: {
9+
javascript: {
10+
commonjsMagicComments: true
11+
}
12+
}
13+
},
14+
output: {
15+
filename: "[name].js"
16+
},
17+
node: {
18+
__dirname: false
19+
}
20+
};

0 commit comments

Comments
 (0)
0