Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): the path to source_file_utils c…
Browse files Browse the repository at this point in the history
…hanged in v10.0.0
  • Loading branch information
petebacondarwin authored and mgechev committed Apr 29, 2020
1 parent 8f5ecd6 commit b3792de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 48 deletions.
45 changes: 2 additions & 43 deletions packages/angular_devkit/build_angular/src/dev-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ExecutionTransformer } from '../transforms';
import { BuildBrowserFeatures, normalizeOptimization } from '../utils';
import { findCachePath } from '../utils/cache-path';
import { I18nOptions } from '../utils/i18n-options';
import { createI18nPlugins } from '../utils/process-bundle';
import { assertCompatibleAngularVersion } from '../utils/version';
import { getIndexInputFile, getIndexOutputFile } from '../utils/webpack-browser-config';
import { Schema } from './schema';
Expand All @@ -54,48 +55,6 @@ const devServerBuildOverriddenKeys: (keyof DevServerBuilderOptions)[] = [
'deployUrl',
];

async function createI18nPlugins(
locale: string,
translation: unknown | undefined,
missingTranslation?: 'error' | 'warning' | 'ignore',
) {
const plugins = [];
// tslint:disable-next-line: no-implicit-dependencies
const localizeDiag = await import('@angular/localize/src/tools/src/diagnostics');

const diagnostics = new localizeDiag.Diagnostics();

const es2015 = await import(
// tslint:disable-next-line: trailing-comma no-implicit-dependencies
'@angular/localize/src/tools/src/translate/source_files/es2015_translate_plugin'
);
plugins.push(
// tslint:disable-next-line: no-any
es2015.makeEs2015TranslatePlugin(diagnostics, (translation || {}) as any, {
missingTranslation: translation === undefined ? 'ignore' : missingTranslation,
}),
);

const es5 = await import(
// tslint:disable-next-line: trailing-comma no-implicit-dependencies
'@angular/localize/src/tools/src/translate/source_files/es5_translate_plugin'
);
plugins.push(
// tslint:disable-next-line: no-any
es5.makeEs5TranslatePlugin(diagnostics, (translation || {}) as any, {
missingTranslation: translation === undefined ? 'ignore' : missingTranslation,
}),
);

const inlineLocale = await import(
// tslint:disable-next-line: trailing-comma no-implicit-dependencies
'@angular/localize/src/tools/src/translate/source_files/locale_plugin'
);
plugins.push(inlineLocale.makeLocalePlugin(locale));

return { diagnostics, plugins };
}

export type DevServerBuilderOutput = DevServerBuildOutput & {
baseUrl: string;
};
Expand Down Expand Up @@ -328,7 +287,7 @@ async function setupLocalize(
const { plugins, diagnostics } = await createI18nPlugins(
locale,
localeDescription && localeDescription.translation,
browserOptions.i18nMissingTranslation,
browserOptions.i18nMissingTranslation || 'ignore',
);

// Modify main entrypoint to include locale data
Expand Down
26 changes: 21 additions & 5 deletions packages/angular_devkit/build_angular/src/utils/process-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,11 @@ function createReplacePlugin(replacements: [string, string][]): PluginObj {

const USE_LOCALIZE_PLUGINS = false;

async function createI18nPlugins(
export async function createI18nPlugins(
locale: string,
translation: unknown | undefined,
missingTranslation: 'error' | 'warning' | 'ignore',
localeDataContent: string | undefined,
localeDataContent?: string,
) {
const plugins = [];
// tslint:disable-next-line: no-implicit-dependencies
Expand Down Expand Up @@ -693,10 +693,15 @@ async function inlineLocalesDirect(ast: ParseResult, options: InlineOptions) {
}

const { default: generate } = await import('@babel/generator');
const utils = await import(
// tslint:disable-next-line: trailing-comma no-implicit-dependencies
'@angular/localize/src/tools/src/translate/source_files/source_file_utils'

// In Angular v10.0.0 the `source_file_utils` file was moved.
// (Remember to remove the `tryImport()` function when only one import path is required.)
// tslint:disable-next-line: no-implicit-dependencies
const utils = await tryImport<typeof import('@angular/localize/src/tools/src/translate/source_files/source_file_utils')>(
'@angular/localize/src/tools/src/source_file_utils',
'@angular/localize/src/tools/src/translate/source_files/source_file_utils',
);

// tslint:disable-next-line: no-implicit-dependencies
const localizeDiag = await import('@angular/localize/src/tools/src/diagnostics');

Expand Down Expand Up @@ -778,6 +783,17 @@ async function inlineLocalesDirect(ast: ParseResult, options: InlineOptions) {
return { file: options.filename, diagnostics: diagnostics.messages, count: positions.length };
}

async function tryImport<T>(...importPaths: string[]): Promise<T> {
for (const importPath of importPaths) {
try {
return await import(importPath);
} catch {
// Do nothing
}
}
throw new Error('Unable to import from any of these paths:\n' + importPaths.map(p => ` - ${p}`).join('\n'));
}

function inlineCopyOnly(options: InlineOptions) {
if (!i18n) {
throw new Error('i18n options are missing');
Expand Down

0 comments on commit b3792de

Please sign in to comment.