angular/packages/compiler-cli/ngcc/index.ts
Paul Gschwendtner b1d6fad5e3 refactor(compiler-cli): do not use __filename or __dirname global for ESM compatibility (#43431)
Switches the compiler-cli usage of `__filename` to `import.meta.url`
when ESM bundles are generated. Unfortunately we cannot start using
only `import.meta` yet as we still build and run all code in Angular
in CommonJS module output for devmode tests.

This commit also fixes various instances where a jasmine spy was applied on
a namespace export that will break with ES module (and the interop for
CommonJS output). We fix these spies by using a default import.

PR Close #43431
2021-10-01 18:28:45 +00:00

41 lines
1.5 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* 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 {dirname, join} from 'path';
import {fileURLToPath} from 'url';
import {NodeJSFileSystem, setFileSystem} from '../src/ngtsc/file_system';
import {mainNgcc} from './src/main';
import {AsyncNgccOptions, SyncNgccOptions} from './src/ngcc_options';
export {ConsoleLogger, Logger, LogLevel} from '../src/ngtsc/logging';
export {AsyncNgccOptions, clearTsConfigCache, NgccOptions, SyncNgccOptions} from './src/ngcc_options';
export {PathMappings} from './src/path_mappings';
export function process<T extends AsyncNgccOptions|SyncNgccOptions>(options: T):
T extends AsyncNgccOptions ? Promise<void>: void;
export function process(options: AsyncNgccOptions|SyncNgccOptions): void|Promise<void> {
setFileSystem(new NodeJSFileSystem());
return mainNgcc(options);
}
// CommonJS/ESM interop for determining the current file name and containing
// directory. These path is needed for providing an absolute path to the ngcc
// command line entry-point script (for the CLI).
export const containingDirPath =
typeof __dirname !== 'undefined' ? __dirname : dirname(fileURLToPath(__ESM_IMPORT_META_URL__));
/**
* Absolute file path that points to the `ngcc` command line entry-point.
*
* This can be used by the Angular CLI to spawn a process running ngcc using
* command line options.
*/
export const ngccMainFilePath = join(containingDirPath, './main-ngcc.js');