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

HMR error: Cannot access '...' before initialization (happend on <script setup lang="ts"> ..... </script>) #4430

Closed
6 tasks done
NomadBin opened this issue Jul 29, 2021 · 5 comments

Comments

@NomadBin
Copy link

Describe the bug

  1. Under the code, the console error " Uncaught ReferenceError: Cannot access 'MAIN_FILE' before initialization
    at store.ts:4 " with first open browser.
// App.vue
<script setup lang="ts">
import {MAIN_FILE} from "./compile"
</script>
// compile.ts
import { store } from "./store";

export const MAIN_FILE = "index.vdest";
console.log(store)
// store.ts
import { MAIN_FILE } from "./compile";
console.log(MAIN_FILE)
export const store = null
  1. But when i remove the script tag of setup, that was no problems。 So, I am not sure it is vite or vue3 bug!

Reproduction

https:/china-bin/vite-script-setup-ts-import-bug

System Info

System:
    OS: Windows 10 10.0.19043
    CPU: (8) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
    Memory: 4.17 GB / 15.96 GB
  Binaries:
    Node: 14.17.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - C:\Program Files\nodejs\yarn.CMD
    npm: 6.14.13 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    @vitejs/plugin-vue: ^1.3.0 => 1.3.0
    vite: ^2.4.4 => 2.4.4
    vue: ^3.0.5 => 3.1.5

Used Package Manager

yarn

Logs

No response

Validations

@arpowers
Copy link
Contributor

Same issue here. Seems like it showed up after upgrading deps.

Screen Shot 2021-07-29 at 3 40 47 PM

@dejour
Copy link
Contributor

dejour commented Aug 2, 2021

you could fix this by removing the circular dependency.

@NomadBin
Copy link
Author

NomadBin commented Aug 6, 2021

you could fix this by removing the circular dependency.

yeah! Remoing the circualr dependency will be right. But I have a question:Is the browser es6 import cause no circular dependency?

@patak-dev
Copy link
Member

patak-dev commented Sep 2, 2021

@china-bin @arpowers would you check if this PR fixes your issues? #4589

@yyx990803
Copy link
Member

This is not a Vite bug. ESM does support circular dependency, but it still has limitations - you can't eagerly access imported bindings of a file that's later in the import chain:

main -> compile -> store -> compile

Here store will not be able to use any binding from compile eagerly - it can only do so in a lazy-invoked function. This is how ESM works, and has nothing to do with Vite HMR.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants