Skip to content

Commit 3a3734a

Browse files
committed
fix: reverse prepend
1 parent f1842b4 commit 3a3734a

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ if (true) {
5757
`
5858
expect(await testTransform(input)).toMatchInlineSnapshot(`
5959
"const exports = {}; const module = { exports };
60-
const __cjs_to_esm_hoist_1 = await import("react-dom");
6160
const __cjs_to_esm_hoist_0 = await import("react");
61+
const __cjs_to_esm_hoist_1 = await import("react-dom");
6262
"production" !== process.env.NODE_ENV && (function() {
6363
var React = __cjs_to_esm_hoist_0;
6464
var ReactDOM = __cjs_to_esm_hoist_1;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export function transformCjsToEsm(
1010
const output = new MagicString(code)
1111
const analyzed = analyze(ast)
1212

13-
let parentNodes: Node[] = []
13+
const parentNodes: Node[] = []
14+
const hoistedCodes: string[] = []
1415
let hoistIndex = 0
1516
walk(ast, {
1617
enter(node) {
@@ -49,7 +50,7 @@ export function transformCjsToEsm(
4950
node.arguments[0]!.start,
5051
node.arguments[0]!.end,
5152
)
52-
output.prepend(`const ${hoisted} = await import(${importee});\n`)
53+
hoistedCodes.push(`const ${hoisted} = await import(${importee});\n`)
5354
output.update(node.start, node.end, hoisted)
5455
hoistIndex++
5556
}
@@ -59,6 +60,9 @@ export function transformCjsToEsm(
5960
parentNodes.pop()!
6061
},
6162
})
63+
for (const hoisted of hoistedCodes.reverse()) {
64+
output.prepend(hoisted)
65+
}
6266
output.prepend(`const exports = {}; const module = { exports };\n`)
6367
return { output }
6468
}

0 commit comments

Comments
 (0)