Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): fixed Module.issuer is deprecated…
Browse files Browse the repository at this point in the history
… in webpack 5 for commonjs warn plugin
  • Loading branch information
valorkin authored and alan-agius4 committed Sep 28, 2020
1 parent aaa7c82 commit c8f8640
Showing 1 changed file with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export class CommonJsUsageWarnPlugin {
apply(compiler: Compiler) {
compiler.hooks.compilation.tap('CommonJsUsageWarnPlugin', compilation => {
compilation.hooks.finishModules.tap('CommonJsUsageWarnPlugin', modules => {
for (const {dependencies, rawRequest, issuer} of modules as unknown as WebpackModule[]) {
for (const module of modules as unknown as WebpackModule[]) {
const {dependencies, rawRequest} = module;
if (
!rawRequest ||
rawRequest.startsWith('.') ||
Expand All @@ -66,18 +67,20 @@ export class CommonJsUsageWarnPlugin {

if (this.hasCommonJsDependencies(compilation, dependencies)) {
// Dependency is CommonsJS or AMD.

const issuer = getIssuer(compilation, module);
// Check if it's parent issuer is also a CommonJS dependency.
// In case it is skip as an warning will be show for the parent CommonJS dependency.
const parentDependencies = issuer?.issuer?.dependencies;
const parentDependencies = getIssuer(compilation, issuer)?.dependencies;
if (parentDependencies && this.hasCommonJsDependencies(compilation, parentDependencies, true)) {
continue;
}

// Find the main issuer (entry-point).
let mainIssuer = issuer;
while (mainIssuer?.issuer) {
mainIssuer = mainIssuer.issuer;
let nextIssuer = getIssuer(compilation, mainIssuer);
while (nextIssuer) {
mainIssuer = nextIssuer;
nextIssuer = getIssuer(compilation, mainIssuer);
}

// Only show warnings for modules from main entrypoint.
Expand Down Expand Up @@ -129,6 +132,19 @@ export class CommonJsUsageWarnPlugin {
}
}

function getIssuer(compilation: compilation.Compilation, module: WebpackModule | null): WebpackModule | null {
if (!module) {
return null;
}

if (!isWebpackFiveOrHigher()) {
return module?.issuer;
}

return (compilation as unknown as { moduleGraph: { getIssuer(dependency: WebpackModule): WebpackModule; } })
.moduleGraph.getIssuer(module);
}

function getWebpackModule(compilation: compilation.Compilation, dependency: WebpackModule): WebpackModule | null {
if (!isWebpackFiveOrHigher()) {
return dependency.module;
Expand Down

0 comments on commit c8f8640

Please sign in to comment.