-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathindex.js
69 lines (60 loc) · 2.14 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
65
66
67
68
69
'use strict';
const fs = require("fs/promises");
const path = require("path");
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) => {
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 `${filePath.endsWith(`types.ts`) ? transformedCode.replace(/\] ?= ?"(Npm|Pnpm|Yarn)";/g, s => s.toLowerCase()) :transformedCode}\n${suffix}`;
},
{ exts: [extension] },
);
}
addHook('.ts', {
transforms: ['imports', 'typescript'],
disableESTransforms: true,
// We ask Sucrase to preserve dynamic imports because we replace them
// ourselves.
preserveDynamicImport: true,
});
async function* findTestFiles(dirpath) {
for await (const dirent of await fs.opendir(dirpath)) {
if (dirent.name === "node_modules") continue;
if (dirent.isDirectory())
yield* findTestFiles(path.join(dirpath, dirent.name));
else if (dirent.name.endsWith(".test.ts")) yield path.join(dirpath, dirent.name);
}
}
(async () => {
for await (const file of findTestFiles(__dirname)) {
require(file);
}
})()