Skip to content

Commit

Permalink
fix: copy loadEsmModule as not present in older angular versions (and…
Browse files Browse the repository at this point in the history
… internal anyway)
  • Loading branch information
daniel-sc committed Feb 7, 2024
1 parent c58cfd9 commit 4f56bbd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion schematics/migrations/2_0_0/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down
2 changes: 1 addition & 1 deletion src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
26 changes: 26 additions & 0 deletions src/loadEsmModule.ts
Original file line number Diff line number Diff line change
@@ -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<T>(modulePath: string|URL): Promise<T> {
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;
}
}

0 comments on commit 4f56bbd

Please sign in to comment.