@@ -1029,28 +1029,6 @@ and there is no security.
10291029// https-loader.mjs
10301030import { get } from 'node:https';
10311031
1032- export function resolve(specifier, context, nextResolve) {
1033- const { parentURL = null } = context;
1034-
1035- // Normally Node.js would error on specifiers starting with 'https://', so
1036- // this hook intercepts them and converts them into absolute URLs to be
1037- // passed along to the later hooks below.
1038- if (specifier.startsWith('https://')) {
1039- return {
1040- shortCircuit: true,
1041- url: specifier,
1042- };
1043- } else if (parentURL && parentURL.startsWith('https://')) {
1044- return {
1045- shortCircuit: true,
1046- url: new URL(specifier, parentURL).href,
1047- };
1048- }
1049-
1050- // Let Node.js handle all other specifiers.
1051- return nextResolve(specifier);
1052- }
1053-
10541032export function load(url, context, nextLoad) {
10551033 // For JavaScript to be loaded over the network, we need to fetch and
10561034 // return it.
@@ -1091,9 +1069,7 @@ prints the current version of CoffeeScript per the module at the URL in
10911069#### Transpiler loader
10921070
10931071Sources that are in formats Node .js doesn' t understand can be converted into
1094- JavaScript using the [`load` hook][load hook]. Before that hook gets called,
1095- however, a [`resolve` hook][resolve hook] needs to tell Node.js not to
1096- throw an error on unknown file types.
1072+ JavaScript using the [`load` hook][load hook].
10971073
10981074This is less performant than transpiling source files before running
10991075Node.js; a transpiler loader should only be used for development and testing
@@ -1109,25 +1085,6 @@ import CoffeeScript from 'coffeescript';
11091085
11101086const baseURL = pathToFileURL(`${cwd()}/`).href;
11111087
1112- // CoffeeScript files end in .coffee, .litcoffee, or .coffee.md.
1113- const extensionsRegex = /\. coffee$|\. litcoffee$|\. coffee\. md$/;
1114-
1115- export function resolve(specifier, context, nextResolve) {
1116- if (extensionsRegex.test(specifier)) {
1117- const { parentURL = baseURL } = context;
1118-
1119- // Node.js normally errors on unknown file extensions, so return a URL for
1120- // specifiers ending in the CoffeeScript file extensions.
1121- return {
1122- shortCircuit: true,
1123- url: new URL(specifier, parentURL).href,
1124- };
1125- }
1126-
1127- // Let Node.js handle all other specifiers.
1128- return nextResolve(specifier);
1129- }
1130-
11311088export async function load(url, context, nextLoad) {
11321089 if (extensionsRegex.test(url)) {
11331090 // Now that we patched resolve to let CoffeeScript URLs through, we need to
@@ -1220,6 +1177,50 @@ loaded from disk but before Node.js executes it; and so on for any `.coffee`,
12201177` .litcoffee ` or ` .coffee .md ` files referenced via ` import ` statements of any
12211178loaded file.
12221179
1180+ #### Overriding loader
1181+
1182+ The above two loaders hooked into the "load" phase of the module loader.
1183+ This loader hooks into the "resolution" phase. This loader reads an
1184+ ` overrides.json` file that specifies which specifiers to override to another
1185+ url.
1186+
1187+ ` ` ` js
1188+ // overriding-loader.js
1189+ import fs from ' node:fs/promises' ;
1190+
1191+ const overrides = JSON .parse (await fs .readFile (' overrides.json' ));
1192+
1193+ export async function resolve (specifier , context , nextResolve ) {
1194+ if (specifier in overrides) {
1195+ return nextResolve (overrides[specifier], context);
1196+ }
1197+
1198+ return nextResolve (specifier, context);
1199+ }
1200+ ` ` `
1201+
1202+ Let's assume we have these files:
1203+
1204+ ` ` ` js
1205+ // main.js
1206+ import ' a-module-to-override' ;
1207+ ` ` `
1208+
1209+ ` ` ` json
1210+ // overrides.json
1211+ {
1212+ " a-module-to-override" : " ./module-override.js"
1213+ }
1214+ ` ` `
1215+
1216+ ` ` ` js
1217+ // module-override.js
1218+ console .log (' module overridden!' );
1219+ ` ` `
1220+
1221+ If you run ` node -- experimental- loader ./ overriding- loader .js main .js `
1222+ the output will be ` module overriden! ` .
1223+
12231224## Resolution algorithm
12241225
12251226### Features
@@ -1506,9 +1507,9 @@ _isImports_, _conditions_)
15061507> 7. If _pjson?.type_ exists and is _"module"_, then
15071508> 1. If _url_ ends in _".js"_, then
15081509> 1. Return _"module"_.
1509- > 2. Throw an _Unsupported File Extension_ error .
1510+ > 2. return **undefined** .
15101511> 8. Otherwise,
1511- > 1. Throw an _Unsupported File Extension_ error .
1512+ > 1. return **undefined** .
15121513
15131514**LOOKUP\_ PACKAGE\_ SCOPE**(_url_)
15141515
@@ -1581,7 +1582,6 @@ for ESM specifiers is [commonjs-extension-resolution-loader][].
15811582[custom https loader]: #https-loader
15821583[load hook]: #loadurl-context-nextload
15831584[percent-encoded]: url.md#percent-encoding-in-urls
1584- [resolve hook]: #resolvespecifier-context-nextresolve
15851585[special scheme]: https://url.spec.whatwg.org/#special-scheme
15861586[status code]: process.md#exit-codes
15871587[the official standard format]: https://tc39.github.io/ecma262/#sec-modules
0 commit comments