Skip to content
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

SSR: References to named default export functions get out of scope #4049

Closed
6 tasks done
GrygrFlzr opened this issue Jun 30, 2021 · 0 comments · Fixed by #4053
Closed
6 tasks done

SSR: References to named default export functions get out of scope #4049

GrygrFlzr opened this issue Jun 30, 2021 · 0 comments · Fixed by #4053
Labels
feat: ssr p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)

Comments

@GrygrFlzr
Copy link
Member

Describe the bug

When any code meets all of the following criteria:

  • Is loaded using ssrLoadModule
  • Has a default named export
  • Has a reference to the above name in the same file

ssrLoadModule will incorrectly treat it as an anonymous function, preventing the rest of the code from accessing it.

This code style is used in various dependencies such as d3-format in order to modify function prototypes.

For example:

export default function hello() {
  return 1234;
}

console.log(typeof hello);
// returns undefined

hello.test = 1234;
// [vite] Error when evaluating SSR module
// ReferenceError: hello is not defined

As a sanity check, I have confirmed that:

  • The above code executes as expected when directly run from node
  • The default export is properly named when splitting the export from the function definition:
    function hello() { /* ... */ }
    export default hello;
    console.log(typeof hello);
    // returns function
  • Named exports are not affected
    export function hello() { /* ... */ }
    console.log(typeof hello);
    // returns function

This is because of the ssrTransform step doesn't look for references to the default export, it only replaces export default with __vite_ssr_exports__.default =:

// default export
if (node.type === 'ExportDefaultDeclaration') {
s.overwrite(
node.start,
node.start + 14,
`${ssrModuleExportsKey}.default =`
)
}

Producing the following transformed code:

__vite_ssr_exports__.default = function hello() {
        return 1234;
}

console.log(typeof hello);
hello.test=1234;

Which now makes hello out of scope.

Reproduction

https://github.com/GrygrFlzr/vite-ssr-default

You can see the logs from CI runs:

System Info

System:
    OS: Windows 10 10.0.22000
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
    Memory: 14.00 GB / 31.93 GB
  Binaries:
    Node: 14.16.0 - C:\program files\nodejs\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.13 - C:\program files\nodejs\npm.CMD
  Browsers:
    Chrome: 91.0.4472.124
    Edge: Spartan (44.22000.1.0), Chromium (91.0.864.59)
    Internet Explorer: 11.0.22000.1
  npmPackages:
    vite: ^2.4.0-beta.2 => 2.4.0-beta.2

Used Package Manager

pnpm

Logs

> vite-ssr-default@1.0.0 dev /home/runner/work/vite-ssr-default/vite-ssr-default
> node server

The variable "app" is of type undefined
9:46:16 AM [vite] Error when evaluating SSR module /src/app.js:
ReferenceError: app is not defined
    at /src/app.js:6:1
    at instantiateModule (/home/runner/work/vite-ssr-default/vite-ssr-default/node_modules/.pnpm/vite@2.4.0-beta.2/node_modules/vite/dist/node/chunks/dep-06e8fbb4.js:72663:166)
ReferenceError: app is not defined
    at /src/app.js:6:1
    at instantiateModule (/home/runner/work/vite-ssr-default/vite-ssr-default/node_modules/.pnpm/vite@2.4.0-beta.2/node_modules/vite/dist/node/chunks/dep-06e8fbb4.js:72663:166)

Validations

@GrygrFlzr GrygrFlzr added bug p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) feat: ssr labels Jun 30, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: ssr p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant