Skip to content

Commit 8f342d8

Browse files
committed
fix(build): prevent module2.module.exports generation
The fix-commonjs-exports script was incorrectly transforming `module2.exports.default` to `module2.module.exports` by replacing `exports.default` without checking for esbuild's moduleN. prefix. Updated the check to skip transformation when exports.default is preceded by any moduleN. pattern (module., module2., etc.), not just module. This prevents the invalid module2.module.exports pattern from being generated in external bundles. Fixes prompt tests that were failing with "Cannot set properties of undefined (setting 'exports')" errors.
1 parent cac6b7a commit 8f342d8

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

scripts/fix-commonjs-exports.mjs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,22 @@ async function processDirectory(dir, verbose = false) {
258258
}
259259

260260
// Check if this is a single default export (legacy pattern)
261-
// Only match 'exports.default =' that is NOT preceded by 'module.'
261+
// Only match 'exports.default =' that is NOT preceded by 'module.' or 'moduleN.'
262262
if (content.includes('exports.default =')) {
263263
// Transform exports.default = value to module.exports = value
264264
let pos = 0
265265
while ((pos = content.indexOf('exports.default = ', pos)) !== -1) {
266-
// Check if this is preceded by 'module.'
267-
const beforePos = pos - 'module.'.length
268-
if (beforePos >= 0 && content.slice(beforePos, pos) === 'module.') {
269-
// Skip module.exports.default (it's already correct)
266+
// Check if this is preceded by 'module.' or 'moduleN.' (from esbuild CommonJS wrapper)
267+
const beforeModule = pos - 'module.'.length
268+
const beforeModule2 = pos - 'module2.'.length
269+
const isModule = beforeModule >= 0 && content.slice(beforeModule, pos) === 'module.'
270+
const isModule2 = beforeModule2 >= 0 && content.slice(beforeModule2, pos) === 'module2.'
271+
// Also check for generic moduleN. pattern
272+
const beforeText = content.slice(Math.max(0, pos - 10), pos)
273+
const hasModuleNPrefix = /module\d*\.$/ .test(beforeText)
274+
275+
if (isModule || isModule2 || hasModuleNPrefix) {
276+
// Skip moduleN.exports.default (it's already from esbuild wrapper)
270277
pos += 1
271278
continue
272279
}
@@ -359,19 +366,21 @@ async function processDirectory(dir, verbose = false) {
359366
/[ \t]*module2\.module\.exports\s*=\s*[^;]+;[ \t]*\n?/g
360367
const matches = [...content.matchAll(pattern)]
361368

362-
if (matches.length > 0 && verbose) {
363-
console.log(
364-
` Removing ${matches.length} module2.module.exports lines from ${path.basename(fullPath)}`,
365-
)
366-
}
369+
if (matches.length > 0) {
370+
if (verbose) {
371+
console.log(
372+
` Removing ${matches.length} module2.module.exports lines from ${path.basename(fullPath)}`,
373+
)
374+
}
367375

368-
// Process matches in reverse order to maintain correct indices
369-
for (let i = matches.length - 1; i >= 0; i--) {
370-
const match = matches[i]
371-
const start = match.index
372-
const end = start + match[0].length
373-
s.remove(start, end)
374-
modified = true
376+
// Process matches in reverse order to maintain correct indices
377+
for (let i = matches.length - 1; i >= 0; i--) {
378+
const match = matches[i]
379+
const start = match.index
380+
const end = start + match[0].length
381+
s.remove(start, end)
382+
modified = true
383+
}
375384
}
376385
}
377386

0 commit comments

Comments
 (0)