Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ const makeSerializable = require("./util/makeSerializable");

/**
* @typedef {object} KnownBuildMeta
* @property {string=} moduleArgument
* @property {string=} exportsArgument
* @property {boolean=} strict
* @property {string=} moduleConcatenationBailout
* @property {("default" | "namespace" | "flagged" | "dynamic")=} exportsType
* @property {(false | "redirect" | "redirect-warn")=} defaultObject
* @property {boolean=} strictHarmonyModule
Expand All @@ -113,6 +109,10 @@ const makeSerializable = require("./util/makeSerializable");
* @typedef {object} KnownBuildInfo
* @property {boolean=} cacheable
* @property {boolean=} parsed
* @property {string=} moduleArgument
* @property {string=} exportsArgument
* @property {boolean=} strict
* @property {string=} moduleConcatenationBailout
* @property {LazySet<string>=} fileDependencies
* @property {LazySet<string>=} contextDependencies
* @property {LazySet<string>=} missingDependencies
Expand Down
30 changes: 30 additions & 0 deletions lib/dependencies/CommonJsImportsParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,36 @@ class CommonJsImportsParserPlugin {
* @returns {boolean | void} true when handled
*/
const processResolve = (expr, weak) => {
if (!weak && options.commonjsMagicComments) {
const { options: requireOptions, errors: commentErrors } =
parser.parseCommentOptions(/** @type {Range} */ (expr.range));

if (commentErrors) {
for (const e of commentErrors) {
const { comment } = e;
parser.state.module.addWarning(
new CommentCompilationWarning(
`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
/** @type {DependencyLocation} */ (comment.loc)
)
);
}
}
if (requireOptions && requireOptions.webpackIgnore !== undefined) {
if (typeof requireOptions.webpackIgnore !== "boolean") {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackIgnore\` expected a boolean, but received: ${requireOptions.webpackIgnore}.`,
/** @type {DependencyLocation} */ (expr.loc)
)
);
} else if (requireOptions.webpackIgnore) {
// Do not instrument `require()` if `webpackIgnore` is `true`
return true;
}
}
}

if (expr.arguments.length !== 1) return;
const param = parser.evaluateExpression(expr.arguments[0]);
if (param.isConditional()) {
Expand Down
9 changes: 9 additions & 0 deletions test/configCases/parsing/require-resolve-ignore/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require("fs");
const path = require("path");

it("should be able to ignore require.resolve()", () => {
const source = fs.readFileSync(path.join(__dirname, "bundle1.js"), "utf-8");
expect(source).toMatch(`require.resolve(/* webpackIgnore: true */ "./non-exists")`);
expect(source).toMatch(`createRequire(import.meta.url).resolve(/* webpackIgnore: true */ "./non-exists")`);
expect(source).toMatch(`require.resolve(/* webpackIgnore: true */ "./non-exists")`);
});
8 changes: 8 additions & 0 deletions test/configCases/parsing/require-resolve-ignore/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createRequire } from 'node:module';

const resolve = require.resolve(/* webpackIgnore: true */ "./non-exists");
const createRequireResolve1 = createRequire(import.meta.url).resolve(/* webpackIgnore: true */ "./non-exists");
const require = createRequire(import.meta.url);
const createRequireResolve2 = require.resolve(/* webpackIgnore: true */ "./non-exists");

export { resolve, createRequireResolve1, createRequireResolve2 }
20 changes: 20 additions & 0 deletions test/configCases/parsing/require-resolve-ignore/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
entry: {
bundle0: "./index.js",
bundle1: "./other.js"
},
module: {
parser: {
javascript: {
commonjsMagicComments: true
}
}
},
output: {
filename: "[name].js"
},
node: {
__dirname: false
}
};
8 changes: 4 additions & 4 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7482,6 +7482,10 @@ declare interface KnownAssetInfo {
declare interface KnownBuildInfo {
cacheable?: boolean;
parsed?: boolean;
moduleArgument?: string;
exportsArgument?: string;
strict?: boolean;
moduleConcatenationBailout?: string;
fileDependencies?: LazySet<string>;
contextDependencies?: LazySet<string>;
missingDependencies?: LazySet<string>;
Expand All @@ -7493,10 +7497,6 @@ declare interface KnownBuildInfo {
snapshot?: null | Snapshot;
}
declare interface KnownBuildMeta {
moduleArgument?: string;
exportsArgument?: string;
strict?: boolean;
moduleConcatenationBailout?: string;
exportsType?: "namespace" | "dynamic" | "default" | "flagged";
defaultObject?: false | "redirect" | "redirect-warn";
strictHarmonyModule?: boolean;
Expand Down
Loading