-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·64 lines (59 loc) · 1.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const pirates = require('pirates');
const { transform } = require('sucrase');
/**
* @param {string} extension File extension. All files with said file extension
* that go through the CJS loader will be transpiled.
* @param {import('sucrase').Options} [options] Options to pass to the Sucrase transform function.
* @returns {import('pirates').RevertFunction}
*/
function addHook(extension, options) {
return pirates.addHook(
(code, filePath) => {
code = code.replace(
/\bimport(\(['"`]\.[^'"`]+\.ts[`'"]\))/g,
`Promise.resolve(require$1)`,
);
if (!options?.transforms) {
// If there are no Sucrase transform necessary, we can return the code as is.
return code;
}
const { code: transformedCode, sourceMap } = transform(
// Replace dynamic imports of `.ts` files with `require`.
// Hooking into the Node.js ESM resolver would take more effort.
code,
{
...options,
sourceMapOptions: { compiledFilename: filePath },
filePath,
},
);
// Split the source map comment across two strings so that tools like
// source-map-support don't accidentally interpret it as a source map
// comment for this file.
const sourceMappingURL = 'sourceMappingURL';
const suffix = `//# ${sourceMappingURL}=data:application/json,${encodeURIComponent(
JSON.stringify(sourceMap),
)}`;
return `${transformedCode}\n${suffix}`;
},
{ exts: [extension] },
);
}
function defaultHooks() {
addHook('.js');
addHook('.ts', {
transforms: ['imports', 'typescript'],
disableESTransforms: true,
// We ask Sucrase to preserve dynamic imports because we replace them
// ourselves.
preserveDynamicImport: true,
});
}
if (module.isPreloading) {
defaultHooks();
}
module.exports = {
addHook,
defaultHooks,
};