|
1 | 1 | #!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" |
2 | 5 | import { rollupConfig } from "@samual/rollup-config"
|
3 | 6 |
|
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