Skip to content

Commit

Permalink
Merge pull request #746 from janicduplessis/webpack-5-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
j0k3r authored Apr 7, 2021
2 parents 89b2c99 + 4744341 commit 5cdefda
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 244 deletions.
46 changes: 26 additions & 20 deletions lib/packExternalModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function getProdModules(externalModules, packagePath, dependencyGraph, forceExcl
const originInfo = _.get(dependencyGraph, 'dependencies', {})[module.origin] || {};
moduleVersion = _.get(_.get(originInfo, 'dependencies', {})[module.external], 'version');
if (!moduleVersion) {
// eslint-disable-next-line lodash/path-style
// eslint-disable-next-line lodash/path-style
moduleVersion = _.get(dependencyGraph, [ 'dependencies', module.external, 'version' ]);
}
if (!moduleVersion) {
Expand Down Expand Up @@ -182,36 +182,42 @@ function isExternalModule(module) {
return _.startsWith(module.identifier(), 'external ') && !isBuiltinModule(getExternalModuleName(module));
}

/**
* Gets the module issuer. The ModuleGraph api does not exists in webpack@4
* so falls back to using module.issuer.
*/
function getIssuerCompat(moduleGraph, module) {
if (moduleGraph) {
return moduleGraph.getIssuer(module);
}

return module.issuer;
}

/**
* Find the original module that required the transient dependency. Returns
* undefined if the module is a first level dependency.
* @param {Object} moduleGraph - Webpack module graph
* @param {Object} issuer - Module issuer
*/
function findExternalOrigin(issuer) {
function findExternalOrigin(moduleGraph, issuer) {
if (!_.isNil(issuer) && _.startsWith(issuer.rawRequest, './')) {
return findExternalOrigin(issuer.issuer);
return findExternalOrigin(moduleGraph, getIssuerCompat(moduleGraph, issuer));
}
return issuer;
}

function getExternalModules(stats) {
if (!stats.compilation.chunks) {
return [];
}
function getExternalModules({ compilation }) {
const externals = new Set();
for (const chunk of stats.compilation.chunks) {
if (!chunk.modulesIterable) {
continue;
}

// Explore each module within the chunk (built inputs):
for (const module of chunk.modulesIterable) {
if (isExternalModule(module)) {
externals.add({
origin: _.get(findExternalOrigin(module.issuer), 'rawRequest'),
external: getExternalModuleName(module)
});
}
for (const module of compilation.modules) {
if (isExternalModule(module)) {
externals.add({
origin: _.get(
findExternalOrigin(compilation.moduleGraph, getIssuerCompat(compilation.moduleGraph, module)),
'rawRequest'
),
external: getExternalModuleName(module)
});
}
}
return Array.from(externals);
Expand Down
Loading

0 comments on commit 5cdefda

Please sign in to comment.