Skip to content

fix(rsc): fix cjs default import on module runner #695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/plugin-rsc/src/plugins/cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export function cjsModuleRunnerPlugin(): Plugin[] {
// TODO: can we use cjs-module-lexer to properly define named exports?
// for re-exports, we need to eagerly transform dependencies though.
// https://github.com/nodejs/node/blob/f3adc11e37b8bfaaa026ea85c1cf22e3a0e29ae9/lib/internal/modules/esm/translators.js#L382-L409
output.append(`__vite_ssr_exportAll__(module.exports)`)
output.append(`
;__vite_ssr_exportAll__(module.exports);
export default module.exports;
`)
return {
code: output.toString(),
map: output.generateMap({ hires: 'boundary' }),
Expand Down
50 changes: 49 additions & 1 deletion packages/plugin-rsc/src/transforms/cjs.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { parseAstAsync } from 'vite'
import { createServer, createServerModuleRunner, parseAstAsync } from 'vite'
import { describe, expect, it } from 'vitest'
import { debugSourceMap } from './test-utils'
import { transformCjsToEsm } from './cjs'
import path from 'node:path'

describe(transformCjsToEsm, () => {
async function testTransform(input: string) {
Expand Down Expand Up @@ -83,4 +84,51 @@ if (true) {
"
`)
})

it('e2e', async () => {
const server = await createServer({
configFile: false,
logLevel: 'error',
root: path.join(import.meta.dirname, 'fixtures/cjs'),
plugins: [
{
name: 'cjs-module-runner-transform',
async transform(code, id) {
if (id.endsWith('.cjs')) {
const ast = await parseAstAsync(code)
const { output } = transformCjsToEsm(code, ast)
output.append(`
;__vite_ssr_exportAll__(module.exports);
export default module.exports;
`)
return {
code: output.toString(),
map: output.generateMap({ hires: 'boundary' }),
}
}
},
},
],
})
const runner = createServerModuleRunner(server.environments.ssr, {
hmr: false,
})
const mod = await runner.import('/entry.mjs')
expect(mod).toMatchInlineSnapshot(`
{
"depDefault": {
"a": "a",
"b": "b",
},
"depNamespace": {
"a": "a",
"b": "b",
"default": {
"a": "a",
"b": "b",
},
},
}
`)
})
})
2 changes: 2 additions & 0 deletions packages/plugin-rsc/src/transforms/fixtures/cjs/dep1.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.a = 'a'
exports.b = 'b'
2 changes: 2 additions & 0 deletions packages/plugin-rsc/src/transforms/fixtures/cjs/dep2.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.a = 'a'
exports.b = 'b'
3 changes: 3 additions & 0 deletions packages/plugin-rsc/src/transforms/fixtures/cjs/entry.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import depDefault from './dep1.cjs'
import * as depNamespace from './dep2.cjs'
export { depDefault, depNamespace }
Loading