-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathwebpack-plugin.ts
37 lines (34 loc) · 1.58 KB
/
webpack-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @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 { Compiler, WebpackFourCompiler } from 'webpack';
interface ModuleData {
resourceResolveData: { descriptionFileData?: { typings?: string } };
}
export class BuildOptimizerWebpackPlugin {
apply(compiler: Compiler | WebpackFourCompiler) {
(compiler as Compiler).hooks.normalModuleFactory.tap('BuildOptimizerWebpackPlugin', nmf => {
// tslint:disable-next-line: no-any
nmf.hooks.module.tap('BuildOptimizerWebpackPlugin', (module, data) => {
const { descriptionFileData } = (data as ModuleData).resourceResolveData;
if (descriptionFileData) {
// Only TS packages should use Build Optimizer.
// Notes:
// - a TS package might not have defined typings but still use .d.ts files next to their
// .js files. We don't cover that case because the Angular Package Format (APF) calls for
// using the Typings field and Build Optimizer is geared towards APF. Maybe we could
// provide configuration options to the plugin to cover that case if there's demand.
// - a JS-only package that also happens to provides typings will also be flagged by this
// check. Not sure there's a good way to skip those.
const skipBuildOptimizer = !descriptionFileData.typings;
module.factoryMeta = { ...module.factoryMeta, skipBuildOptimizer };
}
return module;
});
});
}
}