Sunday, 7 October 2018

How to create a library that can have individual components imported if required à la lodash

I've written a library in es6 using import/export. I can bundle this library using Rollup into an iife that can be used in the browser.

I also want to be able to use this library in other projects. However I won't usually want to include the whole library, only parts of it.

Because the library is written using es6 import/export I can include the unbundled index.js file as a dependency in another project and then import { myFunc } from 'my-lib' and this works great - I only get myFunc when my project is bundled.

However I ran into an issue because these files haven't been processed by babel and therefore contain es6 code such as arrow functions. I have read that if you're going to publish a library it shouldn't need to be transpiled by the end user.

How can I take my es6 library and bundle it in such a way that it is transpiled, but also able to have its individual components imported? I want a similar situation to how lodash is organised.

This isn't the easiest thing to articulate so if anything isn't clear please leave a comment and I'll edit my question to try and clarify.



from How to create a library that can have individual components imported if required à la lodash

No comments:

Post a Comment