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

ssrLoadModule executes code in non-strict mode #9197

Closed
7 tasks done
Rich-Harris opened this issue Jul 18, 2022 · 2 comments · Fixed by #9199
Closed
7 tasks done

ssrLoadModule executes code in non-strict mode #9197

Rich-Harris opened this issue Jul 18, 2022 · 2 comments · Fixed by #9199
Labels
feat: ssr p3-minor-bug 🔨 An edge case that only affects very specific usage (priority)

Comments

@Rich-Harris
Copy link
Contributor

Rich-Harris commented Jul 18, 2022

Describe the bug

A module like this...

foo = 1;
console.log(foo);

...should fail, because foo is not declared anywhere, and modules always run in strict mode. This works correctly in an app built by Vite. During development, however, the module is interpreted as a non-strict function, and the code above runs.

Reproduction

https://github.com/Rich-Harris/vite-ssrloadmodule-strict-repro

System Info

System:
    OS: macOS 12.0.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 510.66 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.15.1/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
  Browsers:
    Chrome: 103.0.5060.114
    Chrome Canary: 105.0.5187.0
    Firefox: 102.0.1
    Safari: 15.1
  npmPackages:
    vite: ^3.0.2 => 3.0.2

Used Package Manager

pnpm

Logs

No response

Validations

@Rich-Harris
Copy link
Contributor Author

Rich-Harris commented Jul 18, 2022

Changing this line...

result.code + `\n//# sourceURL=${mod.url}`
...fixes it:

    const AsyncFunction = async function () {}.constructor as typeof Function
    const initModule = new AsyncFunction(
      `global`,
      ssrModuleExportsKey,
      ssrImportMetaKey,
      ssrImportKey,
      ssrDynamicImportKey,
      ssrExportAllKey,
-      result.code + `\n//# sourceURL=${mod.url}`
+      '"use strict";\n' + result.code + `\n//# sourceURL=${mod.url}`
    )

@patak-dev
Copy link
Member

patak-dev commented Jul 18, 2022

LGTM. @Rich-Harris if you don't mind I'll do the PR to change that line as we can run vite-ecosystem-ci if the PR branch is in the repo. If no ecosystem CI complaints I think it is safe to get this one in a patch

@patak-dev patak-dev added p3-minor-bug 🔨 An edge case that only affects very specific usage (priority) feat: ssr and removed pending triage labels Jul 18, 2022
@patak-dev patak-dev linked a pull request Jul 18, 2022 that will close this issue
4 tasks
@github-actions github-actions bot locked and limited conversation to collaborators Aug 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: ssr p3-minor-bug 🔨 An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants