Skip to content

Commit 1a0732f

Browse files
committed
Add _interop bundle helper
1 parent e271d5e commit 1a0732f

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

.config/rollup.base.config.mjs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,33 @@ export default function baseConfig(extendConfig = {}) {
244244
)
245245
}
246246
}),
247+
// Add CJS interop helper for "default" only exports.
248+
socketModifyPlugin({
249+
find: /'use strict';?/,
250+
replace: match => `${match}\n
251+
function _interop(e) {
252+
let d
253+
if (e) {
254+
let c = 0
255+
for (const k in e) {
256+
d = c++ === 0 && k === 'default' ? e[k] : void 0
257+
if (!d) break
258+
}
259+
}
260+
return d ?? e
261+
}`
262+
}),
263+
// Wrap require calls with "_interop" helper.
264+
socketModifyPlugin({
265+
find: /(?<=\s*=\s*)require\(["'].+?["']\)(?=;?\r?\n)/g,
266+
replace: match => `_interop(${match})`
267+
}),
247268
commonjs({
248-
defaultIsModuleExports: 'auto',
249269
extensions: ['.cjs', '.js', '.ts', `.ts${ROLLUP_ENTRY_SUFFIX}`],
250270
ignoreDynamicRequires: true,
251271
ignoreGlobal: true,
252272
ignoreTryCatch: true,
253-
strictRequires: 'auto',
254-
transformMixedEsModules: true
273+
strictRequires: 'auto'
255274
}),
256275
...(extendConfig.plugins ?? [])
257276
]

scripts/rollup/socket-modify-plugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function socketModifyPlugin({
1616
renderChunk(code, { fileName }) {
1717
if (!filter(fileName)) return null
1818
const s = new MagicString(code)
19+
const { global } = find
1920
find.lastIndex = 0
2021
let match
2122
while ((match = find.exec(code)) !== null) {
@@ -26,6 +27,9 @@ function socketModifyPlugin({
2627
? Reflect.apply(replace, match, match)
2728
: String(replace)
2829
)
30+
if (!global) {
31+
break
32+
}
2933
}
3034
return {
3135
code: s.toString(),

0 commit comments

Comments
 (0)