Skip to content

Commit 556a3f5

Browse files
alan-agius4filipesilva
authored andcommitted
refactor(@angular-devkit/build-angular): remove ESM workarounds
1 parent a0c29bf commit 556a3f5

File tree

6 files changed

+21
-57
lines changed

6 files changed

+21
-57
lines changed

packages/angular_devkit/build_angular/src/babel/presets/application.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import type { ɵParsedTranslation } from '@angular/localize/private';
910
import type {
1011
DiagnosticHandlingStrategy,
1112
Diagnostics,
@@ -35,9 +36,9 @@ export interface ApplicationPresetOptions {
3536
i18n?: {
3637
locale: string;
3738
missingTranslationBehavior?: 'error' | 'warning' | 'ignore';
38-
translation?: unknown;
39+
translation?: Record<string, ɵParsedTranslation>;
3940
translationFiles?: string[];
40-
pluginCreators?: I18nPluginCreators;
41+
pluginCreators: I18nPluginCreators;
4142
};
4243

4344
angularLinker?: {
@@ -107,47 +108,30 @@ function createI18nDiagnostics(reporter: DiagnosticReporter | undefined): Diagno
107108

108109
function createI18nPlugins(
109110
locale: string,
110-
translation: unknown | undefined,
111+
translation: Record<string, ɵParsedTranslation> | undefined,
111112
missingTranslationBehavior: 'error' | 'warning' | 'ignore',
112113
diagnosticReporter: DiagnosticReporter | undefined,
113-
// TODO_ESM: Make `pluginCreators` required once `@angular/localize` is published with the `tools` entry point
114-
pluginCreators: I18nPluginCreators | undefined,
114+
pluginCreators: I18nPluginCreators,
115115
) {
116116
const diagnostics = createI18nDiagnostics(diagnosticReporter);
117117
const plugins = [];
118118

119+
const { makeEs5TranslatePlugin, makeEs2015TranslatePlugin, makeLocalePlugin } = pluginCreators;
120+
119121
if (translation) {
120-
const {
121-
makeEs2015TranslatePlugin,
122-
// TODO_ESM: Remove all deep imports once `@angular/localize` is published with the `tools` entry point
123-
} =
124-
pluginCreators ??
125-
require('@angular/localize/src/tools/src/translate/source_files/es2015_translate_plugin');
126122
plugins.push(
127123
makeEs2015TranslatePlugin(diagnostics, translation, {
128124
missingTranslation: missingTranslationBehavior,
129125
}),
130126
);
131127

132-
const {
133-
makeEs5TranslatePlugin,
134-
// TODO_ESM: Remove all deep imports once `@angular/localize` is published with the `tools` entry point
135-
} =
136-
pluginCreators ??
137-
require('@angular/localize/src/tools/src/translate/source_files/es5_translate_plugin');
138128
plugins.push(
139129
makeEs5TranslatePlugin(diagnostics, translation, {
140130
missingTranslation: missingTranslationBehavior,
141131
}),
142132
);
143133
}
144134

145-
const {
146-
makeLocalePlugin,
147-
// TODO_ESM: Remove all deep imports once `@angular/localize` is published with the `tools` entry point
148-
} =
149-
pluginCreators ??
150-
require('@angular/localize/src/tools/src/translate/source_files/locale_plugin');
151135
plugins.push(makeLocalePlugin(locale));
152136

153137
return plugins;

packages/angular_devkit/build_angular/src/babel/webpack-loader.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,14 @@ export default custom<ApplicationPresetOptions>(() => {
9191

9292
// Analyze file for linking
9393
if (await requiresLinking(this.resourcePath, source)) {
94-
if (!linkerPluginCreator) {
95-
// Load ESM `@angular/compiler-cli/linker/babel` using the TypeScript dynamic import workaround.
96-
// Once TypeScript provides support for keeping the dynamic import this workaround can be
97-
// changed to a direct dynamic import.
98-
const linkerBabelModule = await loadEsmModule<
99-
typeof import('@angular/compiler-cli/linker/babel')
100-
>('@angular/compiler-cli/linker/babel');
101-
linkerPluginCreator = linkerBabelModule.createEs2015LinkerPlugin;
102-
}
94+
// Load ESM `@angular/compiler-cli/linker/babel` using the TypeScript dynamic import workaround.
95+
// Once TypeScript provides support for keeping the dynamic import this workaround can be
96+
// changed to a direct dynamic import.
97+
linkerPluginCreator ??= (
98+
await loadEsmModule<typeof import('@angular/compiler-cli/linker/babel')>(
99+
'@angular/compiler-cli/linker/babel',
100+
)
101+
).createEs2015LinkerPlugin;
103102

104103
customOptions.angularLinker = {
105104
shouldLink: true,
@@ -136,14 +135,11 @@ export default custom<ApplicationPresetOptions>(() => {
136135
// During the transition, this will always attempt to load the entry point for each file.
137136
// This will only occur during prerelease and will be automatically corrected once the new
138137
// entry point exists.
139-
// TODO_ESM: Make import failure an error once the `tools` entry point exists.
140138
if (i18nPluginCreators === undefined) {
141139
// Load ESM `@angular/localize/tools` using the TypeScript dynamic import workaround.
142140
// Once TypeScript provides support for keeping the dynamic import this workaround can be
143141
// changed to a direct dynamic import.
144-
try {
145-
i18nPluginCreators = await loadEsmModule<I18nPluginCreators>('@angular/localize/tools');
146-
} catch {}
142+
i18nPluginCreators = await loadEsmModule<I18nPluginCreators>('@angular/localize/tools');
147143
}
148144

149145
customOptions.i18n = {

packages/angular_devkit/build_angular/src/builders/extract-i18n/ivy-extract-loader.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ async function extract(
4949
// Try to load the `@angular/localize` message extractor.
5050
// All the localize usages are setup to first try the ESM entry point then fallback to the deep imports.
5151
// This provides interim compatibility while the framework is transitioned to bundled ESM packages.
52-
// TODO_ESM: Remove all deep imports once `@angular/localize` is published with the `tools` entry point
5352
let MessageExtractor;
5453
try {
5554
// Load ESM `@angular/localize/tools` using the TypeScript dynamic import workaround.

packages/angular_devkit/build_angular/src/utils/i18n-options.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ export async function configureI18nBuild<T extends BrowserBuilderSchema | Server
217217
continue;
218218
}
219219

220-
if (!loader) {
221-
loader = await createTranslationLoader();
222-
}
220+
loader ??= await createTranslationLoader();
223221

224222
loadTranslations(
225223
locale,

packages/angular_devkit/build_angular/src/utils/read-tsconfig.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,9 @@ export async function readTsconfig(
2626
// Load ESM `@angular/compiler-cli` using the TypeScript dynamic import workaround.
2727
// Once TypeScript provides support for keeping the dynamic import this workaround can be
2828
// changed to a direct dynamic import.
29-
const compilerCliModule = await loadEsmModule<{ readConfiguration: unknown; default: unknown }>(
30-
'@angular/compiler-cli',
31-
);
32-
// If it is not ESM then the functions needed will be stored in the `default` property.
33-
// TODO_ESM: This can be removed once `@angular/compiler-cli` is ESM only.
34-
const { formatDiagnostics, readConfiguration } = (
35-
compilerCliModule.readConfiguration ? compilerCliModule : compilerCliModule.default
36-
) as typeof import('@angular/compiler-cli');
29+
const { formatDiagnostics, readConfiguration } = await loadEsmModule<
30+
typeof import('@angular/compiler-cli')
31+
>('@angular/compiler-cli');
3732

3833
const configResult = readConfiguration(tsConfigFullPath);
3934
if (configResult.errors && configResult.errors.length) {

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,11 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
8181
// Load ESM `@angular/compiler-cli` using the TypeScript dynamic import workaround.
8282
// Once TypeScript provides support for keeping the dynamic import this workaround can be
8383
// changed to a direct dynamic import.
84-
const compilerCliModule = await loadEsmModule<{
85-
GLOBAL_DEFS_FOR_TERSER: unknown;
86-
default: unknown;
87-
}>('@angular/compiler-cli');
88-
// If it is not ESM then the values needed will be stored in the `default` property.
89-
// TODO_ESM: This can be removed once `@angular/compiler-cli` is ESM only.
9084
const {
9185
GLOBAL_DEFS_FOR_TERSER,
9286
GLOBAL_DEFS_FOR_TERSER_WITH_AOT,
9387
VERSION: NG_VERSION,
94-
} = (
95-
compilerCliModule.GLOBAL_DEFS_FOR_TERSER ? compilerCliModule : compilerCliModule.default
96-
) as typeof import('@angular/compiler-cli');
88+
} = await loadEsmModule<typeof import('@angular/compiler-cli')>('@angular/compiler-cli');
9789

9890
// determine hashing format
9991
const hashFormat = getOutputHashFormat(buildOptions.outputHashing || 'none');

0 commit comments

Comments
 (0)