-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular-devkit/build-angular): show warnings when depending on …
…CommonJS. Depending on CommonJS modules is know to cause optimization bailouts. With this change when running a browser build and scripts optimization is enabled we display a warning. To suppress the warning for a particular package, users can use the `allowedCommonJsDepedencies` builder options. Example: ``` "build": { "builder": "@angular-devkit/build-angular:browser", "options": { ... "allowedCommonJsDepedencies": ["bootstrap"] }, } ``` Reference: TOOL-1328
- Loading branch information
1 parent
ed90080
commit ea11c55
Showing
8 changed files
with
202 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...angular_devkit/build_angular/src/angular-cli-files/plugins/common-js-usage-warn-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
|
||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { isAbsolute } from 'path'; | ||
import { Compiler, compilation } from 'webpack'; | ||
|
||
// Webpack doesn't export these so the deep imports can potentially break. | ||
const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency'); | ||
const AMDDefineDependency = require('webpack/lib/dependencies/AMDDefineDependency'); | ||
|
||
// The below is extended because there are not in the typings | ||
interface WebpackModule extends compilation.Module { | ||
name?: string; | ||
rawRequest?: string; | ||
dependencies: unknown[]; | ||
issuer: WebpackModule | null; | ||
userRequest?: string; | ||
} | ||
|
||
export interface CommonJsUsageWarnPluginOptions { | ||
/** A list of CommonJS packages that are allowed to be used without a warning. */ | ||
allowedDepedencies?: string[]; | ||
} | ||
|
||
export class CommonJsUsageWarnPlugin { | ||
private shownWarnings = new Set<string>(); | ||
|
||
constructor(private options: CommonJsUsageWarnPluginOptions = {}) { | ||
|
||
} | ||
|
||
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[]) { | ||
if ( | ||
!rawRequest || | ||
rawRequest.startsWith('.') || | ||
isAbsolute(rawRequest) | ||
) { | ||
// Skip if module is absolute or relative. | ||
continue; | ||
} | ||
|
||
if (this.options.allowedDepedencies?.includes(rawRequest)) { | ||
// Skip as this module is allowed even if it's a CommonJS. | ||
continue; | ||
} | ||
|
||
if (this.hasCommonJsDependencies(dependencies)) { | ||
// Dependency is CommonsJS or AMD. | ||
|
||
// 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. | ||
if (this.hasCommonJsDependencies(issuer?.issuer?.dependencies ?? [])) { | ||
continue; | ||
} | ||
|
||
// Find the main issuer (entry-point). | ||
let mainIssuer = issuer; | ||
while (mainIssuer?.issuer) { | ||
mainIssuer = mainIssuer.issuer; | ||
} | ||
|
||
// Only show warnings for modules from main entrypoint. | ||
if (mainIssuer?.name === 'main') { | ||
const warning = `${issuer?.userRequest} depends on ${rawRequest}. CommonJS or AMD dependencies can cause optimization bailouts.`; | ||
|
||
// Avoid showing the same warning multiple times when in 'watch' mode. | ||
if (!this.shownWarnings.has(warning)) { | ||
compilation.warnings.push(warning); | ||
this.shownWarnings.add(warning); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
private hasCommonJsDependencies(dependencies: unknown[]): boolean { | ||
return dependencies.some(d => d instanceof CommonJsRequireDependency || d instanceof AMDDefineDependency); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/angular_devkit/build_angular/test/browser/common-js-warning_spec_large.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import { Architect } from '@angular-devkit/architect'; | ||
import { logging } from '@angular-devkit/core'; | ||
import { createArchitect, host } from '../utils'; | ||
|
||
describe('Browser Builder commonjs warning', () => { | ||
const targetSpec = { project: 'app', target: 'build' }; | ||
|
||
let architect: Architect; | ||
let logger: logging.Logger; | ||
let logs: string[]; | ||
|
||
beforeEach(async () => { | ||
await host.initialize().toPromise(); | ||
architect = (await createArchitect(host.root())).architect; | ||
|
||
// Add a Common JS dependency | ||
host.appendToFile('src/app/app.component.ts', `import 'bootstrap';`); | ||
|
||
// Create logger | ||
logger = new logging.Logger(''); | ||
logs = []; | ||
logger.subscribe(e => logs.push(e.message)); | ||
}); | ||
|
||
afterEach(async () => host.restore().toPromise()); | ||
|
||
it('should show warning when depending on a Common JS bundle', async () => { | ||
const run = await architect.scheduleTarget(targetSpec, undefined, { logger }); | ||
const output = await run.result; | ||
expect(output.success).toBe(true); | ||
const logMsg = logs.join(); | ||
expect(logMsg).toMatch(/WARNING in.+app\.component\.ts depends on bootstrap\. CommonJS or AMD dependencies/); | ||
expect(logMsg).not.toContain('jquery', 'Should not warn on transitive CommonJS packages which parent is also CommonJS.'); | ||
await run.stop(); | ||
}); | ||
|
||
it('should not show warning when depending on a Common JS bundle which is allowed', async () => { | ||
const overrides = { | ||
allowedCommonJsDependencies: [ | ||
'bootstrap', | ||
], | ||
}; | ||
|
||
const run = await architect.scheduleTarget(targetSpec, overrides, { logger }); | ||
const output = await run.result; | ||
expect(output.success).toBe(true); | ||
expect(logs.join()).not.toContain('WARNING'); | ||
await run.stop(); | ||
}); | ||
}); |