fix(localize): do not expose NodeJS typings in $localize runtime code (#38700)
A recent change to `@angular/localize` brought in the `AbsoluteFsPath` type from the `@angular/compiler-cli`. But this brought along with it a reference to NodeJS typings - specifically the `FileSystem` interface refers to the `Buffer` type from NodeJS. This affects compilation of `@angular/localize` code that will be run in the browser - for example projects that reference `loadTranslations()`. The compilation breaks if the NodeJS typings are not included in the build. Clearly it is not desirable to have these typings included when the project is not targeting NodeJS. This commit replaces references to the NodeJS `Buffer` type with `Uint8Array`, which is available across all platforms and is actually the super-class of `Buffer`. Fixes #38692 PR Close #38700
This commit is contained in:

committed by
atscott

parent
92ff6d93eb
commit
2c4a98a285
@ -17,14 +17,14 @@ import {TranslationBundle, TranslationHandler} from '../translator';
|
||||
export class AssetTranslationHandler implements TranslationHandler {
|
||||
constructor(private fs: FileSystem) {}
|
||||
|
||||
canTranslate(_relativeFilePath: PathSegment|AbsoluteFsPath, _contents: Buffer): boolean {
|
||||
canTranslate(_relativeFilePath: PathSegment|AbsoluteFsPath, _contents: Uint8Array): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
translate(
|
||||
diagnostics: Diagnostics, _sourceRoot: AbsoluteFsPath,
|
||||
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Buffer, outputPathFn: OutputPathFn,
|
||||
translations: TranslationBundle[], sourceLocale?: string): void {
|
||||
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Uint8Array,
|
||||
outputPathFn: OutputPathFn, translations: TranslationBundle[], sourceLocale?: string): void {
|
||||
for (const translation of translations) {
|
||||
this.writeAssetFile(
|
||||
diagnostics, outputPathFn, translation.locale, relativeFilePath, contents);
|
||||
@ -36,7 +36,7 @@ export class AssetTranslationHandler implements TranslationHandler {
|
||||
|
||||
private writeAssetFile(
|
||||
diagnostics: Diagnostics, outputPathFn: OutputPathFn, locale: string,
|
||||
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Buffer): void {
|
||||
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Uint8Array): void {
|
||||
try {
|
||||
const outputPath = absoluteFrom(outputPathFn(locale, relativeFilePath));
|
||||
this.fs.ensureDir(this.fs.dirname(outputPath));
|
||||
|
@ -27,15 +27,15 @@ export class SourceFileTranslationHandler implements TranslationHandler {
|
||||
TranslatePluginOptions = {...this.translationOptions, missingTranslation: 'ignore'};
|
||||
constructor(private fs: FileSystem, private translationOptions: TranslatePluginOptions = {}) {}
|
||||
|
||||
canTranslate(relativeFilePath: PathSegment|AbsoluteFsPath, _contents: Buffer): boolean {
|
||||
canTranslate(relativeFilePath: PathSegment|AbsoluteFsPath, _contents: Uint8Array): boolean {
|
||||
return this.fs.extname(relativeFilePath) === '.js';
|
||||
}
|
||||
|
||||
translate(
|
||||
diagnostics: Diagnostics, sourceRoot: AbsoluteFsPath, relativeFilePath: PathSegment,
|
||||
contents: Buffer, outputPathFn: OutputPathFn, translations: TranslationBundle[],
|
||||
contents: Uint8Array, outputPathFn: OutputPathFn, translations: TranslationBundle[],
|
||||
sourceLocale?: string): void {
|
||||
const sourceCode = contents.toString('utf8');
|
||||
const sourceCode = Buffer.from(contents).toString('utf8');
|
||||
// A short-circuit check to avoid parsing the file into an AST if it does not contain any
|
||||
// `$localize` identifiers.
|
||||
if (!sourceCode.includes('$localize')) {
|
||||
@ -97,7 +97,7 @@ export class SourceFileTranslationHandler implements TranslationHandler {
|
||||
|
||||
private writeSourceFile(
|
||||
diagnostics: Diagnostics, outputPathFn: OutputPathFn, locale: string,
|
||||
relativeFilePath: PathSegment, contents: string|Buffer): void {
|
||||
relativeFilePath: PathSegment, contents: string|Uint8Array): void {
|
||||
try {
|
||||
const outputPath = absoluteFrom(outputPathFn(locale, relativeFilePath));
|
||||
this.fs.ensureDir(this.fs.dirname(outputPath));
|
||||
|
@ -35,7 +35,7 @@ export interface TranslationHandler {
|
||||
* @param relativeFilePath A relative path from the sourceRoot to the resource file to handle.
|
||||
* @param contents The contents of the file to handle.
|
||||
*/
|
||||
canTranslate(relativeFilePath: PathSegment|AbsoluteFsPath, contents: Buffer): boolean;
|
||||
canTranslate(relativeFilePath: PathSegment|AbsoluteFsPath, contents: Uint8Array): boolean;
|
||||
|
||||
/**
|
||||
* Translate the file at `relativeFilePath` containing `contents`, using the given `translations`,
|
||||
@ -54,8 +54,8 @@ export interface TranslationHandler {
|
||||
*/
|
||||
translate(
|
||||
diagnostics: Diagnostics, sourceRoot: AbsoluteFsPath,
|
||||
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Buffer, outputPathFn: OutputPathFn,
|
||||
translations: TranslationBundle[], sourceLocale?: string): void;
|
||||
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Uint8Array,
|
||||
outputPathFn: OutputPathFn, translations: TranslationBundle[], sourceLocale?: string): void;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,13 +109,13 @@ runInEachFileSystem(() => {
|
||||
log: string[] = [];
|
||||
constructor(private _canTranslate: boolean = true) {}
|
||||
|
||||
canTranslate(relativePath: string, contents: Buffer) {
|
||||
this.log.push(`canTranslate(${relativePath}, ${contents.toString('utf8')})`);
|
||||
canTranslate(relativePath: string, contents: Uint8Array) {
|
||||
this.log.push(`canTranslate(${relativePath}, ${contents})`);
|
||||
return this._canTranslate;
|
||||
}
|
||||
|
||||
translate(
|
||||
_diagnostics: Diagnostics, rootPath: string, relativePath: string, contents: Buffer,
|
||||
_diagnostics: Diagnostics, rootPath: string, relativePath: string, contents: Uint8Array,
|
||||
_outputPathFn: OutputPathFn, _translations: TranslationBundle[], sourceLocale?: string) {
|
||||
this.log.push(
|
||||
`translate(${rootPath}, ${relativePath}, ${contents}, ...` +
|
||||
|
Reference in New Issue
Block a user