|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @fileoverview |
| 11 | + * |
| 12 | + * Module loader that augments NodeJS's execution to: |
| 13 | + * |
| 14 | + * - support native execution of Angular JavaScript output |
| 15 | + * that isn't strict ESM at this point (lack of explicit extensions). |
| 16 | + * - support path mappings at runtime. This allows us to natively execute ESM |
| 17 | + * without having to pre-bundle for testing, or use the slow full npm linked packages |
| 18 | + */ |
| 19 | + |
| 20 | +import {parseTsconfig, createPathsMatcher} from 'get-tsconfig'; |
| 21 | + |
| 22 | +import path from 'node:path'; |
| 23 | + |
| 24 | +const explicitExtensionRe = /\.[mc]?js$/; |
| 25 | +const nonModuleImportRe = /^[.\/]/; |
| 26 | + |
| 27 | +const runfilesRoot = process.env.JS_BINARY__RUNFILES; |
| 28 | +const tsconfigPath = process.env.NODE_OPTIONS_TSCONFIG_PATH; |
| 29 | + |
| 30 | +let pathMappingMatcher; |
| 31 | +// When no tsconfig is provided no match can be generated so we always return an empty list. |
| 32 | +if (tsconfigPath === undefined) { |
| 33 | + pathMappingMatcher = () => []; |
| 34 | +} else { |
| 35 | + const tsconfigFullPath = path.join(runfilesRoot, tsconfigPath); |
| 36 | + const tsconfig = parseTsconfig(tsconfigFullPath); |
| 37 | + pathMappingMatcher = createPathsMatcher({config: tsconfig, path: tsconfigFullPath}); |
| 38 | +} |
| 39 | + |
| 40 | +/** @type {import('module').ResolveHook} */ |
| 41 | +export const resolve = async (specifier, context, nextResolve) => { |
| 42 | + // True when it's a non-module import without explicit extensions. |
| 43 | + const isNonModuleExtensionlessImport = |
| 44 | + nonModuleImportRe.test(specifier) && !explicitExtensionRe.test(specifier); |
| 45 | + const pathMappings = !nonModuleImportRe.test(specifier) ? pathMappingMatcher(specifier) : []; |
| 46 | + |
| 47 | + // If it's neither path mapped, nor an extension-less import that may be fixed up, exit early. |
| 48 | + if (!isNonModuleExtensionlessImport && pathMappings.length === 0) { |
| 49 | + return nextResolve(specifier, context); |
| 50 | + } |
| 51 | + |
| 52 | + if (pathMappings.length > 0) { |
| 53 | + for (const mapping of pathMappings) { |
| 54 | + const res = await resolve(mapping, context, nextResolve).catch(() => null); |
| 55 | + if (res !== null) { |
| 56 | + return res; |
| 57 | + } |
| 58 | + } |
| 59 | + } else { |
| 60 | + const specifiers = [ |
| 61 | + `${specifier}.js`, |
| 62 | + `${specifier}/index.js`, |
| 63 | + // Legacy variants for the `zone.js` variant using still `ts_library`. |
| 64 | + // TODO(rules_js migration): Remove this. |
| 65 | + `${specifier}.mjs`, |
| 66 | + `${specifier}/index.mjs`, |
| 67 | + ]; |
| 68 | + for (const specifier of specifiers) { |
| 69 | + try { |
| 70 | + return await nextResolve(specifier, context); |
| 71 | + } catch {} |
| 72 | + } |
| 73 | + } |
| 74 | + return nextResolve(specifier, context); |
| 75 | +}; |
0 commit comments