Skip to content

Commit e0cdb53

Browse files
committed
extend build system to account for the babel packages with broken exports
this should be revertable once babel 8 is released and we upgrade to it
1 parent ae53eb7 commit e0cdb53

File tree

3 files changed

+97
-9
lines changed

3 files changed

+97
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
"terser": "^5.36.0"
7878
},
7979
"devDependencies": {
80-
"@samual/rollup-config": "0.1.3-efb250a",
80+
"@paralleldrive/cuid2": "^2.2.2",
81+
"@samual/rollup-config": "0.1.3-858c2de",
8182
"@total-typescript/ts-reset": "^0.6.1",
8283
"@types/babel__core": "^7.20.5",
8384
"@types/babel__generator": "^7.6.8",

pnpm-lock.yaml

Lines changed: 29 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rollup.config.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,69 @@
11
#!node_modules/.bin/rollup --config
2+
import t from "@babel/types"
3+
import { createId as cuid2 } from "@paralleldrive/cuid2"
4+
import { assert } from "@samual/lib/assert"
25
import { rollupConfig } from "@samual/rollup-config"
36

4-
export default rollupConfig()
7+
const isModuleBroken = (/** @type {string} */ name) =>
8+
name == "@babel/traverse" || name == "@babel/generator" || name.startsWith("@babel/plugin-")
9+
10+
export default rollupConfig({
11+
babelOptions: {
12+
plugins: [
13+
{
14+
name: "babel-has-broken-exports",
15+
visitor: {
16+
ImportDefaultSpecifier(path) {
17+
const importDeclarationPath = path.parentPath
18+
19+
assert(importDeclarationPath.isImportDeclaration())
20+
21+
if (!isModuleBroken(importDeclarationPath.node.source.value))
22+
return
23+
24+
const specifierName = path.node.local.name
25+
const specifierNewName = cuid2()
26+
27+
path.node.local.name = specifierNewName
28+
29+
importDeclarationPath.insertAfter(t.variableDeclaration("const", [
30+
t.variableDeclarator(
31+
t.identifier(specifierName),
32+
t.memberExpression(t.identifier(specifierNewName), t.identifier("default"))
33+
)
34+
]))
35+
},
36+
CallExpression(path) {
37+
if (path.node.callee.type != "Import" || ((path.node.arguments[0]?.type != "StringLiteral" ||
38+
!isModuleBroken(path.node.arguments[0].value)
39+
) && (path.node.arguments[0]?.type != "TemplateLiteral" ||
40+
!path.node.arguments[0].quasis[0] ||
41+
!isModuleBroken(path.node.arguments[0].quasis[0].value.raw)
42+
)))
43+
return
44+
45+
path.replaceWith(t.callExpression(
46+
t.memberExpression(
47+
path.node,
48+
t.identifier("then")
49+
),
50+
[
51+
t.arrowFunctionExpression(
52+
[
53+
t.identifier("module")
54+
],
55+
t.memberExpression(
56+
t.identifier("module"),
57+
t.identifier("default")
58+
)
59+
)
60+
]
61+
))
62+
63+
path.skip()
64+
}
65+
}
66+
}
67+
]
68+
}
69+
})

0 commit comments

Comments
 (0)