Skip to content

Commit c329914

Browse files
authored
fix(rsc): fix cjs default import on module runner (#695)
1 parent 9da2c98 commit c329914

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

packages/plugin-rsc/src/plugins/cjs.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ export function cjsModuleRunnerPlugin(): Plugin[] {
5656
// TODO: can we use cjs-module-lexer to properly define named exports?
5757
// for re-exports, we need to eagerly transform dependencies though.
5858
// https://github.com/nodejs/node/blob/f3adc11e37b8bfaaa026ea85c1cf22e3a0e29ae9/lib/internal/modules/esm/translators.js#L382-L409
59-
output.append(`__vite_ssr_exportAll__(module.exports)`)
59+
output.append(`
60+
;__vite_ssr_exportAll__(module.exports);
61+
export default module.exports;
62+
`)
6063
return {
6164
code: output.toString(),
6265
map: output.generateMap({ hires: 'boundary' }),

packages/plugin-rsc/src/transforms/cjs.test.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { parseAstAsync } from 'vite'
1+
import { createServer, createServerModuleRunner, parseAstAsync } from 'vite'
22
import { describe, expect, it } from 'vitest'
33
import { debugSourceMap } from './test-utils'
44
import { transformCjsToEsm } from './cjs'
5+
import path from 'node:path'
56

67
describe(transformCjsToEsm, () => {
78
async function testTransform(input: string) {
@@ -83,4 +84,51 @@ if (true) {
8384
"
8485
`)
8586
})
87+
88+
it('e2e', async () => {
89+
const server = await createServer({
90+
configFile: false,
91+
logLevel: 'error',
92+
root: path.join(import.meta.dirname, 'fixtures/cjs'),
93+
plugins: [
94+
{
95+
name: 'cjs-module-runner-transform',
96+
async transform(code, id) {
97+
if (id.endsWith('.cjs')) {
98+
const ast = await parseAstAsync(code)
99+
const { output } = transformCjsToEsm(code, ast)
100+
output.append(`
101+
;__vite_ssr_exportAll__(module.exports);
102+
export default module.exports;
103+
`)
104+
return {
105+
code: output.toString(),
106+
map: output.generateMap({ hires: 'boundary' }),
107+
}
108+
}
109+
},
110+
},
111+
],
112+
})
113+
const runner = createServerModuleRunner(server.environments.ssr, {
114+
hmr: false,
115+
})
116+
const mod = await runner.import('/entry.mjs')
117+
expect(mod).toMatchInlineSnapshot(`
118+
{
119+
"depDefault": {
120+
"a": "a",
121+
"b": "b",
122+
},
123+
"depNamespace": {
124+
"a": "a",
125+
"b": "b",
126+
"default": {
127+
"a": "a",
128+
"b": "b",
129+
},
130+
},
131+
}
132+
`)
133+
})
86134
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.a = 'a'
2+
exports.b = 'b'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.a = 'a'
2+
exports.b = 'b'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import depDefault from './dep1.cjs'
2+
import * as depNamespace from './dep2.cjs'
3+
export { depDefault, depNamespace }

0 commit comments

Comments
 (0)