Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
(2:12) " first = async () => 'first action called',\n" --> (2:3) " first = async () => 'first action called',\n"
(3:0) " second = first\n" --> (3:0) " second = first\n"
(2:0) "export const first = async () => 'first action called',\n" --> (4:0) "first = /* #__PURE__ */ registerServerReference(first, \"first\");\n"
(2:0) "export const first = async () => 'first action called',\n" --> (5:0) "export { first };\n"
(2:0) "export const first = async () => 'first action called',\n" --> (6:0) "second = /* #__PURE__ */ registerServerReference(second, \"second\");\n"
(2:0) "export const first = async () => 'first action called',\n" --> (7:0) "export { second };\n"
(2:0) "export const first = async () => 'first action called',\n" --> (5:0) "second = /* #__PURE__ */ registerServerReference(second, \"second\");\n"
(2:0) "export const first = async () => 'first action called',\n" --> (6:0) "export { first, second };\n"
```

## module-export-effect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ export const first = async () => 'first action called',

**References:** first, second

[Source map visualization](https://evanw.github.io/source-map-visualization/#MjQ4ACd1c2Ugc2VydmVyJwoKbGV0IGZpcnN0ID0gYXN5bmMgKCkgPT4gJ2ZpcnN0IGFjdGlvbiBjYWxsZWQnLAogIHNlY29uZCA9IGZpcnN0CmZpcnN0ID0gLyogI19fUFVSRV9fICovIHJlZ2lzdGVyU2VydmVyUmVmZXJlbmNlKGZpcnN0LCAiZmlyc3QiKTsKZXhwb3J0IHsgZmlyc3QgfTsKc2Vjb25kID0gLyogI19fUFVSRV9fICovIHJlZ2lzdGVyU2VydmVyUmVmZXJlbmNlKHNlY29uZCwgInNlY29uZCIpOwpleHBvcnQgeyBzZWNvbmQgfTsKMzU4AHsidmVyc2lvbiI6Mywic291cmNlcyI6WyIiXSwic291cmNlc0NvbnRlbnQiOlsiJ3VzZSBzZXJ2ZXInXG5cbmV4c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Q0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxLQUFLLENBQUMsTUFBTSxDQUFDLE1BQU0sQ0FBQztBQUN0RCxDQUFDLENBQUMsTUFBTSxDQUFDLENBQUMsQ0FBQztBQURYO0FBQUE7QUFBQTsifQ==)

```js
'use server'

let first = async () => 'first action called',
second = first
first = /* #__PURE__ */ registerServerReference(first, "first");
export { first };
second = /* #__PURE__ */ registerServerReference(second, "second");
export { second };
export { first, second };
```

## module-export-effect
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-rsc/src/transforms/module-export-scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export function scanModuleExports(
: undefined
return {
node: declarator,
// uniformly handle destructured exports such as
// export const { foo, bar } = ...
// even though associated `meta` doesn't make sense anymore
exports: extractNames(declarator.id).map((name) => ({
localName: name,
exportName: name,
Expand Down
97 changes: 73 additions & 24 deletions packages/plugin-rsc/src/transforms/wrap-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ export const { x, y: [z] } = { x: 0, y: [1] };
"
let { x, y: [z] } = { x: 0, y: [1] };
x = /* #__PURE__ */ $$wrap(x, "<id>", "x");
export { x };
z = /* #__PURE__ */ $$wrap(z, "<id>", "z");
export { z };
export { x, z };
"
`)
})
Expand Down Expand Up @@ -235,8 +234,7 @@ export { a as aa };
a = /* #__PURE__ */ $$wrap(a, "<id>", "a");
export { a };
b = /* #__PURE__ */ $$wrap(b, "<id>", "b");
export { b };
export { b_no };
export { b, b_no };
;
import { c as $$import_c } from "./c";
const $$wrap_$$import_c = /* #__PURE__ */ $$wrap($$import_c, "<id>", "c");
Expand All @@ -247,6 +245,70 @@ export { a as aa };
`)
})

test.each([
`export function action() {}`,
`export const action = () => {}`,
`const action = () => {}; export { action }`,
`export { action } from './dep'`,
`export default () => {}`,
])('does not rewrite fully filtered export groups: %s', async (input) => {
const ast = await parseAstAsync(input)
const result = transformWrapExport(input, ast, {
runtime: (value) => `$$wrap(${value})`,
filter: () => false,
})

expect(result.exportNames).toEqual([])
expect(result.output.hasChanged()).toBe(false)
expect(result.output.toString()).toBe(input)
})

test('preserves filtered export specifiers', async () => {
const input = `\
const selected = 0, skipped = 1
export { selected, skipped as renamed }
export { remote, ignored as forwarded } from './dep'
`
const result = await testTransform(input, {
filter: (name) => name === 'selected' || name === 'remote',
})

expect(result).toMatchInlineSnapshot(`
"const selected = 0, skipped = 1


;
const $$wrap_selected = /* #__PURE__ */ $$wrap(selected, \"<id>\", \"selected\");
export { $$wrap_selected as selected };
export { skipped as renamed };
import { remote as $$import_remote } from './dep';
const $$wrap_$$import_remote = /* #__PURE__ */ $$wrap($$import_remote, \"<id>\", \"remote\");
export { $$wrap_$$import_remote as remote };
export { ignored as forwarded } from './dep';
"
`)
})

test('rejects selected uninitialized variables', async () => {
const input = `export let selected, skipped`
const ast = await parseAstAsync(input)

expect(() =>
transformWrapExport(input, ast, {
runtime: (value) => `$$wrap(${value})`,
rejectNonAsyncFunction: true,
filter: (name) => name === 'selected',
}),
).toThrow('unsupported non async function')

const filtered = transformWrapExport(input, ast, {
runtime: (value) => `$$wrap(${value})`,
rejectNonAsyncFunction: true,
filter: () => false,
})
expect(filtered.output.hasChanged()).toBe(false)
})

test('filter meta', async () => {
const input = `
export const a = 0;
Expand All @@ -259,11 +321,10 @@ export default function d() {}
})
expect(result).toMatchInlineSnapshot(`
"
let a = 0;
export const a = 0;
let b = function() {}
let c = () => {}
function d() {}
export { a };
b = /* #__PURE__ */ $$wrap(b, "<id>", "b");
export { b };
c = /* #__PURE__ */ $$wrap(c, "<id>", "c");
Expand All @@ -282,13 +343,7 @@ export default () => {}
const result = await testTransform(input, {
filter: (_name, meta) => !!(meta.isFunction && meta.declName),
})
expect(result).toMatchInlineSnapshot(`
"
const $$default = () => {}
;
export { $$default as default };
"
`)
expect(result).toMatchInlineSnapshot(`false`)
})

test('filter defaultExportIdentifierName', async () => {
Expand Down Expand Up @@ -329,12 +384,10 @@ export const tags = []
expect(result.exportNames).toEqual(['action'])
expect(result.output.toString()).toMatchInlineSnapshot(`
"let action = async () => {}
let metadata = {}
let tags = []
export const metadata = {}
export const tags = []
action = /* #__PURE__ */ $$wrap(action, "action");
export { action };
export { metadata };
export { tags };
"
`)
})
Expand All @@ -353,9 +406,8 @@ export default async function Page() {}
expect(result.exportNames).toEqual(['default'])
expect(result.output.toString()).toMatchInlineSnapshot(`
"
let revalidate = 1;
export const revalidate = 1;
async function Page() {}
export { revalidate };
;
const $$wrap_Page = /* #__PURE__ */ $$wrap(Page, "default");
export { $$wrap_Page as default };
Expand All @@ -372,11 +424,8 @@ export default async function Page() {}
filter: () => false,
})
expect(result.exportNames).toEqual([])
expect(result.output.toString()).toMatchInlineSnapshot(`
"const $$default = 1;;
export { $$default as default };
"
`)
expect(result.output.hasChanged()).toBe(false)
expect(result.output.toString()).toBe(input)
})

test('unknown identifier exports remain eligible for wrapping', async () => {
Expand Down
Loading
Loading