Recently I wrote a small library for fun, which implements styled-components pattern for CSS Modules. I had a dilemma: how should I publish it, should I compile it down to ES5 and CJS or UMD or publish more than one version at once, which tool to use for it. Previously I tried kcd-scripts
(by Kent C. Dodds) and microbundle
(by Jason Miller).
This time I thought it would be a way to much trouble for a toy project. So I went ahead and published it as ES6 (source code as is without minification or anything), but with node-style module resolution.
Strictly speaking, it is not consumable by the browser, because of node-style module resolution. It is not consumable by node, because node support of ES6 modules is behind the flag. It is only consumable by bundlers, like webpack (CRA for example) and Parcel (actually I haven't tested it).
In my case, I have external dependency (React), but for example, polished doesn't have external dependencies.
What are the downsides of publishing ES6? What your approach here? Tell me your thoughts
Photo by chuttersnap on Unsplash
Top comments (8)
No you cant - there is no such thing as
ES6
.What you can - you can publish as
esmodules
. Theseesmodules
are not "ES Modules", akaimport/export
, but a special Babel target which targets some abstract browser, which does support these "ES Modules"(๐คทโโ๏ธ), plus stuff like async/await and generators(๐ฅ), but not class members for example.So - you can publish code in
esmodules
-import/export
and language transpiled down to some "well-known" target. But any "special" babel plugin (or typescript) should do their job and disappear.After publishing module that way you will have two problems:
jest-transform
for Jest. Drawback - first run, yet without a cache, might be very slow.So - as long as you are removing any "vendor specific" stuff(like stage-1 language features, or babel-plugins) from the package code - it would be ok ๐.
This one may solve the problem github.com/WICG/import-maps#the-ba...
I believe there is no problem publishing ES6 as you call it on NPM. I post stuff all the time that uses ES10 or Electron specific features. Besides, NPM was never browser-only anyway. Lots of stuff needs to be compiled before even using it.
The most important thing is that your package adds value and that it contains a readme.md so people at least know how and in what context to use it.
Here's a resource for npm + modules: pika.dev/cdn.
It's a CDN that serves npm modules as es6 modules! I haven't tried it out, but it seems pretty cool.
So I tried it. Indeed it solves the problem. My module is ES6 (unminified, with node-style module resolution), and it works in the browser as is.
Pika CDN transpiled it for me and changed
import React from"react"
toimport React from"https://cdn.pika.dev/_/react/v16"
๐that is really cool. Thanks for trying it out and posting an update!
Yeah, I want to look into this one. Need some free time to read through
More discussion about the subject