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

Pre-bundling questions #1724

Closed
2 tasks done
anncwb opened this issue Jan 26, 2021 · 3 comments
Closed
2 tasks done

Pre-bundling questions #1724

anncwb opened this issue Jan 26, 2021 · 3 comments

Comments

@anncwb
Copy link
Contributor

anncwb commented Jan 26, 2021

⚠️ IMPORTANT ⚠️ Please check the following list before proceeding. If you ignore this issue template, your issue will be directly closed.

  • Read the docs.
  • Use Vite >=2.0. (1.x is no longer supported)

Describe the bug

In beta.36, import moment. It is a function

import moment from 'moment'

console.log(moment)

=>

ƒ hooks() {
 return hookCallback.apply(null, arguments);
 }

In beta.49, import moment. It is a Object

import moment from 'moment'

console.log(moment)

=>

 {__esModule: true}
default: (...)
__esModule: true
get default: () => moment_default
__proto__: Object
}

Is this expected or a bug?Why the two results are inconsistent

Reproduction

System Info

  • vite version: bete.49
  • Operating System: mac
  • Node version: 12
  • Package manager (npm/yarn/pnpm) and version:yarn
@yyx990803
Copy link
Member

Sigh... this is really messed up:

  • moment defines jsnext:main which points to a ESM file (dist/moment.js)
  • Inside the ESM file, it uses some weird hack: var aliasedRequire = require then calls that aliasedRequire as if it would work in ESM... (it probably works in webpack 🤷 )
  • esbuild is a bit too smart and notices that moment is actually calling require even when it's aliased, so it still wraps the moment ESM build as a CommonJS module
  • However since moment declares that its entry is ESM (and does have export default in it), Vite thinks it does not need CommonJS interop.

@anncwb
Copy link
Contributor Author

anncwb commented Jan 26, 2021

thank

@techird
Copy link

techird commented Mar 29, 2021

Provide a temperary workaround here:

  1. Alias moment to the hooked version:
// vite.config.js

export default {
  // other config
  resolve: {
    alias: [
      // make moment to be a resolvable ESM
      { find: /^moment$/, replacement: path.resolve(__dirname, './alias/moment.js') },
    ],
  },
}
  1. Implement the hooked moment.js
// absolute path to moment in node_modules
import moment from '../../node_modules/moment';

export const {
  fn,
  min,
  max,
  now,
  utc,
  unix,
  months,
  isDate,
  locale,
  invalid,
  duration,
  isMoment,
  weekdays,
  parseZone,
  localeData,
  isDuration,
  monthsShort,
  weekdaysMin,
  defineLocale,
  updateLocale,
  locales,
  weekdaysShort,
  normalizeUnits,
  relativeTimeRounding,
  relativeTimeThreshold,
  calendarFormat,
} = moment;

export default moment;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants