Skip to content

Commit 835506a

Browse files
Merge pull request #6 from nodejs/feat/support-chaining
2 parents 7569804 + 5d6ce23 commit 835506a

File tree

5 files changed

+69
-47
lines changed

5 files changed

+69
-47
lines changed

coffeescript-loader/loader.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@ const baseURL = pathToFileURL(process.cwd() + '/').href;
1414
// CoffeeScript files end in .coffee, .litcoffee or .coffee.md.
1515
const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;
1616

17-
export async function resolve(specifier, context, defaultResolve) {
17+
export async function resolve(specifier, context, nextResolve) {
1818
const { parentURL = baseURL } = context;
1919

2020
// Node.js normally errors on unknown file extensions, so return a URL for
2121
// specifiers ending in the CoffeeScript file extensions.
2222
if (extensionsRegex.test(specifier)) {
2323
return {
24+
shortCircuit: true,
2425
url: new URL(specifier, parentURL).href
2526
};
2627
}
2728

2829
// Let Node.js handle all other specifiers.
29-
return defaultResolve(specifier, context, defaultResolve);
30+
return nextResolve(specifier, context);
3031
}
3132

32-
export async function load(url, context, defaultLoad) {
33+
export async function load(url, context, nextLoad) {
3334
// Now that we patched resolve to let CoffeeScript URLs through, we need to
3435
// tell Node.js what format such URLs should be interpreted as. Because
3536
// CoffeeScript transpiles into JavaScript, it should be one of the two
@@ -47,22 +48,26 @@ export async function load(url, context, defaultLoad) {
4748
// loader. Avoiding the need for a separate CommonJS handler is a future
4849
// enhancement planned for ES module loaders.
4950
if (format === 'commonjs') {
50-
return { format };
51+
return {
52+
format,
53+
shortCircuit: true,
54+
};
5155
}
5256

53-
const { source: rawSource } = await defaultLoad(url, { format });
57+
const { source: rawSource } = await nextLoad(url, { format });
5458
// This hook converts CoffeeScript source code into JavaScript source code
5559
// for all imported CoffeeScript files.
5660
const transformedSource = coffeeCompile(rawSource.toString(), url)
5761

5862
return {
5963
format,
64+
shortCircuit: true,
6065
source: transformedSource,
6166
};
6267
}
6368

6469
// Let Node.js handle all other URLs.
65-
return defaultLoad(url, context, defaultLoad);
70+
return nextLoad(url, context);
6671
}
6772

6873
async function getPackageType(url) {

coffeescript-loader/package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coffeescript-loader/test.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
import { ok } from 'assert';
2-
import { spawn } from 'child_process';
1+
import { fileURLToPath } from 'url';
2+
import { ok, strictEqual } from 'assert';
3+
import { spawnSync } from 'child_process';
34
import { execPath } from 'process';
45

56
// Run this test yourself with debugging mode via:
67
// node --inspect-brk --experimental-loader ./loader.js ./fixtures/esm-and-commonjs-imports.coffee
78

8-
const child = spawn(execPath, [
9-
'--experimental-loader',
9+
const loader = fileURLToPath(new URL(
1010
'./loader.js',
11+
import.meta.url
12+
));
13+
const fixture = fileURLToPath(new URL(
1114
'./fixtures/esm-and-commonjs-imports.coffee',
12-
]);
13-
let stdout = '';
14-
child.stdout.setEncoding('utf8');
15-
child.stdout.on('data', (data) => {
16-
stdout += data;
17-
});
18-
let stderr = '';
19-
child.stderr.setEncoding('utf8');
20-
child.stderr.on('data', (data) => {
21-
stderr += data;
22-
});
15+
import.meta.url
16+
));
2317

24-
child.on('close', (code, signal) => {
25-
ok(stdout.includes('Hello from CoffeeScript'), 'Main entry transpiles');
26-
ok(stdout.includes('HELLO FROM ESM'), 'ESM import transpiles');
27-
ok(stdout.includes('Hello from CommonJS!'), 'Named CommonJS import fails');
28-
});
18+
const { status, stderr, stdout } = spawnSync(
19+
execPath,
20+
[
21+
'--experimental-loader',
22+
loader,
23+
fixture,
24+
],
25+
{ encoding: 'utf8' },
26+
);
27+
28+
console.error(stderr);
29+
30+
strictEqual(status, 0);
31+
ok(stdout.includes('Hello from CoffeeScript'), 'Main entry transpiles');
32+
ok(stdout.includes('HELLO FROM ESM'), 'ESM import transpiles');
33+
ok(stdout.includes('Hello from CommonJS!'), 'Named CommonJS import fails');

https-loader/loader.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
import { get } from 'https';
22

3-
export function resolve(specifier, context, defaultResolve) {
3+
export function resolve(specifier, context, nextResolve) {
44
const { parentURL = null } = context;
55

66
// Normally Node.js would error on specifiers starting with 'https://', so
77
// this hook intercepts them and converts them into absolute URLs to be
88
// passed along to the later hooks below.
99
if (specifier.startsWith('https://')) {
1010
return {
11+
shortCircuit: true,
1112
url: specifier
1213
};
1314
} else if (parentURL && parentURL.startsWith('https://')) {
1415
return {
16+
shortCircuit: true,
1517
url: new URL(specifier, parentURL).href
1618
};
1719
}
1820

1921
// Let Node.js handle all other specifiers.
20-
return defaultResolve(specifier, context, defaultResolve);
22+
return nextResolve(specifier, context);
2123
}
2224

23-
export function load(url, context, defaultLoad) {
25+
export function load(url, context, nextLoad) {
2426
// For JavaScript to be loaded over the network, we need to fetch and
2527
// return it.
2628
if (url.startsWith('https://')) {
@@ -32,12 +34,13 @@ export function load(url, context, defaultLoad) {
3234
// This example assumes all network-provided JavaScript is ES module
3335
// code.
3436
format: 'module',
37+
shortCircuit: true,
3538
source: data,
3639
}));
3740
}).on('error', err => reject(err));
3841
});
3942
}
4043

4144
// Let Node.js handle all other URLs.
42-
return defaultLoad(url, context, defaultLoad);
45+
return nextLoad(url, context);
4346
}

https-loader/test.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
1-
import { ok } from 'assert';
2-
import { spawn } from 'child_process';
1+
import { fileURLToPath } from 'url';
2+
import { ok, strictEqual } from 'assert';
3+
import { spawnSync } from 'child_process';
34
import { execPath } from 'process';
4-
import { fileURLToPath, URL } from 'url';
5-
65

76
// Run this test yourself with debugging mode via:
87
// node --inspect-brk --experimental-loader ./loader.js ./fixture.js
98

10-
const child = spawn(execPath, [
11-
'--experimental-loader',
12-
fileURLToPath(new URL('./loader.js', import.meta.url).href),
13-
fileURLToPath(new URL('./fixture.js', import.meta.url).href),
14-
]);
9+
const loader = fileURLToPath(new URL(
10+
'./loader.js',
11+
import.meta.url
12+
));
13+
const fixture = fileURLToPath(new URL(
14+
'./fixture.js',
15+
import.meta.url
16+
));
17+
18+
const { status, stderr, stdout } = spawnSync(
19+
execPath,
20+
[
21+
'--experimental-loader',
22+
loader,
23+
fixture,
24+
],
25+
{ encoding: 'utf8' },
26+
);
27+
28+
console.error(stderr);
1529

16-
let stdout = '';
17-
child.stdout.setEncoding('utf8');
18-
child.stdout.on('data', (data) => {
19-
stdout += data;
20-
});
2130

22-
child.on('close', (code, signal) => {
23-
ok(/The browser-based version of CoffeeScript hosted at coffeescript\.org is: \d+\.\d+\.\d+/.test(stdout.toString()));
24-
});
31+
strictEqual(status, 0);
32+
ok(/The browser-based version of CoffeeScript hosted at coffeescript\.org is: \d+\.\d+\.\d+/.test(stdout.toString()));

0 commit comments

Comments
 (0)