@@ -1029,28 +1029,6 @@ and there is no security.
1029
1029
// https-loader.mjs
1030
1030
import { get } from 'node:https';
1031
1031
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
-
1054
1032
export function load(url, context, nextLoad) {
1055
1033
// For JavaScript to be loaded over the network, we need to fetch and
1056
1034
// return it.
@@ -1091,9 +1069,7 @@ prints the current version of CoffeeScript per the module at the URL in
1091
1069
#### Transpiler loader
1092
1070
1093
1071
Sources 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].
1097
1073
1098
1074
This is less performant than transpiling source files before running
1099
1075
Node.js; a transpiler loader should only be used for development and testing
@@ -1109,25 +1085,6 @@ import CoffeeScript from 'coffeescript';
1109
1085
1110
1086
const baseURL = pathToFileURL(`${cwd()}/`).href;
1111
1087
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
-
1131
1088
export async function load(url, context, nextLoad) {
1132
1089
if (extensionsRegex.test(url)) {
1133
1090
// 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`,
1220
1177
` .litcoffee ` or ` .coffee .md ` files referenced via ` import ` statements of any
1221
1178
loaded file.
1222
1179
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
+
1223
1224
## Resolution algorithm
1224
1225
1225
1226
### Features
@@ -1506,9 +1507,9 @@ _isImports_, _conditions_)
1506
1507
> 7. If _pjson?.type_ exists and is _"module"_, then
1507
1508
> 1. If _url_ ends in _".js"_, then
1508
1509
> 1. Return _"module"_.
1509
- > 2. Throw an _Unsupported File Extension_ error .
1510
+ > 2. return **undefined** .
1510
1511
> 8. Otherwise,
1511
- > 1. Throw an _Unsupported File Extension_ error .
1512
+ > 1. return **undefined** .
1512
1513
1513
1514
**LOOKUP\_ PACKAGE\_ SCOPE**(_url_)
1514
1515
@@ -1581,7 +1582,6 @@ for ESM specifiers is [commonjs-extension-resolution-loader][].
1581
1582
[custom https loader]: #https-loader
1582
1583
[load hook]: #loadurl-context-nextload
1583
1584
[percent-encoded]: url.md#percent-encoding-in-urls
1584
- [resolve hook]: #resolvespecifier-context-nextresolve
1585
1585
[special scheme]: https://url.spec.whatwg.org/#special-scheme
1586
1586
[status code]: process.md#exit-codes
1587
1587
[the official standard format]: https://tc39.github.io/ecma262/#sec-modules
0 commit comments