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

TypeError: Preview is not a function #16

Open
Loongphy opened this issue Feb 10, 2023 · 10 comments · Fixed by #18
Open

TypeError: Preview is not a function #16

Loongphy opened this issue Feb 10, 2023 · 10 comments · Fixed by #18
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@Loongphy
Copy link

Project created by pnpm create vite my-vue-app --template vue-ts

"dependencies": {
  "vue": "^3.2.45"
},
"devDependencies": {
  "@vitejs/plugin-vue": "^4.0.0",
  "typescript": "^4.9.3",
  "vite": "^4.1.0",
  "vite-plugin-vue-component-preview": "^1.0.2",
  "vue-tsc": "^1.0.24"
}

In vite.config.ts, Preview() got error TypeError: Preview is not a function, using Preview.default() works fine but got typescript error Property 'default' does not exist on type '() => Plugin_2'.

@Take-Me1010
Copy link
Contributor

Take-Me1010 commented Feb 14, 2023

I got the same error in the project geneareted by yarn, in windows 10.

Reproduction:

  1. create a new project by yarn create vite and select Vue.js and TypeScript.
  2. add the plugin via yarn add -D vite-plugin-vue-component-preview, its version is 1.0.2
  3. setup based on the README.md

Then I see the terminal named volar-preview:3333

node "c:\\Users\\takeMe\\.vscode\\extensions\\vue.volar-1.0.24-win32-x64\\dist\\preview-bin\\vite.js" --port=3333
failed to load config from C:\Users\\takeMe\programing\vscodeExtension\vite-preview-test\node_modules\.volar\vite.config.ts
error when starting dev server:
TypeError: Preview is not a function
    ...

Actually the impoted plugin is { default: [Function: default_1] } which can be seen as follows:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import preview from "vite-plugin-vue-component-preview"
console.debug(preview)
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue(), preview()],
})

By replacing preview() with preview.default(), the error disappear (but VSCode warns Property 'default' does not exist on type '() => Plugin_2'.).
However I can't get the expected result with the below error messages:

Sourcemap for "C:/path_to/vite-preview-test/node_modules/vite/dist/client/client.mjs" points to missing source files
Sourcemap for "C:/path_to/vite-preview-test/node_modules/vite/dist/client/env.mjs" points to missing source files
Failed to load url assets/style/common.css (resolved id: assets/style/common.css). Does the file exist?
Failed to load url assets/style/form.css (resolved id: assets/style/form.css). Does the file exist?

@johnsoncodehk
Copy link
Owner

I can't reproduce this problem with a new project create via pnpm create vite my-vue-app --template vue-ts, can you provide minimal reproduction?

@Loongphy
Copy link
Author

I can't reproduce this problem with a new project create via pnpm create vite my-vue-app --template vue-ts, can you provide minimal reproduction?

https://github.com/Loongphy/my-vue-app
image

@johnsoncodehk
Copy link
Owner

@johnsoncodehk johnsoncodehk added bug Something isn't working help wanted Extra attention is needed labels Feb 17, 2023
@Take-Me1010
Copy link
Contributor

About the TypeError problem, How about changing configures like below?

  1. add "type": "module", in package.json
{
 	"type": "module",
}
  1. change target and module in tsconfig.json, like https://github.com/vitejs/vite-plugin-vue/blob/main/packages/plugin-vue/tsconfig.json
{
	"compilerOptions": {
		"target": "ES2020",
		"module": "ES2020",
        },
}

The js files generated by pnpm tsc seem to work well in my environment.
Maybe it is better to use some transpiler like babel. But it is not exactly sure because I am not used to developing npm packages :(

@zssng
Copy link

zssng commented Feb 17, 2023

import Preview from 'vite-plugin-vue-component-preview'

const PreviewFunc=(Preview as any).default

export default defineConfig({
    plugins: [
	    PreviewFunc()
    ],
})

works for me

@johnsoncodehk
Copy link
Owner

Please try v1.1.0

@johnsoncodehk
Copy link
Owner

Re-opened as the relevant changes have been reverted as it broke Vue and Nuxt Preview, maybe have to build .mjs + .cjs together.

@johnsoncodehk johnsoncodehk reopened this Feb 19, 2023
@Take-Me1010
Copy link
Contributor

vitejs/vite#8178
the idea of CJS Proxy, which is used in vite, seems one of the best solutions.

@salomaosnff
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants