Skip to content

Commit 5cdefda

Browse files
authored
Merge pull request #746 from janicduplessis/webpack-5-fix
2 parents 89b2c99 + 4744341 commit 5cdefda

File tree

2 files changed

+193
-244
lines changed

2 files changed

+193
-244
lines changed

lib/packExternalModules.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ function getProdModules(externalModules, packagePath, dependencyGraph, forceExcl
130130
const originInfo = _.get(dependencyGraph, 'dependencies', {})[module.origin] || {};
131131
moduleVersion = _.get(_.get(originInfo, 'dependencies', {})[module.external], 'version');
132132
if (!moduleVersion) {
133-
// eslint-disable-next-line lodash/path-style
133+
// eslint-disable-next-line lodash/path-style
134134
moduleVersion = _.get(dependencyGraph, [ 'dependencies', module.external, 'version' ]);
135135
}
136136
if (!moduleVersion) {
@@ -182,36 +182,42 @@ function isExternalModule(module) {
182182
return _.startsWith(module.identifier(), 'external ') && !isBuiltinModule(getExternalModuleName(module));
183183
}
184184

185+
/**
186+
* Gets the module issuer. The ModuleGraph api does not exists in webpack@4
187+
* so falls back to using module.issuer.
188+
*/
189+
function getIssuerCompat(moduleGraph, module) {
190+
if (moduleGraph) {
191+
return moduleGraph.getIssuer(module);
192+
}
193+
194+
return module.issuer;
195+
}
196+
185197
/**
186198
* Find the original module that required the transient dependency. Returns
187199
* undefined if the module is a first level dependency.
200+
* @param {Object} moduleGraph - Webpack module graph
188201
* @param {Object} issuer - Module issuer
189202
*/
190-
function findExternalOrigin(issuer) {
203+
function findExternalOrigin(moduleGraph, issuer) {
191204
if (!_.isNil(issuer) && _.startsWith(issuer.rawRequest, './')) {
192-
return findExternalOrigin(issuer.issuer);
205+
return findExternalOrigin(moduleGraph, getIssuerCompat(moduleGraph, issuer));
193206
}
194207
return issuer;
195208
}
196209

197-
function getExternalModules(stats) {
198-
if (!stats.compilation.chunks) {
199-
return [];
200-
}
210+
function getExternalModules({ compilation }) {
201211
const externals = new Set();
202-
for (const chunk of stats.compilation.chunks) {
203-
if (!chunk.modulesIterable) {
204-
continue;
205-
}
206-
207-
// Explore each module within the chunk (built inputs):
208-
for (const module of chunk.modulesIterable) {
209-
if (isExternalModule(module)) {
210-
externals.add({
211-
origin: _.get(findExternalOrigin(module.issuer), 'rawRequest'),
212-
external: getExternalModuleName(module)
213-
});
214-
}
212+
for (const module of compilation.modules) {
213+
if (isExternalModule(module)) {
214+
externals.add({
215+
origin: _.get(
216+
findExternalOrigin(compilation.moduleGraph, getIssuerCompat(compilation.moduleGraph, module)),
217+
'rawRequest'
218+
),
219+
external: getExternalModuleName(module)
220+
});
215221
}
216222
}
217223
return Array.from(externals);

0 commit comments

Comments
 (0)