Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

[Discussion] Better dist format #22

Closed radex closed 6 years ago

radex commented 6 years ago

Currently, we build and output Watermelon in two formats: ESM and CJS. If I understand correctly (@mobily knows this), it's because React Native needs CJS, but Webpack can do tree shaking with ESM. In #19, we add another format (same as source but with rewritten imports) to support Flow.

This complicates things because users have to include a Babel plugin to rewrite @nozbe/watermelon/xxx imports to @nozbe/watermelondb/{esm,cjs}/xxx.

  1. Do we really need ESM for tree shaking? Any 🍉 user is almost surely going to be using almost all features, so I can't see how tree shaking could help. And all potentially tree-shakable things like adapters already require a separate import (import from '@nozbe/watermelondb/adapters') and are not exported from the main import
  2. Can we just use CJS for all platforms and remove the need for a babel plugin?
  3. We could still write the source code (if we fix #20) in dist, so that an advanced user could set up Babel, tree shaking, whatever they need in their app (We're probably going to do this at Nozbe, since we don't need e.g. class transpilation for React Native)
satya164 commented 6 years ago

it's because React Native needs CJS

React Native doesn't need CJS. ESM will work fine with React Native.

Do we really need ESM for tree shaking

Yup.

radex commented 6 years ago

React Native doesn't need CJS. ESM will work fine with React Native

Hmm 🤔 What modern targets require CJS then? Is it just a Node.js thing? (🍉 doesn't have adapters anyway to make running on Node useful)

satya164 commented 6 years ago

AFAIK all bundlers (Webpack, Parcel, Rollup) support ESM, so it must be just NodeJS. The only thing I can think of is running it in Node for testing, but then you could use @std/esm or babel for that. But probably who put it there will have more insight :D