mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-22 15:02:11 +08:00
refactor(@angular/cli): centralize ES2015 support checks
This commit is contained in:
parent
cb53fb933c
commit
00ca690807
@ -1,5 +1,8 @@
|
|||||||
|
// @ignoreDep typescript - used only for type information
|
||||||
|
import * as ts from 'typescript';
|
||||||
import { AngularCompilerPlugin } from '@ngtools/webpack';
|
import { AngularCompilerPlugin } from '@ngtools/webpack';
|
||||||
import { readTsconfig } from '../utilities/read-tsconfig';
|
import { readTsconfig } from '../utilities/read-tsconfig';
|
||||||
|
import { requireProjectModule } from '../utilities/require-project-module';
|
||||||
const webpackMerge = require('webpack-merge');
|
const webpackMerge = require('webpack-merge');
|
||||||
import { CliConfig } from './config';
|
import { CliConfig } from './config';
|
||||||
import { BuildOptions } from './build-options';
|
import { BuildOptions } from './build-options';
|
||||||
@ -20,6 +23,7 @@ export interface WebpackConfigOptions<T extends BuildOptions = BuildOptions> {
|
|||||||
buildOptions: T;
|
buildOptions: T;
|
||||||
appConfig: any;
|
appConfig: any;
|
||||||
tsConfig: any;
|
tsConfig: any;
|
||||||
|
supportES2015: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NgCliWebpackConfig<T extends BuildOptions = BuildOptions> {
|
export class NgCliWebpackConfig<T extends BuildOptions = BuildOptions> {
|
||||||
@ -39,7 +43,12 @@ export class NgCliWebpackConfig<T extends BuildOptions = BuildOptions> {
|
|||||||
const tsconfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);
|
const tsconfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);
|
||||||
const tsConfig = readTsconfig(tsconfigPath);
|
const tsConfig = readTsconfig(tsconfigPath);
|
||||||
|
|
||||||
this.wco = { projectRoot, buildOptions, appConfig, tsConfig };
|
const projectTs = requireProjectModule(projectRoot, 'typescript') as typeof ts;
|
||||||
|
|
||||||
|
const supportES2015 = tsConfig.options.target !== projectTs.ScriptTarget.ES3
|
||||||
|
&& tsConfig.options.target !== projectTs.ScriptTarget.ES5;
|
||||||
|
|
||||||
|
this.wco = { projectRoot, buildOptions, appConfig, tsConfig, supportES2015 };
|
||||||
}
|
}
|
||||||
|
|
||||||
public buildConfig() {
|
public buildConfig() {
|
||||||
|
@ -8,14 +8,11 @@ import { packageChunkSort } from '../../utilities/package-chunk-sort';
|
|||||||
import { BaseHrefWebpackPlugin } from '../../lib/base-href-webpack';
|
import { BaseHrefWebpackPlugin } from '../../lib/base-href-webpack';
|
||||||
import { extraEntryParser, lazyChunksFilter } from './utils';
|
import { extraEntryParser, lazyChunksFilter } from './utils';
|
||||||
import { WebpackConfigOptions } from '../webpack-config';
|
import { WebpackConfigOptions } from '../webpack-config';
|
||||||
import { requireProjectModule } from '../../utilities/require-project-module';
|
|
||||||
|
|
||||||
|
|
||||||
export function getBrowserConfig(wco: WebpackConfigOptions) {
|
export function getBrowserConfig(wco: WebpackConfigOptions) {
|
||||||
const { projectRoot, buildOptions, appConfig } = wco;
|
const { projectRoot, buildOptions, appConfig } = wco;
|
||||||
|
|
||||||
const projectTs = requireProjectModule(projectRoot, 'typescript');
|
|
||||||
|
|
||||||
const appRoot = path.resolve(projectRoot, appConfig.root);
|
const appRoot = path.resolve(projectRoot, appConfig.root);
|
||||||
|
|
||||||
let extraPlugins: any[] = [];
|
let extraPlugins: any[] = [];
|
||||||
@ -80,13 +77,10 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
const supportES2015 = wco.tsConfig.options.target !== projectTs.ScriptTarget.ES3
|
|
||||||
&& wco.tsConfig.options.target !== projectTs.ScriptTarget.ES5;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
resolve: {
|
resolve: {
|
||||||
mainFields: [
|
mainFields: [
|
||||||
...(supportES2015 ? ['es2015'] : []),
|
...(wco.supportES2015 ? ['es2015'] : []),
|
||||||
'browser', 'module', 'main'
|
'browser', 'module', 'main'
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -30,8 +30,6 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
|
|||||||
const appRoot = path.resolve(projectRoot, appConfig.root);
|
const appRoot = path.resolve(projectRoot, appConfig.root);
|
||||||
const nodeModules = path.resolve(projectRoot, 'node_modules');
|
const nodeModules = path.resolve(projectRoot, 'node_modules');
|
||||||
|
|
||||||
const projectTs = requireProjectModule(projectRoot, 'typescript');
|
|
||||||
|
|
||||||
let extraPlugins: any[] = [];
|
let extraPlugins: any[] = [];
|
||||||
let extraRules: any[] = [];
|
let extraRules: any[] = [];
|
||||||
let entryPoints: { [key: string]: string[] } = {};
|
let entryPoints: { [key: string]: string[] } = {};
|
||||||
@ -159,18 +157,11 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
|
|||||||
extraPlugins.push(new NamedLazyChunksWebpackPlugin());
|
extraPlugins.push(new NamedLazyChunksWebpackPlugin());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the tsconfig to determine if we should prefer ES2015 modules.
|
|
||||||
|
|
||||||
// Load rxjs path aliases.
|
// Load rxjs path aliases.
|
||||||
// https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md#build-and-treeshaking
|
// https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md#build-and-treeshaking
|
||||||
|
|
||||||
const supportES2015 =
|
|
||||||
wco.tsConfig.options.target !== projectTs.ScriptTarget.ES3 &&
|
|
||||||
wco.tsConfig.options.target !== projectTs.ScriptTarget.ES5;
|
|
||||||
|
|
||||||
let alias = {};
|
let alias = {};
|
||||||
try {
|
try {
|
||||||
const rxjsPathMappingImport = supportES2015
|
const rxjsPathMappingImport = wco.supportES2015
|
||||||
? 'rxjs/_esm2015/path-mapping'
|
? 'rxjs/_esm2015/path-mapping'
|
||||||
: 'rxjs/_esm5/path-mapping';
|
: 'rxjs/_esm5/path-mapping';
|
||||||
const rxPaths = requireProjectModule(projectRoot, rxjsPathMappingImport);
|
const rxPaths = requireProjectModule(projectRoot, rxjsPathMappingImport);
|
||||||
|
@ -8,8 +8,6 @@ import { PurifyPlugin } from '@angular-devkit/build-optimizer';
|
|||||||
import { StaticAssetPlugin } from '../../plugins/static-asset';
|
import { StaticAssetPlugin } from '../../plugins/static-asset';
|
||||||
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
|
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
|
||||||
import { WebpackConfigOptions } from '../webpack-config';
|
import { WebpackConfigOptions } from '../webpack-config';
|
||||||
import { readTsconfig } from '../../utilities/read-tsconfig';
|
|
||||||
import { requireProjectModule } from '../../utilities/require-project-module';
|
|
||||||
|
|
||||||
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
|
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
|
||||||
|
|
||||||
@ -24,8 +22,6 @@ const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
|
|||||||
export function getProdConfig(wco: WebpackConfigOptions) {
|
export function getProdConfig(wco: WebpackConfigOptions) {
|
||||||
const { projectRoot, buildOptions, appConfig } = wco;
|
const { projectRoot, buildOptions, appConfig } = wco;
|
||||||
|
|
||||||
const projectTs = requireProjectModule(projectRoot, 'typescript');
|
|
||||||
|
|
||||||
let extraPlugins: any[] = [];
|
let extraPlugins: any[] = [];
|
||||||
let entryPoints: { [key: string]: string[] } = {};
|
let entryPoints: { [key: string]: string[] } = {};
|
||||||
|
|
||||||
@ -123,12 +119,6 @@ export function getProdConfig(wco: WebpackConfigOptions) {
|
|||||||
uglifyCompressOptions.passes = 3;
|
uglifyCompressOptions.passes = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the tsconfig to determine if we should apply ES6 uglify.
|
|
||||||
const tsconfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);
|
|
||||||
const tsConfig = readTsconfig(tsconfigPath);
|
|
||||||
const supportES2015 = tsConfig.options.target !== projectTs.ScriptTarget.ES3
|
|
||||||
&& tsConfig.options.target !== projectTs.ScriptTarget.ES5;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
entry: entryPoints,
|
entry: entryPoints,
|
||||||
plugins: [
|
plugins: [
|
||||||
@ -140,7 +130,7 @@ export function getProdConfig(wco: WebpackConfigOptions) {
|
|||||||
new UglifyJSPlugin({
|
new UglifyJSPlugin({
|
||||||
sourceMap: buildOptions.sourcemaps,
|
sourceMap: buildOptions.sourcemaps,
|
||||||
uglifyOptions: {
|
uglifyOptions: {
|
||||||
ecma: supportES2015 ? 6 : 5,
|
ecma: wco.supportES2015 ? 6 : 5,
|
||||||
warnings: buildOptions.verbose,
|
warnings: buildOptions.verbose,
|
||||||
ie8: false,
|
ie8: false,
|
||||||
mangle: true,
|
mangle: true,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { WebpackConfigOptions } from '../webpack-config';
|
import { WebpackConfigOptions } from '../webpack-config';
|
||||||
import { requireProjectModule } from '../../utilities/require-project-module';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a partial specific to creating a bundle for node
|
* Returns a partial specific to creating a bundle for node
|
||||||
@ -7,15 +6,10 @@ import { requireProjectModule } from '../../utilities/require-project-module';
|
|||||||
*/
|
*/
|
||||||
export function getServerConfig(wco: WebpackConfigOptions) {
|
export function getServerConfig(wco: WebpackConfigOptions) {
|
||||||
|
|
||||||
const projectTs = requireProjectModule(wco.projectRoot, 'typescript');
|
|
||||||
|
|
||||||
const supportES2015 = wco.tsConfig.options.target !== projectTs.ScriptTarget.ES3
|
|
||||||
&& wco.tsConfig.options.target !== projectTs.ScriptTarget.ES5;
|
|
||||||
|
|
||||||
const config: any = {
|
const config: any = {
|
||||||
resolve: {
|
resolve: {
|
||||||
mainFields: [
|
mainFields: [
|
||||||
...(supportES2015 ? ['es2015'] : []),
|
...(wco.supportES2015 ? ['es2015'] : []),
|
||||||
'main', 'module',
|
'main', 'module',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user