angular/tools/bazel/node_loader/hooks.mjs
Paul Gschwendtner d86d11d4c1 build: introduce NodeJS loader for rules_js Node execution (#61865)
For the `rules_js` migration, we are facing the problem where
our current Angular code is shipped as ESM, but we aren't fully
there yet with fully compliant strict ESM during development.

That is because we lack explicit import extensions, and it's also a
different story how this would work in Google3, if we were to add them.

In addition, we cross-import from our packages using npm module names.
This works well for TS, for ESBuild because those can respect path
mappings— but at runtime, when executing native `jasmine_test`'s— such
mappings aren't respected. The options here are:

- avoid module imports in the repo (impossible; undesired)
- use pre-bundling of all NodeJS execution involving npm package code
  (slower, extra build action cost)
- wire up a simple NodeJS loader (supported via official APIs) to simply
  account for our cases (preferred and similar to what we experimented
  with for the last year(s); and worked well)

This commit implements the last option and allows for an easy migration
to `rules_js`, and also is pretty reasonable. Long-term we can resolve
the extension problem if we e.g. migrate to real explicit extensions + a
proper TS module resolution like e.g. `nodenext`.

PR Close #61865
2025-06-05 12:04:51 +02:00

71 lines
2.2 KiB
JavaScript

/**
* @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.dev/license
*/
/**
* @fileoverview
*
* Module loader that augments NodeJS's execution to:
*
* - support native execution of Angular JavaScript output
* that isn't strict ESM at this point (lack of explicit extensions).
* - support path mappings at runtime. This allows us to natively execute ESM
* without having to pre-bundle for testing, or use the slow full npm linked packages
*/
import {parseTsconfig, createPathsMatcher} from 'get-tsconfig';
import path from 'node:path';
const explicitExtensionRe = /\.[mc]?js$/;
const nonModuleImportRe = /^[.\/]/;
const runfilesRoot = process.env.JS_BINARY__RUNFILES;
const tsconfigPath = path.join(runfilesRoot, 'angular/packages/tsconfig-build.json');
const tsconfig = parseTsconfig(tsconfigPath);
const pathMappingMatcher = createPathsMatcher({config: tsconfig, path: tsconfigPath});
/** @type {import('module').ResolveHook} */
export const resolve = async (specifier, context, nextResolve) => {
// True when it's a non-module import without explicit extensions.
const isNonModuleExtensionlessImport =
nonModuleImportRe.test(specifier) && !explicitExtensionRe.test(specifier);
const pathMappings = !nonModuleImportRe.test(specifier) ? pathMappingMatcher(specifier) : [];
// If it's neither path mapped, nor an extension-less import that may be fixed up, exit early.
if (!isNonModuleExtensionlessImport && pathMappings.length === 0) {
return nextResolve(specifier, context);
}
if (pathMappings.length > 0) {
for (const mapping of pathMappings) {
const res = await catchError(() => resolve(mapping, context, nextResolve));
if (res !== null) {
return res;
}
}
} else {
const fixedResult =
(await catchError(() => nextResolve(`${specifier}.js`, context))) ||
(await catchError(() => nextResolve(`${specifier}/index.js`, context)));
if (fixedResult !== null) {
return fixedResult;
}
}
return await nextResolve(specifier, context);
};
async function catchError(fn) {
try {
return await fn();
} catch {
return null;
}
}