An Interest In:
Web News this Week
- April 12, 2024
- April 11, 2024
- April 10, 2024
- April 9, 2024
- April 8, 2024
- April 7, 2024
- April 6, 2024
February 23, 2020 10:28 am GMT
Original Link: https://dev.to/patarapolw/what-s-your-way-of-publishing-node-js-library-for-the-browser-also-introducing-rollup-4i5h
What's your way of publishing Node.js library for the browser? (also, introducing Rollup)
- Do you attach to
window
object, or is there a better alternative?- How many namespaces do you usually attach?
- How do you maintain both browser version and bundler version?
- Do you require your library users to use a bundler or Node.js, for simplicity's sake?
- If you bundle for library users, which bundler do you use?
- How many percentage of browsers do you support? Which ES version?
BTW, I have just started using Rollup to bundle TypeScript for <script type="module">
, so that I don't have to attach to window
object.
- Webpack just couldn't target ESM
- Bundled ESM might also be usable in Deno.
- Output of non-minified Rollup is readable. Webpack is not, and is full of eval. (Even minified version of Rollup is relative readable as well.)
tsconfig.json
uses"module": "commonjs"
(so that I can runts-node
,ts-mocha
and publish for Node, without esm), butrollup.config.js
usesesnext
.- I also use
tsc
to build for Node. Rollup just cannot properly do it. And Rollup cannot generatedeclaration
anddeclarationMap
- I also use
// rollup.config.jsimport typescript from '@rollup/plugin-typescript'import minify from 'rollup-plugin-babel-minify'const getConfig = ({ output, isMinify }) => { return { input: 'src/index.ts', output: { file: output, format: 'esm', sourcemap: true }, plugins: [ typescript({ module: 'esnext' }), ...(isMinify ? [ minify({ comments: false }) ] : []) ] }}export default [ getConfig({ output: 'lib/index.mjs' }), getConfig({ output: 'lib/index.min.mjs', isMinify: true })]
- If you wonder about
package.json
, it is
{ "main": "lib/index.js", "module": "lib/index.mjs", "unpkg": "lib/index.min.mjs", "types": "lib/index.d.ts", "files": [ "lib", "src" ], "scripts": { "build": "rimraf lib && rollup -c && yarn tsc", "tsc": "tsc -P src/tsconfig.json", "prepack": "yarn build && yarn deploy" }}
- And
tsconfig.json
{ "compilerOptions": { "target": "ES2017", "module": "commonjs", "declaration": true, "declarationMap": true, }}
- Generated files can be viewed here -- https://unpkg.com/any-serialize/. As you can see index.mjs is so nice to read. And there is no
require()
or importing from node_modules.
patarapolw / any-serialize
Serialize any JavaScript objects, as long as you provides how-to. I have already provided Date, RegExp and Function.
Features are
- Serialize anything.
- Deserialize almost anything, but if you tweak a little, you can make it work with anything as well.
- Hash anything to string.
- Clone anything.
- No external dependencies, and highly customizable.
Original Link: https://dev.to/patarapolw/what-s-your-way-of-publishing-node-js-library-for-the-browser-also-introducing-rollup-4i5h
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To