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:
parent
92ff6d93eb
commit
2c4a98a285
|
@ -22,10 +22,10 @@ export class InvalidFileSystem implements FileSystem {
|
||||||
readFile(path: AbsoluteFsPath): string {
|
readFile(path: AbsoluteFsPath): string {
|
||||||
throw makeError();
|
throw makeError();
|
||||||
}
|
}
|
||||||
readFileBuffer(path: AbsoluteFsPath): Buffer {
|
readFileBuffer(path: AbsoluteFsPath): Uint8Array {
|
||||||
throw makeError();
|
throw makeError();
|
||||||
}
|
}
|
||||||
writeFile(path: AbsoluteFsPath, data: string|Buffer, exclusive?: boolean): void {
|
writeFile(path: AbsoluteFsPath, data: string|Uint8Array, exclusive?: boolean): void {
|
||||||
throw makeError();
|
throw makeError();
|
||||||
}
|
}
|
||||||
removeFile(path: AbsoluteFsPath): void {
|
removeFile(path: AbsoluteFsPath): void {
|
||||||
|
|
|
@ -23,10 +23,10 @@ export class NodeJSFileSystem implements FileSystem {
|
||||||
readFile(path: AbsoluteFsPath): string {
|
readFile(path: AbsoluteFsPath): string {
|
||||||
return fs.readFileSync(path, 'utf8');
|
return fs.readFileSync(path, 'utf8');
|
||||||
}
|
}
|
||||||
readFileBuffer(path: AbsoluteFsPath): Buffer {
|
readFileBuffer(path: AbsoluteFsPath): Uint8Array {
|
||||||
return fs.readFileSync(path);
|
return fs.readFileSync(path);
|
||||||
}
|
}
|
||||||
writeFile(path: AbsoluteFsPath, data: string|Buffer, exclusive: boolean = false): void {
|
writeFile(path: AbsoluteFsPath, data: string|Uint8Array, exclusive: boolean = false): void {
|
||||||
fs.writeFileSync(path, data, exclusive ? {flag: 'wx'} : undefined);
|
fs.writeFileSync(path, data, exclusive ? {flag: 'wx'} : undefined);
|
||||||
}
|
}
|
||||||
removeFile(path: AbsoluteFsPath): void {
|
removeFile(path: AbsoluteFsPath): void {
|
||||||
|
|
|
@ -37,8 +37,8 @@ export type PathSegment = BrandedPath<'PathSegment'>;
|
||||||
export interface FileSystem {
|
export interface FileSystem {
|
||||||
exists(path: AbsoluteFsPath): boolean;
|
exists(path: AbsoluteFsPath): boolean;
|
||||||
readFile(path: AbsoluteFsPath): string;
|
readFile(path: AbsoluteFsPath): string;
|
||||||
readFileBuffer(path: AbsoluteFsPath): Buffer;
|
readFileBuffer(path: AbsoluteFsPath): Uint8Array;
|
||||||
writeFile(path: AbsoluteFsPath, data: string|Buffer, exclusive?: boolean): void;
|
writeFile(path: AbsoluteFsPath, data: string|Uint8Array, exclusive?: boolean): void;
|
||||||
removeFile(path: AbsoluteFsPath): void;
|
removeFile(path: AbsoluteFsPath): void;
|
||||||
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void;
|
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void;
|
||||||
readdir(path: AbsoluteFsPath): PathSegment[];
|
readdir(path: AbsoluteFsPath): PathSegment[];
|
||||||
|
|
|
@ -38,16 +38,16 @@ export abstract class MockFileSystem implements FileSystem {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readFileBuffer(path: AbsoluteFsPath): Buffer {
|
readFileBuffer(path: AbsoluteFsPath): Uint8Array {
|
||||||
const {entity} = this.findFromPath(path);
|
const {entity} = this.findFromPath(path);
|
||||||
if (isFile(entity)) {
|
if (isFile(entity)) {
|
||||||
return Buffer.isBuffer(entity) ? entity : new Buffer(entity);
|
return entity instanceof Uint8Array ? entity : new Buffer(entity);
|
||||||
} else {
|
} else {
|
||||||
throw new MockFileSystemError('ENOENT', path, `File "${path}" does not exist.`);
|
throw new MockFileSystemError('ENOENT', path, `File "${path}" does not exist.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
writeFile(path: AbsoluteFsPath, data: string|Buffer, exclusive: boolean = false): void {
|
writeFile(path: AbsoluteFsPath, data: string|Uint8Array, exclusive: boolean = false): void {
|
||||||
const [folderPath, basename] = this.splitIntoFolderAndFile(path);
|
const [folderPath, basename] = this.splitIntoFolderAndFile(path);
|
||||||
const {entity} = this.findFromPath(folderPath);
|
const {entity} = this.findFromPath(folderPath);
|
||||||
if (entity === null || !isFolder(entity)) {
|
if (entity === null || !isFolder(entity)) {
|
||||||
|
@ -295,7 +295,7 @@ export type Entity = Folder|File|SymLink;
|
||||||
export interface Folder {
|
export interface Folder {
|
||||||
[pathSegments: string]: Entity;
|
[pathSegments: string]: Entity;
|
||||||
}
|
}
|
||||||
export type File = string|Buffer;
|
export type File = string|Uint8Array;
|
||||||
export class SymLink {
|
export class SymLink {
|
||||||
constructor(public path: AbsoluteFsPath) {}
|
constructor(public path: AbsoluteFsPath) {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,14 +17,14 @@ import {TranslationBundle, TranslationHandler} from '../translator';
|
||||||
export class AssetTranslationHandler implements TranslationHandler {
|
export class AssetTranslationHandler implements TranslationHandler {
|
||||||
constructor(private fs: FileSystem) {}
|
constructor(private fs: FileSystem) {}
|
||||||
|
|
||||||
canTranslate(_relativeFilePath: PathSegment|AbsoluteFsPath, _contents: Buffer): boolean {
|
canTranslate(_relativeFilePath: PathSegment|AbsoluteFsPath, _contents: Uint8Array): boolean {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
translate(
|
translate(
|
||||||
diagnostics: Diagnostics, _sourceRoot: AbsoluteFsPath,
|
diagnostics: Diagnostics, _sourceRoot: AbsoluteFsPath,
|
||||||
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Buffer, outputPathFn: OutputPathFn,
|
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Uint8Array,
|
||||||
translations: TranslationBundle[], sourceLocale?: string): void {
|
outputPathFn: OutputPathFn, translations: TranslationBundle[], sourceLocale?: string): void {
|
||||||
for (const translation of translations) {
|
for (const translation of translations) {
|
||||||
this.writeAssetFile(
|
this.writeAssetFile(
|
||||||
diagnostics, outputPathFn, translation.locale, relativeFilePath, contents);
|
diagnostics, outputPathFn, translation.locale, relativeFilePath, contents);
|
||||||
|
@ -36,7 +36,7 @@ export class AssetTranslationHandler implements TranslationHandler {
|
||||||
|
|
||||||
private writeAssetFile(
|
private writeAssetFile(
|
||||||
diagnostics: Diagnostics, outputPathFn: OutputPathFn, locale: string,
|
diagnostics: Diagnostics, outputPathFn: OutputPathFn, locale: string,
|
||||||
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Buffer): void {
|
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Uint8Array): void {
|
||||||
try {
|
try {
|
||||||
const outputPath = absoluteFrom(outputPathFn(locale, relativeFilePath));
|
const outputPath = absoluteFrom(outputPathFn(locale, relativeFilePath));
|
||||||
this.fs.ensureDir(this.fs.dirname(outputPath));
|
this.fs.ensureDir(this.fs.dirname(outputPath));
|
||||||
|
|
|
@ -27,15 +27,15 @@ export class SourceFileTranslationHandler implements TranslationHandler {
|
||||||
TranslatePluginOptions = {...this.translationOptions, missingTranslation: 'ignore'};
|
TranslatePluginOptions = {...this.translationOptions, missingTranslation: 'ignore'};
|
||||||
constructor(private fs: FileSystem, private translationOptions: TranslatePluginOptions = {}) {}
|
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';
|
return this.fs.extname(relativeFilePath) === '.js';
|
||||||
}
|
}
|
||||||
|
|
||||||
translate(
|
translate(
|
||||||
diagnostics: Diagnostics, sourceRoot: AbsoluteFsPath, relativeFilePath: PathSegment,
|
diagnostics: Diagnostics, sourceRoot: AbsoluteFsPath, relativeFilePath: PathSegment,
|
||||||
contents: Buffer, outputPathFn: OutputPathFn, translations: TranslationBundle[],
|
contents: Uint8Array, outputPathFn: OutputPathFn, translations: TranslationBundle[],
|
||||||
sourceLocale?: string): void {
|
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
|
// A short-circuit check to avoid parsing the file into an AST if it does not contain any
|
||||||
// `$localize` identifiers.
|
// `$localize` identifiers.
|
||||||
if (!sourceCode.includes('$localize')) {
|
if (!sourceCode.includes('$localize')) {
|
||||||
|
@ -97,7 +97,7 @@ export class SourceFileTranslationHandler implements TranslationHandler {
|
||||||
|
|
||||||
private writeSourceFile(
|
private writeSourceFile(
|
||||||
diagnostics: Diagnostics, outputPathFn: OutputPathFn, locale: string,
|
diagnostics: Diagnostics, outputPathFn: OutputPathFn, locale: string,
|
||||||
relativeFilePath: PathSegment, contents: string|Buffer): void {
|
relativeFilePath: PathSegment, contents: string|Uint8Array): void {
|
||||||
try {
|
try {
|
||||||
const outputPath = absoluteFrom(outputPathFn(locale, relativeFilePath));
|
const outputPath = absoluteFrom(outputPathFn(locale, relativeFilePath));
|
||||||
this.fs.ensureDir(this.fs.dirname(outputPath));
|
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 relativeFilePath A relative path from the sourceRoot to the resource file to handle.
|
||||||
* @param contents The contents of the 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`,
|
* Translate the file at `relativeFilePath` containing `contents`, using the given `translations`,
|
||||||
|
@ -54,8 +54,8 @@ export interface TranslationHandler {
|
||||||
*/
|
*/
|
||||||
translate(
|
translate(
|
||||||
diagnostics: Diagnostics, sourceRoot: AbsoluteFsPath,
|
diagnostics: Diagnostics, sourceRoot: AbsoluteFsPath,
|
||||||
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Buffer, outputPathFn: OutputPathFn,
|
relativeFilePath: PathSegment|AbsoluteFsPath, contents: Uint8Array,
|
||||||
translations: TranslationBundle[], sourceLocale?: string): void;
|
outputPathFn: OutputPathFn, translations: TranslationBundle[], sourceLocale?: string): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -109,13 +109,13 @@ runInEachFileSystem(() => {
|
||||||
log: string[] = [];
|
log: string[] = [];
|
||||||
constructor(private _canTranslate: boolean = true) {}
|
constructor(private _canTranslate: boolean = true) {}
|
||||||
|
|
||||||
canTranslate(relativePath: string, contents: Buffer) {
|
canTranslate(relativePath: string, contents: Uint8Array) {
|
||||||
this.log.push(`canTranslate(${relativePath}, ${contents.toString('utf8')})`);
|
this.log.push(`canTranslate(${relativePath}, ${contents})`);
|
||||||
return this._canTranslate;
|
return this._canTranslate;
|
||||||
}
|
}
|
||||||
|
|
||||||
translate(
|
translate(
|
||||||
_diagnostics: Diagnostics, rootPath: string, relativePath: string, contents: Buffer,
|
_diagnostics: Diagnostics, rootPath: string, relativePath: string, contents: Uint8Array,
|
||||||
_outputPathFn: OutputPathFn, _translations: TranslationBundle[], sourceLocale?: string) {
|
_outputPathFn: OutputPathFn, _translations: TranslationBundle[], sourceLocale?: string) {
|
||||||
this.log.push(
|
this.log.push(
|
||||||
`translate(${rootPath}, ${relativePath}, ${contents}, ...` +
|
`translate(${rootPath}, ${relativePath}, ${contents}, ...` +
|
||||||
|
|
Loading…
Reference in New Issue