mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-23 15:36:23 +08:00
refactor(@angular-devkit/build-angular): cleanup webpack type usage
This commit is contained in:
parent
78e8d3c59c
commit
378faa7011
@ -62,7 +62,7 @@ export function getBrowserConfig(wco: WebpackConfigOptions): webpack.Configurati
|
|||||||
const globalStylesBundleNames = normalizeExtraEntryPoints(styles, 'styles')
|
const globalStylesBundleNames = normalizeExtraEntryPoints(styles, 'styles')
|
||||||
.map(style => style.bundleName);
|
.map(style => style.bundleName);
|
||||||
|
|
||||||
let crossOriginLoading: string | false = false;
|
let crossOriginLoading: 'anonymous' | 'use-credentials' | false = false;
|
||||||
if (subresourceIntegrity && crossOrigin === 'none') {
|
if (subresourceIntegrity && crossOrigin === 'none') {
|
||||||
crossOriginLoading = 'anonymous';
|
crossOriginLoading = 'anonymous';
|
||||||
} else if (crossOrigin !== 'none') {
|
} else if (crossOrigin !== 'none') {
|
||||||
|
@ -16,14 +16,12 @@ import * as path from 'path';
|
|||||||
import { RollupOptions } from 'rollup';
|
import { RollupOptions } from 'rollup';
|
||||||
import { ScriptTarget } from 'typescript';
|
import { ScriptTarget } from 'typescript';
|
||||||
import {
|
import {
|
||||||
ChunkData,
|
|
||||||
Compiler,
|
Compiler,
|
||||||
Configuration,
|
Configuration,
|
||||||
ContextReplacementPlugin,
|
ContextReplacementPlugin,
|
||||||
HashedModuleIdsPlugin,
|
|
||||||
Plugin,
|
|
||||||
Rule,
|
|
||||||
RuleSetLoader,
|
RuleSetLoader,
|
||||||
|
RuleSetRule,
|
||||||
|
compilation,
|
||||||
debug,
|
debug,
|
||||||
} from 'webpack';
|
} from 'webpack';
|
||||||
import { RawSource } from 'webpack-sources';
|
import { RawSource } from 'webpack-sources';
|
||||||
@ -64,9 +62,9 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
|
|||||||
vendor: vendorSourceMap,
|
vendor: vendorSourceMap,
|
||||||
} = buildOptions.sourceMap;
|
} = buildOptions.sourceMap;
|
||||||
|
|
||||||
const extraPlugins: Plugin[] = [];
|
const extraPlugins: { apply(compiler: Compiler): void }[] = [];
|
||||||
const extraRules: Rule[] = [];
|
const extraRules: RuleSetRule[] = [];
|
||||||
const entryPoints: { [key: string]: string[] } = {};
|
const entryPoints: { [key: string]: [string, ...string[]] } = {};
|
||||||
|
|
||||||
// determine hashing format
|
// determine hashing format
|
||||||
const hashFormat = getOutputHashFormat(buildOptions.outputHashing || 'none');
|
const hashFormat = getOutputHashFormat(buildOptions.outputHashing || 'none');
|
||||||
@ -150,7 +148,10 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
|
|||||||
// tslint:disable-next-line: no-any
|
// tslint:disable-next-line: no-any
|
||||||
(compilation.mainTemplate.hooks as any).assetPath.tap(
|
(compilation.mainTemplate.hooks as any).assetPath.tap(
|
||||||
'build-angular',
|
'build-angular',
|
||||||
(filename: string | ((data: ChunkData) => string), data: ChunkData) => {
|
(
|
||||||
|
filename: string | ((data: { chunk: compilation.Chunk }) => string),
|
||||||
|
data: { chunk: compilation.Chunk },
|
||||||
|
) => {
|
||||||
const assetName = typeof filename === 'function' ? filename(data) : filename;
|
const assetName = typeof filename === 'function' ? filename(data) : filename;
|
||||||
const isMap = assetName && assetName.endsWith('.map');
|
const isMap = assetName && assetName.endsWith('.map');
|
||||||
|
|
||||||
@ -177,17 +178,21 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (buildOptions.polyfills) {
|
if (buildOptions.polyfills) {
|
||||||
entryPoints['polyfills'] = [
|
const projectPolyfills = path.resolve(root, buildOptions.polyfills);
|
||||||
...(entryPoints['polyfills'] || []),
|
if (entryPoints['polyfills']) {
|
||||||
path.resolve(root, buildOptions.polyfills),
|
entryPoints['polyfills'].push(projectPolyfills);
|
||||||
];
|
} else {
|
||||||
|
entryPoints['polyfills'] = [projectPolyfills];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!buildOptions.aot) {
|
if (!buildOptions.aot) {
|
||||||
entryPoints['polyfills'] = [
|
const jitPolyfills = path.join(__dirname, '..', 'jit-polyfills.js');
|
||||||
...(entryPoints['polyfills'] || []),
|
if (entryPoints['polyfills']) {
|
||||||
path.join(__dirname, '..', 'jit-polyfills.js'),
|
entryPoints['polyfills'].push(jitPolyfills);
|
||||||
];
|
} else {
|
||||||
|
entryPoints['polyfills'] = [jitPolyfills];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -592,8 +597,9 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
optimization: {
|
optimization: {
|
||||||
|
minimizer: extraMinimizers,
|
||||||
|
moduleIds: 'hashed',
|
||||||
noEmitOnErrors: true,
|
noEmitOnErrors: true,
|
||||||
minimizer: [new HashedModuleIdsPlugin(), ...extraMinimizers],
|
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
// Always replace the context for the System.import in angular/core to prevent warnings.
|
// Always replace the context for the System.import in angular/core to prevent warnings.
|
||||||
|
@ -24,11 +24,11 @@ const postcssImports = require('postcss-import');
|
|||||||
// tslint:disable-next-line:no-big-function
|
// tslint:disable-next-line:no-big-function
|
||||||
export function getStylesConfig(wco: WebpackConfigOptions) {
|
export function getStylesConfig(wco: WebpackConfigOptions) {
|
||||||
const { root, buildOptions } = wco;
|
const { root, buildOptions } = wco;
|
||||||
const entryPoints: { [key: string]: string[] } = {};
|
const entryPoints: { [key: string]: [string, ...string[]] } = {};
|
||||||
const globalStylePaths: string[] = [];
|
const globalStylePaths: string[] = [];
|
||||||
const extraPlugins: webpack.Plugin[] = [
|
const extraPlugins: { apply(compiler: webpack.Compiler): void }[] = [];
|
||||||
new AnyComponentStyleBudgetChecker(buildOptions.budgets),
|
|
||||||
];
|
extraPlugins.push(new AnyComponentStyleBudgetChecker(buildOptions.budgets));
|
||||||
|
|
||||||
const cssSourceMap = buildOptions.sourceMap.styles;
|
const cssSourceMap = buildOptions.sourceMap.styles;
|
||||||
|
|
||||||
|
@ -17,8 +17,8 @@ export function getTestConfig(
|
|||||||
): webpack.Configuration {
|
): webpack.Configuration {
|
||||||
const { root, buildOptions, sourceRoot: include } = wco;
|
const { root, buildOptions, sourceRoot: include } = wco;
|
||||||
|
|
||||||
const extraRules: webpack.Rule[] = [];
|
const extraRules: webpack.RuleSetRule[] = [];
|
||||||
const extraPlugins: webpack.Plugin[] = [];
|
const extraPlugins: { apply(compiler: webpack.Compiler): void }[] = [];
|
||||||
|
|
||||||
if (buildOptions.codeCoverage) {
|
if (buildOptions.codeCoverage) {
|
||||||
const codeCoverageExclude = buildOptions.codeCoverageExclude;
|
const codeCoverageExclude = buildOptions.codeCoverageExclude;
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { Compiler, Plugin } from 'webpack';
|
import { Compiler } from 'webpack';
|
||||||
import { Budget, Type } from '../../../src/browser/schema';
|
import { Budget, Type } from '../../../src/browser/schema';
|
||||||
import { ThresholdSeverity, calculateThresholds, checkThresholds } from '../utilities/bundle-calculator';
|
import { ThresholdSeverity, calculateThresholds, checkThresholds } from '../utilities/bundle-calculator';
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ const PLUGIN_NAME = 'AnyComponentStyleBudgetChecker';
|
|||||||
* Check budget sizes for component styles by emitting a warning or error if a
|
* Check budget sizes for component styles by emitting a warning or error if a
|
||||||
* budget is exceeded by a particular component's styles.
|
* budget is exceeded by a particular component's styles.
|
||||||
*/
|
*/
|
||||||
export class AnyComponentStyleBudgetChecker implements Plugin {
|
export class AnyComponentStyleBudgetChecker {
|
||||||
private readonly budgets: Budget[];
|
private readonly budgets: Budget[];
|
||||||
constructor(budgets: Budget[]) {
|
constructor(budgets: Budget[]) {
|
||||||
this.budgets = budgets.filter((budget) => budget.type === Type.AnyComponentStyle);
|
this.budgets = budgets.filter((budget) => budget.type === Type.AnyComponentStyle);
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
* Use of this source code is governed by an MIT-style license that can be
|
* 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
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import { Compiler, compilation } from 'webpack';
|
import { Compiler } from 'webpack';
|
||||||
import { Budget } from '../../browser/schema';
|
import { Budget } from '../../browser/schema';
|
||||||
import { ProcessBundleResult } from '../../utils/process-bundle';
|
import { ProcessBundleResult } from '../../utils/process-bundle';
|
||||||
import { ThresholdSeverity, checkBudgets } from '../utilities/bundle-calculator';
|
import { ThresholdSeverity, checkBudgets } from '../utilities/bundle-calculator';
|
||||||
@ -24,12 +24,7 @@ export class BundleBudgetPlugin {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
compiler.hooks.afterEmit.tap('BundleBudgetPlugin', (compilation: compilation.Compilation) => {
|
compiler.hooks.afterEmit.tap('BundleBudgetPlugin', (compilation) => {
|
||||||
this.runChecks(budgets, compilation);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private runChecks(budgets: Budget[], compilation: compilation.Compilation) {
|
|
||||||
// No process bundle results because this plugin is only used when differential
|
// No process bundle results because this plugin is only used when differential
|
||||||
// builds are disabled.
|
// builds are disabled.
|
||||||
const processResults: ProcessBundleResult[] = [];
|
const processResults: ProcessBundleResult[] = [];
|
||||||
@ -45,5 +40,6 @@ export class BundleBudgetPlugin {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,11 @@ export class IndexHtmlWebpackPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadOutputFile = (name: string) => compilation.assets[name].source();
|
const loadOutputFile = async (name: string) => {
|
||||||
|
const data = compilation.assets[name].source();
|
||||||
|
|
||||||
|
return typeof data === 'string' ? data : data.toString();
|
||||||
|
};
|
||||||
let indexSource = await augmentIndexHtml({
|
let indexSource = await augmentIndexHtml({
|
||||||
input: this._options.input,
|
input: this._options.input,
|
||||||
inputContent,
|
inputContent,
|
||||||
|
@ -19,17 +19,14 @@ function hook(
|
|||||||
compiler: Compiler,
|
compiler: Compiler,
|
||||||
action: (
|
action: (
|
||||||
compilation: compilation.Compilation,
|
compilation: compilation.Compilation,
|
||||||
chunks: compilation.Chunk[],
|
chunks: Iterable<compilation.Chunk>,
|
||||||
) => Promise<void | void[]>,
|
) => Promise<void>,
|
||||||
) {
|
) {
|
||||||
compiler.hooks.compilation.tap(
|
compiler.hooks.compilation.tap('optimize-css-webpack-plugin', (compilation) => {
|
||||||
'optimize-css-webpack-plugin',
|
compilation.hooks.optimizeChunkAssets.tapPromise('optimize-css-webpack-plugin', (chunks) =>
|
||||||
(compilation: compilation.Compilation) => {
|
|
||||||
compilation.hooks.optimizeChunkAssets.tapPromise('optimize-css-webpack-plugin', chunks =>
|
|
||||||
action(compilation, chunks),
|
action(compilation, chunks),
|
||||||
);
|
);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class OptimizeCssWebpackPlugin {
|
export class OptimizeCssWebpackPlugin {
|
||||||
@ -44,14 +41,18 @@ export class OptimizeCssWebpackPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
apply(compiler: Compiler): void {
|
apply(compiler: Compiler): void {
|
||||||
hook(compiler, (compilation: compilation.Compilation, chunks: compilation.Chunk[]) => {
|
hook(compiler, (compilation: compilation.Compilation, chunks: Iterable<compilation.Chunk>) => {
|
||||||
const files: string[] = [...compilation.additionalChunkAssets];
|
const files: string[] = [...compilation.additionalChunkAssets];
|
||||||
|
|
||||||
chunks.forEach(chunk => {
|
for (const chunk of chunks) {
|
||||||
if (chunk.files && chunk.files.length > 0) {
|
if (!chunk.files) {
|
||||||
files.push(...chunk.files);
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const file of chunk.files) {
|
||||||
|
files.push(file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const actions = files
|
const actions = files
|
||||||
.filter(file => this._options.test(file))
|
.filter(file => this._options.test(file))
|
||||||
@ -120,7 +121,7 @@ export class OptimizeCssWebpackPlugin {
|
|||||||
compilation.assets[file] = newSource;
|
compilation.assets[file] = newSource;
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.all(actions);
|
return Promise.all(actions).then(() => {});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user