Skip to content

Commit 213c4d6

Browse files
Merge pull request #3 from STRd6/coffeescript-cjs-esm
2 parents 30b03c3 + b2a70d9 commit 213c4d6

File tree

2 files changed

+11
-28
lines changed

2 files changed

+11
-28
lines changed

coffeescript-loader/loader.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { readFile } from 'fs/promises';
2-
import { readFileSync } from 'fs';
32
import { createRequire } from 'module';
43
import { dirname, extname, resolve as resolvePath } from 'path';
54
import { fileURLToPath, pathToFileURL } from 'url';
65

76
import CoffeeScript from 'coffeescript';
8-
7+
const coffeeCompile = (source, filename) => CoffeeScript.compile(source, {
8+
bare: true,
9+
filename
10+
});
911

1012
const baseURL = pathToFileURL(process.cwd() + '/').href;
1113

@@ -51,10 +53,7 @@ export async function load(url, context, defaultLoad) {
5153
const { source: rawSource } = await defaultLoad(url, { format });
5254
// This hook converts CoffeeScript source code into JavaScript source code
5355
// for all imported CoffeeScript files.
54-
const transformedSource = CoffeeScript.compile(rawSource.toString(), {
55-
bare: true,
56-
filename: url,
57-
});
56+
const transformedSource = coffeeCompile(rawSource.toString(), url)
5857

5958
return {
6059
format,
@@ -84,15 +83,6 @@ async function getPackageType(url) {
8483
return dir.length > 1 && getPackageType(resolvePath(dir, '..'));
8584
}
8685

87-
88-
// Register CoffeeScript to also transform CommonJS files. This can more
89-
// thoroughly be done for CoffeeScript specifically via
90-
// `CoffeeScript.register()`, but for purposes of this example this is the
91-
// simplest method.
86+
// Register CoffeeScript to also transform CommonJS files.
9287
const require = createRequire(import.meta.url);
93-
['.coffee', '.litcoffee', '.coffee.md'].forEach(extension => {
94-
require.extensions[extension] = (module, filename) => {
95-
const source = readFileSync(filename, 'utf8');
96-
return CoffeeScript.compile(source, { bare: true, filename });
97-
}
98-
})
88+
require("coffeescript/register")

coffeescript-loader/test.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { ok } from 'assert';
22
import { spawn } from 'child_process';
33
import { execPath } from 'process';
4-
import { fileURLToPath, URL } from 'url';
5-
64

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

108
const child = spawn(execPath, [
119
'--experimental-loader',
12-
fileURLToPath(new URL('./loader.js', import.meta.url).href),
13-
fileURLToPath(new URL('./fixtures/esm-and-commonjs-imports.coffee', import.meta.url).href),
10+
'./loader.js',
11+
'./fixtures/esm-and-commonjs-imports.coffee',
1412
]);
1513
let stdout = '';
1614
child.stdout.setEncoding('utf8');
@@ -24,12 +22,7 @@ child.stderr.on('data', (data) => {
2422
});
2523

2624
child.on('close', (code, signal) => {
27-
ok(stdout.includes('Hello from CoffeeScript', 'Main entry transpiles'));
25+
ok(stdout.includes('Hello from CoffeeScript'), 'Main entry transpiles');
2826
ok(stdout.includes('HELLO FROM ESM'), 'ESM import transpiles');
29-
30-
// There is a known issue between ESM + CJS where CJS named exports get lost:
31-
// require.extentions appropriately supplies (transformed) source, but an
32-
// empty object is returned for the module's exports.
33-
// the import does NOT hit ESMLoader or its CJS strategy.
34-
ok(!stdout.includes('Hello from CommonJS!'), 'Named CommonJS import fails');
27+
ok(stdout.includes('Hello from CommonJS!'), 'Named CommonJS import fails');
3528
});

0 commit comments

Comments
 (0)