From d44276447055d3d28489ffd42590d3be598da2a1 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 29 Sep 2021 23:26:42 +0200 Subject: [PATCH] refactor(compiler-cli): adjust lock file resolution in ngcc to work with ESM (#43431) Updates the lock file resolution logic in ngcc to work with ESM output. The compiler-cli is now shipped in bundles, so the actual module resolution needs to stay to keep the lock file path consistent regardless of where the lock file code is bundled into. The ngcc integration test needs to be updated though since the `ngcc` entry-point will always reside in the `bundles/` directory now. It has been considered using the top-level `package.json` of the compiler-cli package, but that caused problems in tests down the line because the ngcc tests only have the `@angular/compiler-cli/ngcc/...` targets linked into the node modules. It's not worth changing this and reworking tests if ngcc is going away in the future anyway (+ it has been like that before!). PR Close #43431 --- integration/ngcc/test.sh | 2 +- .../compiler-cli/ngcc/src/locking/lock_file.ts | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/integration/ngcc/test.sh b/integration/ngcc/test.sh index df50722d64f..e6895ef66b2 100755 --- a/integration/ngcc/test.sh +++ b/integration/ngcc/test.sh @@ -209,7 +209,7 @@ ngcc --formats fesm2015 assertFailed "Expected 'ngcc --formats fesm2015' to fail (since '--formats' is deprecated)." # Does it timeout if there is another ngcc process running -LOCKFILE=node_modules/@angular/compiler-cli/ngcc/__ngcc_lock_file__ +LOCKFILE=node_modules/@angular/compiler-cli/bundles/ngcc/__ngcc_lock_file__ touch $LOCKFILE trap "[[ -f $LOCKFILE ]] && rm $LOCKFILE" EXIT ngcc diff --git a/packages/compiler-cli/ngcc/src/locking/lock_file.ts b/packages/compiler-cli/ngcc/src/locking/lock_file.ts index 08f141c0ee7..d627e0106a6 100644 --- a/packages/compiler-cli/ngcc/src/locking/lock_file.ts +++ b/packages/compiler-cli/ngcc/src/locking/lock_file.ts @@ -5,10 +5,22 @@ * 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 module from 'module'; + import {AbsoluteFsPath, PathManipulation} from '../../../src/ngtsc/file_system'; export function getLockFilePath(fs: PathManipulation) { - return fs.resolve(require.resolve('@angular/compiler-cli/ngcc'), '../__ngcc_lock_file__'); + // This is an interop allowing for the unlocking script to be determined in both + // a CommonJS module, or an ES module which does not come with `require` by default. + const requireFn = + typeof require !== 'undefined' ? require : module.createRequire(__ESM_IMPORT_META_URL__); + // The lock file location is resolved based on the location of the `ngcc` entry-point as this + // allows us to have a consistent position for the lock file to reside. We are unable to rely + // on `__dirname` (or equivalent) as this code is being bundled and different entry-points + // will have dedicated bundles where the lock file location would differ then. + const ngccEntryPointFile = requireFn.resolve('@angular/compiler-cli/ngcc'); + return fs.resolve(ngccEntryPointFile, '../__ngcc_lock_file__'); } export interface LockFile {