Skip to content
Open
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
20 changes: 20 additions & 0 deletions packages/plugin-rsc/src/transforms/wrap-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ export class Cls {};
`)
})

test('module without a final newline', async () => {
// Published packages often end with a source map comment and no newline.
const input = `export const Arrow = () => {};\nexport function Fn() {};\n//# sourceMappingURL=index.js.map`
const output = await testTransform(input)
expect(output).toMatchInlineSnapshot(`
"let Arrow = () => {};
function Fn() {};
//# sourceMappingURL=index.js.map
Arrow = /* #__PURE__ */ $$wrap(Arrow, "<id>", "Arrow");
export { Arrow };
Fn = /* #__PURE__ */ $$wrap(Fn, "<id>", "Fn");
export { Fn };
"
`)
const lastLine = (output as string)
.split('\n')
.findLast((line) => line.includes('sourceMappingURL'))
expect(lastLine).toBe('//# sourceMappingURL=index.js.map')
})

test('preserve reference', async () => {
const input = `
export let count = 0;
Expand Down
7 changes: 6 additions & 1 deletion packages/plugin-rsc/src/transforms/wrap-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export function transformWrapExport(
const exportNames: string[] = []
const appendedCode: string[] = []
const filter = options.filter ?? (() => true)
// Code moved to the end of a module that has no final newline would join its
// last line. If that line is a comment (often `//# sourceMappingURL=...`),
// the moved code becomes part of the comment and never runs.
let needsLeadingNewline = !input.endsWith('\n')

/**
* Strips a direct export declaration and emits assignments and exports at
Expand Down Expand Up @@ -113,8 +117,9 @@ export function transformWrapExport(
.filter(Boolean)
.join('') +
`export { ${exports.map((e) => e.localName).join(', ')} };\n`
output.update(start, end, newCode)
output.update(start, end, (needsLeadingNewline ? '\n' : '') + newCode)
output.move(start, end, input.length)
needsLeadingNewline = false
}

/**
Expand Down
Loading