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

without __esModule property when format esm module cause load module error #3399

Closed
zhangyuang opened this issue May 13, 2021 · 13 comments · Fixed by #3718
Closed

without __esModule property when format esm module cause load module error #3399

zhangyuang opened this issue May 13, 2021 · 13 comments · Fixed by #3718
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release

Comments

@zhangyuang
Copy link
Contributor

zhangyuang commented May 13, 2021

Describe the bug

when the thirdparty module is esm format, esbuild manage it without add __esModule property. but webpack will add __esModule property.

for examle, the source code is

// swiper.esm.js
export { default as Swiper, default } from './esm/components/core/core-class';
export { default as Virtual } from './esm/components/virtual/virtual';
//...

After esbuild format ,will output

import {default as default2, default as default3} from "./esm/components/core/core-class";
import {default as default4} from "./esm/components/virtual/virtual";
export {
  default14 as A11y,
  default3 as default
};

without __esModule property.

it will be cause an error. if a module build by webpack, the source code as below

//  react-id-swiper sourceCode
import Swiper from 'swiper';
new Swiper()

after webpack build , output code will add helper function __importDefault

// react-id-swiper
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var swiper_1 = __importDefault(require("swiper"));
new swiper_1.default(swiperNodeRef.current, object_assign_1.default({}, props))

but because of __esModule is undefined, __importDefault function will add default property once again cause the return value __importDefault(require("swiper")).default is not the current object will be error

image
image

Reproduction

https://github.com/zhangyuang/vite-react-swiper-error

System Info

vite/2.3.2 darwin-x64 node-v12.18.3

additional

when i add __esModule property in source code manually, it can be executed succeed like

export { default as Swiper, default } from './esm/components/core/core-class';
export { default as Virtual } from './esm/components/virtual/virtual';
export const __esModule = true 

In this case,maybe vite can add __esModule in esbuild plugin . for example

build.onLoad({ filter: /.*/, namespace: 'swiper' }, ({ path: id }) => {
        return {
          loader: 'js',
          resolveDir: root,
          contents: `export * from "filepath"
          export {default} from "filePath"
          export const __esModule = true 
          `
        }
      })
@Shinigami92 Shinigami92 added bug p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels May 13, 2021
@Sociosarbis
Copy link
Contributor

temporary workaround for your case.

add a resolve.alias in vite.cofnig.js as follows:

{
  resolve: {
    alias: {
     swiper: 'swiper/swiper.cjs'
   }
  }
}

BTW,is possible to achieve this specifc functionality you proposed somehow like add a extra file and use resolve.alias to redirect the request to that. 🤔

@zhangyuang
Copy link
Contributor Author

temporary workaround for your case.

add a resolve.alias in vite.cofnig.js as follows:

{
  resolve: {
    alias: {
     swiper: 'swiper/swiper.cjs'
   }
  }
}

BTW,is possible to achieve this specifc functionality you proposed somehow like add a extra file and use resolve.alias to redirect the request to that. 🤔

yeah, but it's a temporary solution。the problem may be appear frequently

@Sociosarbis
Copy link
Contributor

export default defineConfig({
  plugins: [reactRefresh()],
  resolve: {
    alias: {
      swiper: join(__dirname, 'src/swiper'),
      '@swiper': join(__dirname, 'node_modules/swiper/swiper.esm')
    }
  }
})
// src/swiper.js
export * from '@swiper'
export { default } from '@swiper'
export const __esModule = true

clean the vite prebundle cache and rebuild. This would work. I think it's a issue of esbuild rather than vite's.😄

@zhangyuang
Copy link
Contributor Author

yeah, i also think that. in the final analysis,the problem is es module and commonjs module are not compatible. export default not have corresponding declare in commonjs

@BasixKOR
Copy link

BasixKOR commented May 18, 2021

This causes latest evergreen-ui to break, caused by hyphenate-style-name and @emotion/hash.

@frabarz
Copy link

frabarz commented May 19, 2021

I'm also having this issue with resize-observer-polyfill. The package offers a es module, but the content doesn't have the __esModule = true export, and the app fails because the function is wrapped in an object under a default key:
image
image
This is clearly something they need to fix, but the thing is, this was working correctly 2 weeks ago. Does anyone know since which version this changed?

@zhangyuang
Copy link
Contributor Author

zhangyuang commented May 27, 2021

@antfu @underfin @Shinigami92 @patak-js please see see, the issue occurs many times in different project. for example, use element-ui with vue2.0 and vite

@Shinigami92
Copy link
Member

Does this also happen with Vite v2.2.4? If not, I assume it also has to do with the esbuild update.
But if v2.2.4 works for you, you are at least able to use Vite for now

@zhangyuang
Copy link
Contributor Author

@Shinigami92 it use the newest vite 2.3.4,although i think it's esbuild problem

@Shinigami92
Copy link
Member

Ok so please downgrade and try it out. Then write me feedback.

@zhangyuang
Copy link
Contributor Author

@Shinigami92 vite@2.2.4 will not occur the problem. but we want to use the newest vite.maybe you can push esbuild fix the problem

@softboy99
Copy link

Hi,
does this has been released?

@github-actions
Copy link

This issue has been locked since it has been closed for more than 14 days.

If you have found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Vite version. If you have any other comments you should join the chat at Vite Land or create a new discussion.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 15, 2021
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) regression The issue only appears after a new release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants