diff --git a/schematics/migrations/2_0_0/index.ts b/schematics/migrations/2_0_0/index.ts index 4068427..c880cf8 100644 --- a/schematics/migrations/2_0_0/index.ts +++ b/schematics/migrations/2_0_0/index.ts @@ -1,7 +1,7 @@ import {Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics'; import {updateWorkspace} from '@schematics/angular/utility/workspace'; -import {loadEsmModule} from '@angular-devkit/architect/node'; import {Version} from '@angular/core'; +import {loadEsmModule} from '../../../src/loadEsmModule'; function updateNpmScript(tree: Tree, logger: SchematicContext['logger']) { const pkgPath = '/package.json'; diff --git a/schematics/ng-add/index.ts b/schematics/ng-add/index.ts index 19636ef..f2db6c1 100644 --- a/schematics/ng-add/index.ts +++ b/schematics/ng-add/index.ts @@ -4,7 +4,7 @@ import {Schema} from './schema'; import {JsonArray, JsonObject, normalize, Path, relative} from '@angular-devkit/core'; import {Options} from '../../src/options'; -import {loadEsmModule} from '@angular-devkit/architect/node'; +import {loadEsmModule} from '../../src/loadEsmModule'; import {Version} from '@angular/core'; function getTargetFiles(i18nExtension: JsonObject | undefined): string[] { diff --git a/src/builder.ts b/src/builder.ts index 90e400c..ce1cd50 100644 --- a/src/builder.ts +++ b/src/builder.ts @@ -8,8 +8,8 @@ import {TranslationFile, TranslationUnit} from './model/translationFileModels'; import {Merger} from './merger'; import {Options} from './options'; import {doCollapseWhitespace} from './stringUtils'; -import {loadEsmModule} from '@angular-devkit/architect/node'; import {Version} from '@angular/core'; +import {loadEsmModule} from './loadEsmModule'; const STATE_INITIAL_XLF_2_0 = 'initial'; diff --git a/src/loadEsmModule.ts b/src/loadEsmModule.ts new file mode 100644 index 0000000..e2b3f8f --- /dev/null +++ b/src/loadEsmModule.ts @@ -0,0 +1,26 @@ +/** + * This uses a dynamic import to load a module which may be ESM. + * CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript + * will currently, unconditionally downlevel dynamic import into a require call. + * require calls cannot load ESM code and will result in a runtime error. To workaround + * this, a Function constructor is used to prevent TypeScript from changing the dynamic import. + * Once TypeScript provides support for keeping the dynamic import this workaround can + * be dropped. + * This is only intended to be used with Angular framework packages. + * + * Copied from https://github.com/angular/angular/blob/e0015d3c456d584242269b0765878d598a550888/packages/core/schematics/utils/load_esm.ts#L11-L35 + * + * @param modulePath The path of the module to load. + * @returns A Promise that resolves to the dynamically imported module. + */ +export async function loadEsmModule(modulePath: string|URL): Promise { + const namespaceObject = + (await new Function('modulePath', `return import(modulePath);`)(modulePath)); + + // If it is not ESM then the values needed will be stored in the `default` property. + if (namespaceObject.default) { + return namespaceObject.default; + } else { + return namespaceObject; + } +} \ No newline at end of file