Skip to content

Commit

Permalink
refactor: update compiler-cli to work with ESM (angular#48521)
Browse files Browse the repository at this point in the history
Updates compiler-cli & tests to be full ESM compatible. Tests
no longer with CommonJS.

PR Close angular#48521
  • Loading branch information
devversion committed Dec 19, 2022
1 parent 619f090 commit decae5b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/compiler-cli/src/ngtsc/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
15 changes: 8 additions & 7 deletions packages/compiler-cli/src/ngtsc/testing/src/mock_file_loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}:
Expand Down Expand Up @@ -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'));
}
Expand Down
11 changes: 4 additions & 7 deletions packages/compiler-cli/src/ngtsc/testing/src/runfile_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
/// <reference types="node" />

import {runfiles} from '@bazel/runfiles';
import * as fs from 'fs';
import * as path from 'path';

Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler-cli/test/test_support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit decae5b

Please sign in to comment.