|
1 | 1 | import { readFile } from 'fs/promises';
|
2 |
| -import { readFileSync } from 'fs'; |
3 | 2 | import { createRequire } from 'module';
|
4 | 3 | import { dirname, extname, resolve as resolvePath } from 'path';
|
5 | 4 | import { fileURLToPath, pathToFileURL } from 'url';
|
6 | 5 |
|
7 | 6 | import CoffeeScript from 'coffeescript';
|
8 |
| - |
| 7 | +const coffeeCompile = (source, filename) => CoffeeScript.compile(source, { |
| 8 | + bare: true, |
| 9 | + filename |
| 10 | +}); |
9 | 11 |
|
10 | 12 | const baseURL = pathToFileURL(process.cwd() + '/').href;
|
11 | 13 |
|
@@ -51,10 +53,7 @@ export async function load(url, context, defaultLoad) {
|
51 | 53 | const { source: rawSource } = await defaultLoad(url, { format });
|
52 | 54 | // This hook converts CoffeeScript source code into JavaScript source code
|
53 | 55 | // 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) |
58 | 57 |
|
59 | 58 | return {
|
60 | 59 | format,
|
@@ -84,15 +83,6 @@ async function getPackageType(url) {
|
84 | 83 | return dir.length > 1 && getPackageType(resolvePath(dir, '..'));
|
85 | 84 | }
|
86 | 85 |
|
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. |
92 | 87 | 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") |
0 commit comments