Skip to content

Commit a3cdecb

Browse files
Fix #4: add support for Node 18
1 parent 525ee90 commit a3cdecb

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/create-loader.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,21 @@ class Loader {
6060
// For backwards compatibility purposes, we will manually compose
6161
// `format()`, `fetch()` and `transform()` into a `load()` function.
6262
const hook = id => (...args) => this.handleStack(id, ...args);
63-
const resolve = hook('resolve');
63+
const resolve = shortCircuit(hook('resolve'));
6464
const getFormat = hook('format');
6565
const getSource = hook('fetch');
6666

67+
// See #4. In Node 18 we now need to specify explicitly that we're
68+
// short-circuiting, which is what this function does.
69+
function shortCircuit(fn) {
70+
return async function(...args) {
71+
return {
72+
shortCircuit: true,
73+
...await fn(...args),
74+
};
75+
}
76+
}
77+
6778
// Handling transformation is fundamentally different as we have to
6879
// chain results here.
6980
const transformSource = async (source, ctx, node) => {
@@ -109,7 +120,7 @@ class Loader {
109120
// higher, which uses the new approach. We only have to export a
110121
// `resolve` and `load` function here, but the difficulty is that the
111122
// `load()` function has to be composed manually!
112-
const load = async function(url, ctx, defaultLoad) {
123+
const load = shortCircuit(async function(url, ctx, defaultLoad) {
113124

114125
// If the format was already specified by the resolve hook, we
115126
// won't try to fetch it again. Note that this functionality is
@@ -143,7 +154,7 @@ class Loader {
143154
source: transform.source,
144155
};
145156

146-
};
157+
});
147158
return { resolve, load };
148159

149160
}

test/loaders-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('ESM loaders', function() {
4040
});
4141
await new Promise(resolve => server.listen(resolve));
4242
const { port } = server.address();
43-
const url = `http://127.0.0.1:${port}`;
43+
const url = `http://127.0.0.1:${port}/foo`;
4444

4545
const run = this.loader('./loaders/http.js');
4646
let result = await run(`

0 commit comments

Comments
 (0)