-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
transpileImports.ts
48 lines (40 loc) · 1.42 KB
/
transpileImports.ts
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
import { walk } from 'estree-walker';
import rewriteImports from './rewriteImports';
import getAst from './getAst';
const hasImports = (code: string): boolean => !!code.match(/import[\S\s]+?['"]([^'"]+)['"];?/m);
/**
* Replace ECMAScript imports with require() calls
*/
export default function transpileImports(code: string): string {
// Don't do anything when the code has nothing that looks like an import
if (!hasImports(code)) {
return code;
}
// Ignore errors, they should be caught by Buble
const ast = getAst(code);
if (!ast) {
return code;
}
let offset = 0;
// estree walkers type is incompatible with acorns output
// it is working here out of luck and typescript is demonstrating it
// we have to go through the any part to keep the nodes with their `node.start`
// and `node.stop`
// eslint-disable-next-line @typescript-eslint/no-explicit-any
walk(ast as any, {
// import foo from 'foo'
// import 'foo'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
enter: (node: any) => {
if (node.type === 'ImportDeclaration' && node.source) {
const start = node.start + offset;
const end = node.end + offset;
const statement = code.substring(start, end);
const transpiledStatement = rewriteImports(statement);
code = code.substring(0, start) + transpiledStatement + code.substring(end);
offset += transpiledStatement.length - statement.length;
}
},
});
return code;
}