Receive "Uncaught ReferenceError: _sfc_main is not defined" when using "export" and "default" strings in one line #10796
Labels
bug: upstream
Bug in a dependency of Vite
p3-minor-bug
An edge case that only affects very specific usage (priority)
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:
<script lang="ts"> export const foo = 'custom'; </script>./components/MyComponent.vue file:
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
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: