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

Plugin built by esbuild cannot be loaded in vite.config.ts #12276

Closed
7 tasks done
guoyunhe opened this issue Mar 3, 2023 · 3 comments
Closed
7 tasks done

Plugin built by esbuild cannot be loaded in vite.config.ts #12276

guoyunhe opened this issue Mar 3, 2023 · 3 comments
Labels
invalid This doesn't seem right

Comments

@guoyunhe
Copy link

guoyunhe commented Mar 3, 2023

Describe the bug

I write a plugin for Vite, using a default export syntax export default myPlugin.
And I built it with esbuild. See plugin folder in the reproduction sandbox.

Then I load the plugin into my vite.config.js and run vite build. I got error:

Error: import_plugin.default is not a function

Reproduction

https://stackblitz.com/edit/vitejs-vite-k7eufn

Steps to reproduce

No response

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (6) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @vitejs/plugin-react: ^3.1.0 => 3.1.0 
    vite: ^4.1.4 => 4.1.4

Used Package Manager

npm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Mar 3, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@fi3ework
Copy link
Member

fi3ework commented Mar 4, 2023

There's an extra (for this case) default wrapper when transpiling from ESM to CJS. It's by-designed for esbuild for now, see evanw/esbuild#2026 (comment). You can remove the type: module or use a named export instead or ship a pure ESM/dual pkg.

@sapphi-red
Copy link
Member

Vite's config works like node.js.

If you import your plugin with node.js like this, you can see it's not what you expect.

import foo from './plugin/dist/index.js'

console.log(foo)
console.log(foo.default)

Closing as this is not a bug in Vite.

@sapphi-red sapphi-red added invalid This doesn't seem right and removed pending triage labels Mar 11, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Mar 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants