Skip to content

Commit

Permalink
refactor(@angular-devkit/build-angular): use script target in custom …
Browse files Browse the repository at this point in the history
…babel loader

This change uses the project's TypeScript configuration script target to determine the required Babel processing.
  • Loading branch information
clydin authored and alan-agius4 committed Jan 11, 2021
1 parent 507be0d commit 4f352ea
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
19 changes: 16 additions & 3 deletions packages/angular_devkit/build_angular/src/babel/webpack-loader.ts
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import { custom } from 'babel-loader';
import { ScriptTarget } from 'typescript';

interface AngularCustomOptions {
forceES5: boolean;
Expand Down Expand Up @@ -62,9 +63,11 @@ export default custom<AngularCustomOptions>(() => {
});

return {
async customOptions({ forceES5, ...loaderOptions }, { source }) {
let shouldProcess = forceES5;
async customOptions({ scriptTarget, ...loaderOptions }, { source }) {
// Must process file if plugins are added
let shouldProcess = Array.isArray(loaderOptions.plugins) && loaderOptions.plugins.length > 0;

// Analyze file for linking
let shouldLink = false;
const { hasLinkerSupport, requiresLinking } = await checkLinking(this.resourcePath, source);
if (requiresLinking && !hasLinkerSupport) {
Expand All @@ -77,17 +80,27 @@ export default custom<AngularCustomOptions>(() => {
}
shouldProcess ||= shouldLink;

// Analyze for ES target processing
let forceES5 = false;
const esTarget = scriptTarget as ScriptTarget;
if (esTarget < ScriptTarget.ES2015) {
forceES5 = true;
}
shouldProcess ||= forceES5;

// Add provided loader options to default base options
const options: Record<string, unknown> = {
...baseOptions,
...loaderOptions,
};

// Skip babel processing if no actions are needed
if (!shouldProcess) {
// Force the current file to be ignored
options.ignore = [() => true];
}

return { custom: { forceES5: !!forceES5, shouldLink }, loader: options };
return { custom: { forceES5, shouldLink }, loader: options };
},
config(configuration, { customOptions }) {
return {
Expand Down
Expand Up @@ -16,7 +16,6 @@ import {
I18NMissingTranslation,
IndexUnion,
Localize,
OptimizationClass,
SourceMapClass,
} from '../browser/schema';
import { Schema as DevServerSchema } from '../dev-server/schema';
Expand Down Expand Up @@ -98,5 +97,5 @@ export interface WebpackConfigOptions<T = BuildOptions> {
buildOptions: T;
tsConfig: ParsedConfiguration;
tsConfigPath: string;
supportES2015: boolean;
scriptTarget: import('typescript').ScriptTarget;
}
Expand Up @@ -77,7 +77,7 @@ export async function generateWebpackConfig(
buildOptions,
tsConfig,
tsConfigPath,
supportES2015,
scriptTarget,
};

wco.buildOptions.progress = defaultProgress(wco.buildOptions.progress);
Expand Down
Expand Up @@ -554,7 +554,7 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
cacheIdentifier: JSON.stringify({
buildAngular: require('../../../package.json').version,
}),
forceES5: !wco.supportES2015,
scriptTarget: wco.scriptTarget,
},
},
...buildOptimizerUseRule,
Expand Down

0 comments on commit 4f352ea

Please sign in to comment.