-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrollup.config.js
executable file
·69 lines (60 loc) · 1.85 KB
/
rollup.config.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
#!node_modules/.bin/rollup --config
import t from "@babel/types"
import { createId as cuid2 } from "@paralleldrive/cuid2"
import { assert } from "@samual/lib/assert"
import { rollupConfig } from "@samual/rollup-config"
const isModuleBroken = (/** @type {string} */ name) =>
name == "@babel/traverse" || name == "@babel/generator" || name.startsWith("@babel/plugin-")
export default rollupConfig({
babelOptions: {
plugins: [
{
name: "babel-has-broken-exports",
visitor: {
ImportDefaultSpecifier(path) {
const importDeclarationPath = path.parentPath
assert(importDeclarationPath.isImportDeclaration())
if (!isModuleBroken(importDeclarationPath.node.source.value))
return
const specifierName = path.node.local.name
const specifierNewName = cuid2()
path.node.local.name = specifierNewName
importDeclarationPath.insertAfter(t.variableDeclaration("const", [
t.variableDeclarator(
t.identifier(specifierName),
t.memberExpression(t.identifier(specifierNewName), t.identifier("default"))
)
]))
},
CallExpression(path) {
if (path.node.callee.type != "Import" || ((path.node.arguments[0]?.type != "StringLiteral" ||
!isModuleBroken(path.node.arguments[0].value)
) && (path.node.arguments[0]?.type != "TemplateLiteral" ||
!path.node.arguments[0].quasis[0] ||
!isModuleBroken(path.node.arguments[0].quasis[0].value.raw)
)))
return
path.replaceWith(t.callExpression(
t.memberExpression(
path.node,
t.identifier("then")
),
[
t.arrowFunctionExpression(
[
t.identifier("module")
],
t.memberExpression(
t.identifier("module"),
t.identifier("default")
)
)
]
))
path.skip()
}
}
}
]
}
})