Skip to content

Commit b8bbe96

Browse files
crisbetodgp1130
authored andcommitted
fix(@angular/cli): error if Angular compiler is used in a schematic
When a schematic is executed, it is wrapped in a custom Node context. This context doesn't expose the same set of global variables. This can lead to an error if a schematic is importing the Angular compiler and the app is using i18n, because the `TextEncoder` isn't exposed through the custom context (see angular/angular#48940). These changes add the `TextEncoder` to the context. Fixes angular/angular#48940. (cherry picked from commit 234f265)
1 parent d80adde commit b8bbe96

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

packages/angular/cli/src/command-builder/utilities/schematic-engine-host.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { readFileSync } from 'fs';
1212
import { parse as parseJson } from 'jsonc-parser';
1313
import { createRequire } from 'module';
1414
import { dirname, resolve } from 'path';
15+
import { TextEncoder } from 'util';
1516
import { Script } from 'vm';
1617
import { assertIsError } from '../../utilities/error';
1718

@@ -211,6 +212,12 @@ function wrap(
211212
__dirname: schematicDirectory,
212213
__filename: schematicFile,
213214
Buffer,
215+
// TextEncoder is used by the compiler to generate i18n message IDs. See:
216+
// https://github.com/angular/angular/blob/main/packages/compiler/src/i18n/digest.ts#L17
217+
// It is referenced globally, because it may be run either on the browser or the server.
218+
// Usually Node exposes it globally, but in order for it to work, our custom context
219+
// has to expose it too. Issue context: https://github.com/angular/angular/issues/48940.
220+
TextEncoder,
214221
console,
215222
process,
216223
get global() {

0 commit comments

Comments
 (0)