Skip to content

Commit 1f83f30

Browse files
clydinfilipesilva
authored andcommitted
feat(@angular-devkit/build-optimizer): support Webpack 5
The `@angular-devkit/build-optimizer` package now officially supports Webpack 5. Webpack 4 support is temporarily maintained while the remainder of the tooling is transitioned.
1 parent a408aee commit 1f83f30

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

etc/api/angular_devkit/build_optimizer/src/_golden-api.d.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ export declare class BuildOptimizerWebpackPlugin {
66
apply(compiler: Compiler): void;
77
}
88

9-
export default function buildOptimizerLoader(this: webpack.loader.LoaderContext, content: string, previousSourceMap: RawSourceMap): void;
9+
export default function buildOptimizerLoader(this: {
10+
resourcePath: string;
11+
_module: {
12+
factoryMeta: {
13+
skipBuildOptimizer?: boolean;
14+
sideEffectFree?: boolean;
15+
};
16+
};
17+
cacheable(): void;
18+
callback(error?: Error | null, content?: string, sourceMap?: unknown): void;
19+
}, content: string, previousSourceMap: RawSourceMap): void;
1020

1121
export declare function getPrefixClassesTransformer(): ts.TransformerFactory<ts.SourceFile>;
1222

packages/angular_devkit/build_optimizer/package.json

+8
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,13 @@
1414
"tslib": "2.1.0",
1515
"typescript": "4.1.5",
1616
"webpack-sources": "2.2.0"
17+
},
18+
"peerDependencies": {
19+
"webpack": "^4.0.0 || ^5.20.0"
20+
},
21+
"peerDependenciesMeta": {
22+
"webpack": {
23+
"optional": true
24+
}
1725
}
1826
}

packages/angular_devkit/build_optimizer/src/build-optimizer/webpack-loader.ts

+11-19
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { RawSourceMap } from 'source-map';
9-
import * as webpack from 'webpack'; // tslint:disable-line:no-implicit-dependencies
109
import { SourceMapSource } from 'webpack-sources';
1110
const loaderUtils = require('loader-utils');
1211

@@ -27,26 +26,25 @@ const alwaysProcess = (path: string) =>
2726
path.endsWith('.ngstyle.js');
2827

2928
export default function buildOptimizerLoader(
30-
this: webpack.loader.LoaderContext,
29+
// Webpack 5 does not provide a LoaderContext type
30+
this: {
31+
resourcePath: string;
32+
_module: { factoryMeta: { skipBuildOptimizer?: boolean; sideEffectFree?: boolean } };
33+
cacheable(): void;
34+
callback(error?: Error | null, content?: string, sourceMap?: unknown): void;
35+
},
3136
content: string,
3237
previousSourceMap: RawSourceMap,
3338
) {
3439
this.cacheable();
35-
const callback = this.async();
36-
if (!callback) {
37-
throw new Error('Async loader support is required.');
38-
}
3940

4041
const skipBuildOptimizer =
4142
this._module && this._module.factoryMeta && this._module.factoryMeta.skipBuildOptimizer;
4243

4344
if (!alwaysProcess(this.resourcePath) && skipBuildOptimizer) {
4445
// Skip loading processing this file with Build Optimizer if we determined in
4546
// BuildOptimizerWebpackPlugin that we shouldn't.
46-
47-
// Webpack typings for previousSourceMap are wrong, they are JSON objects and not strings.
48-
// tslint:disable-next-line:no-any
49-
this.callback(null, content, previousSourceMap as any);
47+
this.callback(null, content, previousSourceMap);
5048

5149
return;
5250
}
@@ -64,8 +62,7 @@ export default function buildOptimizerLoader(
6462
});
6563

6664
if (boOutput.emitSkipped || boOutput.content === null) {
67-
// tslint:disable-next-line:no-any
68-
this.callback(null, content, previousSourceMap as any);
65+
this.callback(null, content, previousSourceMap);
6966

7067
return;
7168
}
@@ -81,10 +78,7 @@ export default function buildOptimizerLoader(
8178

8279
if (previousSourceMap) {
8380
// Use http://sokra.github.io/source-map-visualization/ to validate sourcemaps make sense.
84-
85-
// The last argument is not yet in the typings
86-
// tslint:disable-next-line: no-any
87-
newSourceMap = new (SourceMapSource as any)(
81+
newSourceMap = new SourceMapSource(
8882
newContent,
8983
this.resourcePath,
9084
intermediateSourceMap,
@@ -98,7 +92,5 @@ export default function buildOptimizerLoader(
9892
}
9993
}
10094

101-
// Webpack typings for previousSourceMap are wrong, they are JSON objects and not strings.
102-
// tslint:disable-next-line:no-any
103-
callback(null, newContent, newSourceMap as any);
95+
this.callback(null, newContent, newSourceMap);
10496
}

packages/angular_devkit/build_optimizer/src/build-optimizer/webpack-plugin.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
21
/**
32
* @license
43
* Copyright Google Inc. All Rights Reserved.
54
*
65
* Use of this source code is governed by an MIT-style license that can be
76
* found in the LICENSE file at https://angular.io/license
87
*/
9-
import { Compiler } from 'webpack'; // tslint:disable-line:no-implicit-dependencies
8+
import { Compiler } from 'webpack';
9+
10+
interface ModuleData {
11+
resourceResolveData: { descriptionFileData?: { typings?: string } };
12+
}
1013

1114
export class BuildOptimizerWebpackPlugin {
1215
apply(compiler: Compiler) {
1316
compiler.hooks.normalModuleFactory.tap('BuildOptimizerWebpackPlugin', nmf => {
17+
// tslint:disable-next-line: no-any
1418
nmf.hooks.module.tap('BuildOptimizerWebpackPlugin', (module, data) => {
15-
const { descriptionFileData } = data.resourceResolveData;
19+
const { descriptionFileData } = (data as ModuleData).resourceResolveData;
1620
if (descriptionFileData) {
1721
// Only TS packages should use Build Optimizer.
1822
// Notes:

0 commit comments

Comments
 (0)