-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesmcjs.js
More file actions
104 lines (94 loc) · 3.55 KB
/
esmcjs.js
File metadata and controls
104 lines (94 loc) · 3.55 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Babel plugin: transform-imports-to-require
* ------------------------------------------------------------------
* Transforms ES Module import statements into CommonJS require() calls.
* This plugin enables ES Module syntax to work in CommonJS environments.
*
* The plugin transforms code by:
* 1. Converting import statements into equivalent require() calls
* 2. Handling both default and named imports
* 3. Maintaining proper variable declarations and scoping
*
* Key behaviors:
* 1. Transforms default imports (import x from 'y') into const x = require('y')
* 2. Transforms named imports (import { x } from 'y') into const { x } = require('y')
* 3. Preserves the const declaration to maintain immutability
* 4. Handles multiple specifiers in a single import statement
*/
module.exports = function transformImportsToRequire(babel) {
const { types: t } = babel
return {
name: 'transform-imports-to-require',
visitor: {
ImportDeclaration(path) {
const { node } = path
const source = node.source.value
const specifiers = node.specifiers
// Handle side-effect only imports: `import 'module';`
if (specifiers.length === 0) {
path.replaceWith(
t.expressionStatement(
t.callExpression(t.identifier('require'), [
t.stringLiteral(source),
])
)
)
return
}
// Categorize specifiers
const defaultSpecifiers = specifiers.filter((s) =>
t.isImportDefaultSpecifier(s)
)
const namedSpecifiers = specifiers.filter((s) => t.isImportSpecifier(s))
const namespaceSpecifiers = specifiers.filter((s) =>
t.isImportNamespaceSpecifier(s)
)
const declarations = []
// Default import: `import foo from 'bar'` → `const foo = require('bar');`
if (defaultSpecifiers.length > 0) {
const [{ local }] = defaultSpecifiers // There can be only one default
declarations.push(
t.variableDeclarator(
t.identifier(local.name),
t.callExpression(t.identifier('require'), [
t.stringLiteral(source),
])
)
)
}
// Namespace import: `import * as ns from 'bar'` → `const ns = require('bar');`
if (namespaceSpecifiers.length > 0) {
const [{ local }] = namespaceSpecifiers // There can be only one namespace import
declarations.push(
t.variableDeclarator(
t.identifier(local.name),
t.callExpression(t.identifier('require'), [
t.stringLiteral(source),
])
)
)
}
// Named imports: `import { x, y as z } from 'bar'` → `const { x, y: z } = require('bar');`
if (namedSpecifiers.length > 0) {
const properties = namedSpecifiers.map((s) =>
t.objectProperty(
t.identifier(s.imported.name),
t.identifier(s.local.name),
false,
s.imported.name === s.local.name // Shorthand when names match
)
)
declarations.push(
t.variableDeclarator(
t.objectPattern(properties),
t.callExpression(t.identifier('require'), [
t.stringLiteral(source),
])
)
)
}
// Replace the original import with one or more `const` declarations.
path.replaceWith(t.variableDeclaration('const', declarations))
},
},
}
}