refactor: update compiler-cli to work with ESM (#48521)

Updates compiler-cli & tests to be full ESM compatible. Tests
no longer with CommonJS.

PR Close #48521
This commit is contained in:
Paul Gschwendtner 2022-12-13 17:33:35 +00:00
parent 619f0900a2
commit decae5b8e9
8 changed files with 26 additions and 23 deletions

View file

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
/// <reference types="node" />
import * as fs from 'fs';
import fs from 'fs';
import module from 'module';
import * as p from 'path';
import {fileURLToPath} from 'url';

View file

@ -9,7 +9,8 @@
// named exports being modified if we apply jasmine spies on `realFs`. Using
// the default export gives us an object where we can patch properties on.
import realFs from 'fs';
import * as os from 'os';
import os from 'os';
import url from 'url';
import {NodeJSFileSystem, NodeJSPathManipulation, NodeJSReadonlyFileSystem} from '../src/node_js_file_system';
import {AbsoluteFsPath, PathSegment} from '../src/types';
@ -57,7 +58,8 @@ describe('NodeJSReadonlyFileSystem', () => {
describe('isCaseSensitive()', () => {
it('should return true if the FS is case-sensitive', () => {
const isCaseSensitive = !realFs.existsSync(__filename.toUpperCase());
const currentFilename = url.fileURLToPath(import.meta.url);
const isCaseSensitive = !realFs.existsSync(currentFilename.toUpperCase());
expect(fs.isCaseSensitive()).toEqual(isCaseSensitive);
});
});

View file

@ -15,6 +15,7 @@ ts_library(
"//packages/compiler-cli/src/ngtsc/file_system/testing",
"//packages/compiler-cli/src/ngtsc/reflection",
"//packages/compiler-cli/src/ngtsc/util",
"@npm//@bazel/runfiles",
"@npm//typescript",
],
)

View file

@ -13,7 +13,7 @@ import {resolve} from 'path';
import {AbsoluteFsPath, FileSystem, getFileSystem} from '../../file_system';
import {Folder, MockFileSystemPosix, TestFile} from '../../file_system/testing';
import {getAngularPackagesFromRunfiles, resolveNpmTreeArtifact} from './runfile_helpers';
import {getAngularPackagesFromRunfiles, resolveFromRunfiles} from './runfile_helpers';
export function loadTestFiles(files: TestFile[]) {
const fs = getFileSystem();
@ -39,9 +39,10 @@ class CachedFolder {
}
}
const typescriptFolder = new CachedFolder(() => loadFolder(resolveNpmTreeArtifact('typescript')));
const typescriptFolder =
new CachedFolder(() => loadFolder(resolveFromRunfiles('npm/node_modules/typescript')));
const angularFolder = new CachedFolder(loadAngularFolder);
const rxjsFolder = new CachedFolder(() => loadFolder(resolveNpmTreeArtifact('rxjs')));
const rxjsFolder = new CachedFolder(() => loadFolder(resolveFromRunfiles('npm/node_modules/rxjs')));
export function loadStandardTestFiles(
{fakeCore = true, fakeCommon = false, rxjs = false}:
@ -72,21 +73,21 @@ export function loadStandardTestFiles(
export function loadTsLib(fs: FileSystem, basePath: string = '/') {
loadTestDirectory(
fs, resolveNpmTreeArtifact('tslib'), fs.resolve(basePath, 'node_modules/tslib'));
fs, resolveFromRunfiles('npm/node_modules/tslib'),
fs.resolve(basePath, 'node_modules/tslib'));
}
export function loadFakeCore(fs: FileSystem, basePath: string = '/') {
loadTestDirectory(
fs,
resolveNpmTreeArtifact(
'angular/packages/compiler-cli/src/ngtsc/testing/fake_core/npm_package'),
resolveFromRunfiles('angular/packages/compiler-cli/src/ngtsc/testing/fake_core/npm_package'),
fs.resolve(basePath, 'node_modules/@angular/core'));
}
export function loadFakeCommon(fs: FileSystem, basePath: string = '/') {
loadTestDirectory(
fs,
resolveNpmTreeArtifact(
resolveFromRunfiles(
'angular/packages/compiler-cli/src/ngtsc/testing/fake_common/npm_package'),
fs.resolve(basePath, 'node_modules/@angular/common'));
}

View file

@ -7,6 +7,7 @@
*/
/// <reference types="node" />
import {runfiles} from '@bazel/runfiles';
import * as fs from 'fs';
import * as path from 'path';
@ -39,11 +40,7 @@ export function getAngularPackagesFromRunfiles() {
}));
}
/**
* Resolves a NPM package from the Bazel runfiles. We need to resolve the Bazel tree
* artifacts using a "resolve file" because the NodeJS module resolution does not allow
* resolving to directory paths.
*/
export function resolveNpmTreeArtifact(manifestPath: string, resolveFile = 'package.json') {
return path.dirname(require.resolve(path.posix.join(manifestPath, resolveFile)));
/** Resolves a file or directory from the Bazel runfiles. */
export function resolveFromRunfiles(manifestPath: string) {
return runfiles.resolve(manifestPath);
}

View file

@ -5,9 +5,10 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import babel, {PluginObj} from '@babel/core';
import {needsLinking} from '../../../linker';
import {createEs2015LinkerPlugin} from '../../../linker/babel';
import {PluginObj, transformSync} from '../../../linker/babel/src/babel_core';
import {AbsoluteFsPath, FileSystem} from '../../../src/ngtsc/file_system';
import {ConsoleLogger, LogLevel} from '../../../src/ngtsc/logging';
import {MapAndPath, RawSourceMap, SourceFileLoader} from '../../../src/ngtsc/sourcemaps';
@ -88,7 +89,7 @@ function applyLinker(
if (!filename.endsWith('.js') || !needsLinking(filename, source)) {
return {linkedSource: source, linkedSourceMap: sourceMap};
}
const result = transformSync(source, {
const result = babel.transformSync(source, {
cwd,
filename,
sourceMaps: !!sourceMap,

View file

@ -17,7 +17,8 @@ def partial_compliance_golden(filePath):
nodejs_binary(
name = generate_partial_name,
testonly = True,
data = data + [filePath],
data = data,
data_for_args = [filePath],
visibility = [":__pkg__"],
entry_point = "//packages/compiler-cli/test/compliance/partial:cli.ts",
templated_args = ["$(execpath %s)" % filePath],

View file

@ -12,7 +12,7 @@ import ts from 'typescript';
import * as ng from '../index';
import {NodeJSFileSystem, setFileSystem} from '../src/ngtsc/file_system';
import {getAngularPackagesFromRunfiles, resolveNpmTreeArtifact} from '../src/ngtsc/testing';
import {getAngularPackagesFromRunfiles, resolveFromRunfiles} from '../src/ngtsc/testing';
// TEST_TMPDIR is always set by Bazel.
const tmpdir = process.env.TEST_TMPDIR!;
@ -132,14 +132,14 @@ export function setupBazelTo(tmpDirPath: string) {
});
// Link typescript
const typeScriptSource = resolveNpmTreeArtifact('npm/node_modules/typescript');
const typeScriptSource = resolveFromRunfiles('npm/node_modules/typescript');
const typescriptDest = path.join(nodeModulesPath, 'typescript');
fs.symlinkSync(typeScriptSource, typescriptDest, 'junction');
// Link "rxjs" if it has been set up as a runfile. "rxjs" is linked optionally because
// not all compiler-cli tests need "rxjs" set up.
try {
const rxjsSource = resolveNpmTreeArtifact('rxjs', 'index.js');
const rxjsSource = resolveFromRunfiles('npm/node_modules/rxjs');
const rxjsDest = path.join(nodeModulesPath, 'rxjs');
fs.symlinkSync(rxjsSource, rxjsDest, 'junction');
} catch (e: any) {