young-steveo / bottlejs

A powerful dependency injection micro container for JavaScript applications
MIT License
1.29k stars 67 forks source link

Add ES module build #138

Closed blikblum closed 4 years ago

blikblum commented 4 years ago

Fixes #129

Adds an ES module build along side existing one.

Code wise there's no change. I tested in a webpack project and is working fine.

Anyway is a breaking change because webpack users that import bottle with require will break:

import Bottle from 'bottlejs'; // works fine as before

const Bottle = require('bottlejs'); // does not work

// would need to change to 

const Bottle = require('bottlejs').default;

This is far from ideal.

Solutions: 1) Avoid default export and use an named export. Would be consumed as below being consistent for both ES and CJS consumers

// ES import
import { Bottle } from 'bottlejs'; 
// CJS require
const { Bottle } = require('bottlejs');

2) Do not use module entry in package. The users by default would still use the UMD build (no Breaking Change) and to use es module version would need to be explicit:

// ES import
import Bottle from 'bottlejs/dist/bottle-es'; 

Personally i'm in favor of 1 since is a better solution long term but 2 is also doable.

Some context:

https://github.com/markedjs/marked/pull/1571#issuecomment-562891963 https://github.com/webpack/webpack/issues/4742#issuecomment-462789849

young-steveo commented 4 years ago

This is great! thanks. I'll review and test it soon.

blikblum commented 4 years ago

Hi, any updates?

Anything i can do?

young-steveo commented 4 years ago

@blikblum Sorry, I've been on holiday for a long while.

If I understand your comments correctly, this works both before and after your change:

import Bottle from 'bottlejs';

and this works before your change but not after:

const Bottle = require('bottlejs');

I'm fine bumping the major version number of bottle and leaving this the way you've described (I may have some additional breaking changes to make anyway). Let me know if I understand that correctly (I don't use webpack.)

blikblum commented 4 years ago

If I understand your comments correctly,

Yes you are right

To use require would need to be:

const Bottle = require('bottlejs').default;

blikblum commented 4 years ago

Hi, any news when a new release will be done, with ES modules support?

ES modules are required for tools like vite, pika and es-dev server

Let me know if i can help somehow

young-steveo commented 4 years ago

@blikblum https://github.com/young-steveo/bottlejs/releases/tag/v2.0.0 Thanks!

blikblum commented 4 years ago

Many thanks