Skip to content

Commit

Permalink
Merge pull request #12872 from webpack/bugfix/ignore-asset-modules
Browse files Browse the repository at this point in the history
allow to define "ignored modules" per dependency
  • Loading branch information
sokra committed Mar 11, 2021
2 parents 8ea0a6a + 7245527 commit bcf3cb2
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 20 deletions.
15 changes: 15 additions & 0 deletions lib/Dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

"use strict";

const memoize = require("./util/memoize");

/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./ChunkGraph")} ChunkGraph */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
Expand Down Expand Up @@ -73,6 +75,11 @@
* @property {boolean=} canMangle when false, referenced export can not be mangled, defaults to true
*/

const getIgnoredModule = memoize(() => {
const RawModule = require("./RawModule");
return new RawModule("/* (ignored) */", `ignored`, `(ignored)`);
});

class Dependency {
constructor() {
// TODO check if this can be moved into ModuleDependency
Expand Down Expand Up @@ -187,6 +194,14 @@ class Dependency {
return true;
}

/**
* @param {string} context context directory
* @returns {Module} a module
*/
createIgnoredModule(context) {
return getIgnoredModule();
}

serialize({ write }) {
write(this.weak);
write(this.optional);
Expand Down
10 changes: 1 addition & 9 deletions lib/NormalModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const {
const Module = require("./Module");
const ModuleFactory = require("./ModuleFactory");
const NormalModule = require("./NormalModule");
const RawModule = require("./RawModule");
const BasicEffectRulePlugin = require("./rules/BasicEffectRulePlugin");
const BasicMatcherRulePlugin = require("./rules/BasicMatcherRulePlugin");
const DescriptionDataMatcherRulePlugin = require("./rules/DescriptionDataMatcherRulePlugin");
Expand Down Expand Up @@ -427,14 +426,7 @@ class NormalModuleFactory extends ModuleFactory {

if (!resourceData) {
// ignored
return callback(
null,
new RawModule(
"/* (ignored) */",
`ignored|${request}`,
`${request} (ignored)`
)
);
return callback(null, dependencies[0].createIgnoredModule(context));
}

const userRequest =
Expand Down
16 changes: 11 additions & 5 deletions lib/RawModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ const makeSerializable = require("./util/makeSerializable");
const TYPES = new Set(["javascript"]);

class RawModule extends Module {
constructor(source, identifier, readableIdentifier) {
/**
* @param {string} source source code
* @param {string} identifier unique identifier
* @param {string=} readableIdentifier readable identifier
* @param {ReadonlySet<string>=} runtimeRequirements runtime requirements needed for the source code
*/
constructor(source, identifier, readableIdentifier, runtimeRequirements) {
super("javascript/dynamic", null);
/** @type {string} */
this.sourceStr = source;
/** @type {string} */
this.identifierStr = identifier || this.sourceStr;
/** @type {string} */
this.readableIdentifierStr = readableIdentifier || this.identifierStr;
this.runtimeRequirements = runtimeRequirements || null;
}

/**
Expand Down Expand Up @@ -107,7 +111,7 @@ class RawModule extends Module {
} else {
sources.set("javascript", new RawSource(this.sourceStr));
}
return { sources, runtimeRequirements: null };
return { sources, runtimeRequirements: this.runtimeRequirements };
}

/**
Expand All @@ -126,6 +130,7 @@ class RawModule extends Module {
write(this.sourceStr);
write(this.identifierStr);
write(this.readableIdentifierStr);
write(this.runtimeRequirements);

super.serialize(context);
}
Expand All @@ -136,6 +141,7 @@ class RawModule extends Module {
this.sourceStr = read();
this.identifierStr = read();
this.readableIdentifierStr = read();
this.runtimeRequirements = read();

super.deserialize(context);
}
Expand Down
18 changes: 18 additions & 0 deletions lib/dependencies/ModuleDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

const Dependency = require("../Dependency");
const DependencyTemplate = require("../DependencyTemplate");
const memoize = require("../util/memoize");

/** @typedef {import("../Module")} Module */

const getRawModule = memoize(() => require("../RawModule"));

class ModuleDependency extends Dependency {
/**
Expand All @@ -26,6 +31,19 @@ class ModuleDependency extends Dependency {
return `module${this.request}`;
}

/**
* @param {string} context context directory
* @returns {Module} a module
*/
createIgnoredModule(context) {
const RawModule = getRawModule();
return new RawModule(
"/* (ignored) */",
`ignored|${context}|${this.request}`,
`${this.request} (ignored)`
);
}

serialize(context) {
const { write } = context;
write(this.request);
Expand Down
18 changes: 18 additions & 0 deletions lib/dependencies/URLDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ const {
getDependencyUsedByExportsCondition
} = require("../optimize/InnerGraph");
const makeSerializable = require("../util/makeSerializable");
const memoize = require("../util/memoize");
const ModuleDependency = require("./ModuleDependency");

/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
/** @typedef {import("../util/Hash")} Hash */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */

const getRawModule = memoize(() => require("../RawModule"));

class URLDependency extends ModuleDependency {
/**
* @param {string} request request
Expand Down Expand Up @@ -59,6 +63,20 @@ class URLDependency extends ModuleDependency {
);
}

/**
* @param {string} context context directory
* @returns {Module} a module
*/
createIgnoredModule(context) {
const RawModule = getRawModule();
return new RawModule(
'module.exports = "data:,";',
`ignored-asset`,
`(ignored asset)`,
new Set([RuntimeGlobals.module])
);
}

serialize(context) {
const { write } = context;
write(this.outerRange);
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/ConfigCacheTestCases.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ exports[`ConfigCacheTestCases records issue-2991 exported tests should write rel
\\"./test.js\\": 393,
\\"external \\\\\\"fs\\\\\\"\\": 747,
\\"external \\\\\\"path\\\\\\"\\": 622,
\\"ignored|pkgs/somepackage/foo\\": 713
\\"ignored|./|pkgs/somepackage/foo\\": 907
},
\\"usedIds\\": [
393,
622,
713,
747
747,
907
]
}
}"
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/ConfigTestCases.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ exports[`ConfigTestCases records issue-2991 exported tests should write relative
\\"./test.js\\": 393,
\\"external \\\\\\"fs\\\\\\"\\": 747,
\\"external \\\\\\"path\\\\\\"\\": 622,
\\"ignored|pkgs/somepackage/foo\\": 713
\\"ignored|./|pkgs/somepackage/foo\\": 907
},
\\"usedIds\\": [
393,
622,
713,
747
747,
907
]
}
}"
Expand Down
5 changes: 5 additions & 0 deletions test/configCases/asset-modules/ignore/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const url = new URL("image.png", import.meta.url);

it("should output asset with path", () => {
expect(url + "").toBe("data:,");
});
9 changes: 9 additions & 0 deletions test/configCases/asset-modules/ignore/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
resolve: {
alias: {
"image.png": false
}
}
};
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2408,6 +2408,7 @@ declare class Dependency {
getModuleEvaluationSideEffectsState(
moduleGraph: ModuleGraph
): ConnectionState;
createIgnoredModule(context: string): Module;
serialize(__0: { write: any }): void;
deserialize(__0: { read: any }): void;
module: any;
Expand Down

0 comments on commit bcf3cb2

Please sign in to comment.