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

Receive "Uncaught ReferenceError: _sfc_main is not defined" when using "export" and "default" strings in one line #10796

Closed
7 tasks done
klyuevtech opened this issue Nov 5, 2022 · 3 comments
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@klyuevtech
Copy link

klyuevtech commented Nov 5, 2022

Describe the bug

I receive error message in browser:
Uncaught ReferenceError: _sfc_main is not defined

when I try to use component like this:

./components/MyComponent.vue file:

<script lang="ts"> export const foo = 'default'; </script>

in App.vue file:

<script setup lang="ts"> import { foo } from './components/MyComponent.vue' const bar = foo; </script>

But when I replace 'default' word in ./components/MyComponent.vue file with anything else, for example:
./components/MyComponent.vue file:

<script lang="ts"> export const foo = 'custom'; </script>

Then all is compiled properly and I don't receive any error.

This is happening due to this reg ex in node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)(?:as)?(\s*)default/s;

It gives false positives when line contains any 'default' sub-string in it.

Actually I am not sure if it is vite bug or vue one, because error goes from vite code: it does not add "const _sfc_main = {}" to compiled file in genScriptCode function of node_modules/@vitejs/plugin-vue/dist/index.cjs file, but it seems the root of the problem goes from vue script. If I should re-post this issue to vue team please let me know.

Reproduction

https://stackblitz.com/edit/vitejs-vite-xeg2wy?file=src%2Fcomponents%2FMyComponent.vue&terminal=dev

Steps to reproduce

% npm create vite@latest vitejs-vite-jtwu7b-- --template vue

% cd vitejs-vite-jtwu7b && npm i

% touch ./src/components/MyComponent.vue

% echo '<script lang="ts">
export const foo = "default";
</script>' > ./src/components/MyComponent.vue

% echo '<script setup lang="ts">
import { foo } from "./components/MyComponent.vue"
const bar = foo;
</script>' > ./src/App.vue;

% npm run dev

Now check web browser

System Info

System:
    OS: macOS 13.0
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 11.93 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.13.2 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.19.3 - /usr/local/bin/npm
  Browsers:
    Chrome: 107.0.5304.87
    Firefox: 106.0.5
    Safari: 16.1
  npmPackages:
    @vitejs/plugin-vue: ^3.2.0 => 3.2.0 
    vite: ^3.2.0 => 3.2.2

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red sapphi-red added plugin: vue p3-minor-bug An edge case that only affects very specific usage (priority) labels Nov 6, 2022
@sapphi-red
Copy link
Member

Since rewriteDefault function is said that it rewrites export default value into a variable value, I think this is a bug in Vue side.

scriptCode = options.compiler.rewriteDefault(
script.content,
'_sfc_main',
[...defaultPlugins, ...userPlugins]
)

https://github.com/vuejs/core/blob/81a7819535c4382ba7817c817722bac6d41921d8/packages/compiler-sfc/src/rewriteDefault.ts#L9-L12

Would you create a issue to Vue?

@sapphi-red sapphi-red added the bug: upstream Bug in a dependency of Vite label Nov 6, 2022
@klyuevtech
Copy link
Author

Hi @sapphi-red ,
Sure, I have re-posted it to vue maintenance team:
vuejs/core#7038

Would you create a issue to Vue?

@sapphi-red
Copy link
Member

Thanks! Closing this one as the fix is coming on their side.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

2 participants