-
Star
(120)
You must be signed in to star a gist -
Fork
(13)
You must be signed in to fork a gist
-
-
Save jayphelps/51bafb4505558736fdba0aaf8bfe69d3 to your computer and use it in GitHub Desktop.
| { | |
| "scripts": { | |
| "build": "npm run build:es2015 && npm run build:esm && npm run build:cjs && npm run build:umd && npm run build:umd:min", | |
| "build:es2015": "tsc --module es2015 --target es2015 --outDir dist/es2015", | |
| "build:esm": "tsc --module es2015 --target es5 --outDir dist/esm", | |
| "build:cjs": "tsc --module commonjs --target es5 --outDir dist/cjs", | |
| "build:umd": "rollup dist/esm/index.js --format umd --name YourLibrary --sourceMap --output dist/umd/yourlibrary.js", | |
| "build:umd:min": "cd dist/umd && uglifyjs --compress --mangle --source-map --screw-ie8 --comments --o yourlibrary.min.js -- yourlibrary.js && gzip yourlibrary.min.js -c > yourlibrary.min.js.gz", | |
| } | |
| } |
I find it worth mentioning that this scripts is dependent on some packages:
yarn add -D rollup uglify-jsAlso, you obviously gonna need to rename sections like /yourlibrary.js and --name TextEncoding to match your package name preferences.
If you trying to run this in a project that has folder imports (... from './module';) you will need to add the -p @rollup/plugin-node-resolve param to the build:umd script and install the plugin so it can be available yarn add -D @rollup/plugin-node-resolve.
I also find a "typo" in the uglifyjs flag --o, it shoul be -o. This was causing a STDOUT error when trying to GZIP because uglifyjs was simply not writing to a file, but to the console and took me some time to find out. Oh, and almost forgot, I had to remove --source-map from build:umd:min otherwise it wouldn't work, still can't figure out why.
Thanks @jayphelps for this awesome source. Good luck to future users!
For reference here's my devDependencies with all working:
{
"@rollup/plugin-node-resolve": "^7.1.3",
"rollup": "^2.8.2",
"uglifyjs": "^2.4.11"
}@jayphelps
I notice you are calling tsc 3 times - that's 3 times the TS type checker is being invoked, and doing exactly the same thing each time (because the different parameters you passed only apply to the output after the type-checking [.... I believe]). Type checking is what takes time for tsc.
Alternately you could pass through the type checker with tsc just once to produce ESM, and pass that ESM result through rollup to produce ESM and CJS versions (or whatever else) which would include all that great tree shaking functionality which tsc doesn't do.
Disclaimer - I'm in the middle of trying to that now and was looking around for references which is how I got here - haven't completed it yet.
@jdalton
Good point. so there is no choice but to output both CJS and ESM. (Which is what @jayphelps seems to be doing).
CJS for users who are writing CommonJS apps and don't wan't to go through the trouble of converting ESM->CSJ themselves.
ESM for just about any other use.
Just an update that Node v12 reworked the experimental modules support, and expects to drop the flag in October 2019.
Also, if anyone wants to reply here, please keep in mind that Gists still don't send notifications for comments.