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
This commit is contained in:
Paul Gschwendtner
2021-09-29 23:26:42 +02:00
committed by Dylan Hunn
parent b1d6fad5e3
commit d442764470
2 changed files with 14 additions and 2 deletions
+1 -1
View File
@@ -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
@@ -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 {