Skip to content

Commit

Permalink
fix modules concatenation with importModule usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Apr 19, 2022
1 parent a72548f commit 3f00b12
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/dependencies/LoaderPlugin.js
Expand Up @@ -63,6 +63,11 @@ class LoaderPlugin {

compiler.hooks.compilation.tap("LoaderPlugin", compilation => {
const moduleGraph = compilation.moduleGraph;
const disableConnection = dep => {
const connection = moduleGraph.getConnection(dep);
connection.setActive(false);
};

NormalModule.getCompilationHooks(compilation).loader.tap(
"LoaderPlugin",
loaderContext => {
Expand Down Expand Up @@ -117,6 +122,7 @@ class LoaderPlugin {
)
);
}
disableConnection(dep);
let source, map;
if (moduleSource.sourceAndMap) {
const sourceAndMap = moduleSource.sourceAndMap();
Expand Down Expand Up @@ -196,6 +202,7 @@ class LoaderPlugin {
if (!referencedModule) {
return callback(new Error("Cannot load the module"));
}
disableConnection(dep);
compilation.executeModule(
referencedModule,
{
Expand Down
2 changes: 1 addition & 1 deletion lib/optimize/ModuleConcatenationPlugin.js
Expand Up @@ -606,7 +606,7 @@ class ModuleConcatenationPlugin {
incomingConnectionsFromNonModules.filter(connection => {
// We are not interested in inactive connections
// or connections without dependency
return connection.isActive(runtime) || connection.dependency;
return connection.isActive(runtime) && connection.dependency;
});
if (activeNonModulesConnections.length > 0) {
const problem = requestShortener => {
Expand Down
1 change: 1 addition & 0 deletions test/configCases/concatenate-modules/import-module/a.txt
@@ -0,0 +1 @@
data
7 changes: 7 additions & 0 deletions test/configCases/concatenate-modules/import-module/index.js
@@ -0,0 +1,7 @@
import url from "./loader!!";
import {url as url2} from "./module1";

it("should compile and run", () => {
expect(url).toBe("webpack:///a.txt");
expect(url2.toString()).toMatch(/^file:/);
});
8 changes: 8 additions & 0 deletions test/configCases/concatenate-modules/import-module/loader.js
@@ -0,0 +1,8 @@
/** @type {import("../../../../").LoaderDefinitionFunction} */
module.exports = function () {
const callback = this.async();
this.importModule("./module1", { baseUri: "webpack://" }, (err, exports) => {
if (err) return callback(err);
callback(null, `module.exports = ${JSON.stringify(exports.url)}`);
});
};
3 changes: 3 additions & 0 deletions test/configCases/concatenate-modules/import-module/module1.js
@@ -0,0 +1,3 @@
const url = new URL("./a.txt", import.meta.url);

export { url }
@@ -0,0 +1,6 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
output: {
assetModuleFilename: "[name][ext]"
}
};

0 comments on commit 3f00b12

Please sign in to comment.