Skip to content

Commit

Permalink
fix: use dynamic import to prevent error on ng add
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sc committed Jan 26, 2024
1 parent ac0a07f commit 2357c0e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions schematics/migrations/2_0_0/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
import {updateWorkspace} from '@schematics/angular/utility/workspace';
import {VERSION} from '@angular/core';

function updateNpmScript(tree: Tree, logger: SchematicContext['logger']) {
const pkgPath = '/package.json';
Expand All @@ -24,7 +23,8 @@ function updateNpmScript(tree: Tree, logger: SchematicContext['logger']) {
export default function (): Rule {
return (tree: Tree, context: SchematicContext) => {
updateNpmScript(tree, context.logger);
return updateWorkspace((workspace) => {
return updateWorkspace(async (workspace) => {
const {VERSION} = await import('@angular/core');
workspace.projects.forEach((project, projectName) => {
const i18nMergeTarget = project.targets.get('extract-i18n-merge');
if (i18nMergeTarget) {
Expand Down
4 changes: 2 additions & 2 deletions schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Schema} from './schema';
import {JsonArray, JsonObject, normalize, Path, relative} from '@angular-devkit/core';

import {Options} from '../../src/options';
import {VERSION} from '@angular/core';

function getTargetFiles(i18nExtension: JsonObject | undefined): string[] {
const locales = i18nExtension?.locales ? (Object.values(i18nExtension?.locales) as JsonArray | string[] | undefined) : undefined;
Expand Down Expand Up @@ -58,7 +57,7 @@ function getOutFileRelativeToOutputPath(outFile: string, outputPathFromExtractI1
// noinspection JSUnusedGlobalSymbols
export function ngAdd(_options: Schema): Rule {
return (tree: Tree, context: SchematicContext) => {
return updateWorkspace((workspace) => {
return updateWorkspace(async (workspace) => {
const projectName = _options.project || Array.from(workspace.projects.keys())[0];
const projectWorkspace = workspace.projects.get(projectName)!;
if (!projectWorkspace) {
Expand Down Expand Up @@ -94,6 +93,7 @@ export function ngAdd(_options: Schema): Rule {
const filesWithoutOutputPath = files?.map(f => relative(`/${outputPath}` as Path, `/${f}` as Path));

const target = projectWorkspace.targets.get('extract-i18n');
const {VERSION} = await import('@angular/core');
const angularMajorVersion = parseInt(VERSION.major);
const buildTargetAttribute = angularMajorVersion >= 17 ? 'buildTarget' : 'browserTarget';
const builderOptions: Partial<Options> = {
Expand Down
2 changes: 1 addition & 1 deletion src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {TranslationFile, TranslationUnit} from './model/translationFileModels';
import {Merger} from './merger';
import {Options} from './options';
import {doCollapseWhitespace} from './stringUtils';
import {VERSION} from '@angular/core';


const STATE_INITIAL_XLF_2_0 = 'initial';
Expand Down Expand Up @@ -107,6 +106,7 @@ async function extractI18nMergeBuilder(options: Options, context: BuilderContext
const sourcePath = join(normalize(outputPath), options.sourceFile ?? 'messages.xlf');
const translationSourceFileOriginal = fromXlf(await readFileIfExists(sourcePath));

const {VERSION} = await import('@angular/core');
const angularMajorVersion = parseInt(VERSION.major);
const buildTargetAttribute = angularMajorVersion >= 17 ? 'buildTarget' : 'browserTarget';
const extractI18nRun = await context.scheduleBuilder(options.builderI18n ?? '@angular-devkit/build-angular:extract-i18n', {
Expand Down

0 comments on commit 2357c0e

Please sign in to comment.