Skip to content

Commit

Permalink
fix: dynamic import() for ES Module
Browse files Browse the repository at this point in the history
  • Loading branch information
boeckMt committed Nov 30, 2023
1 parent 270b0d8 commit 919ac6e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions projects/core-ui/schematics/html-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const RewritingStream = require('parse5-html-rewriting-stream');
import { Rule, SchematicContext, SchematicsException } from '@angular-devkit/schematics';
import { Rule, SchematicContext, SchematicsException, } from '@angular-devkit/schematics';
import { Tree } from '@angular-devkit/schematics/src/tree/interface';
import { Readable, Writable } from 'stream';

export function loadEsmModule<T>(modulePath: string | URL): Promise<T> {
return new Function('modulePath', `return import(modulePath);`)(modulePath) as Promise<T>;
}

interface Iparse5Tag {
tagName: string;
attrs: [];
Expand All @@ -22,13 +25,17 @@ interface Iparse5Tag {
* https://github.com/angular/angular-cli/blob/aedfcc1862afc599ea18c578248d0aa373a947bb/packages/angular_devkit/build_angular/src/utils/index-file/html-rewriting-stream.ts#L11
*/
export function updateHtmlFile(path: string, startTagStr: string, endTagStr: string, items: string | string[]): Rule {
return (tree: Tree, context: SchematicContext) => {
return async (tree: Tree, context: SchematicContext) => {

const buffer = tree.read(path);
if (buffer === null) {
throw new SchematicsException(`Could not read index file: ${path}`);
}

const { RewritingStream } = await loadEsmModule<typeof import('parse5-html-rewriting-stream')>(
'parse5-html-rewriting-stream',
);

const rewriter = new RewritingStream();
const startTags: Iparse5Tag[] = [];
rewriter.on('startTag', (startTag: Iparse5Tag) => {
Expand Down

0 comments on commit 919ac6e

Please sign in to comment.