caoxiemeihao / vite-plugins

🌱 为社区尽一份绵薄之力
MIT License
65 stars 15 forks source link

vite-plugin-esmodule with file-type #9

Closed nosferatu500 closed 2 years ago

nosferatu500 commented 2 years ago

Hello, thank you for this plugin.

It's works smoothly with execa but I have one issue with another esm package;

I'm trying to use it with file-type but I'm getting an error "getDefaultExportFromCjs is not a function"

What can be a problem?

caoxiemeihao commented 2 years ago

In Vite, the module exports are loaded by default Browser module
I tried to change the default behavior by configuration, such as

esmodule([
  { 'file-type': 'file-type/index.js' },
])

However, there is a problem with the operation of the code packaged with Vite or esbuild, and I am trying to solve this problem with Webpack

caoxiemeihao commented 2 years ago

I tried webpack and it works normally.
I am rewriting the core logic part, and the next version will support webpack built.
like this, e.g.

esmodule([
  { 'file-type': 'file-type/index.js' },
], {
  webpack: (config) => config, // support custom webpack config, if you choose it.
  // or
  vite: (config) => config, // support custom vite config, if you choose it
})

I'll fix it soon. Just a moment, please. 😄

nosferatu500 commented 2 years ago

Thank you!

caoxiemeihao commented 2 years ago

Hey!
vite-plugin-esmodule@1.1.0 has been released
You can configure blow options in your vite.config.js file for resolve the BUG

esmodule([
  'file-type',
  // or
  // { 'file-type': 'file-type/index.js' },
  // When webpack is used, the `exports.node` filed will be hit first
], {
  webpack: true,
})
nosferatu500 commented 2 years ago

Hi! Thank you for this update.

But I just checked it and I still have the same issue. I tried to use both 'file-type' and { 'file-type': 'file-type/index.js' } :(

nosferatu500 commented 2 years ago

Case 1: plugins: [esmodule(["execa", "file-type"], { webpack: true })], Error: getDefaultExportFromCjs is not a function

Case 2: plugins: [esmodule(["execa", { "file-type": "file-type/index.js" }], { webpack: true })], Error: [vite:load-fallback] Could not load /Users/***/node_modules/.vite-plugin-esmodule/execa (imported by electron/electron.ts): EISDIR: illegal operation on a directory, read

caoxiemeihao commented 2 years ago

vite-plugin-esmodule@1.1.2 fix the BUG 😅

You can see the demo file-type.ts

nosferatu500 commented 2 years ago

With v1.1.2 I still had some issues, but with v1.2+ everything looks fine. Thanks!

caoxiemeihao commented 2 years ago

As time goes on, everything will be all right 😄