Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 23, 2020 10:28 am GMT

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 run ts-node, ts-mocha and publish for Node, without esm), but rollup.config.js uses esnext.
    • I also use tsc to build for Node. Rollup just cannot properly do it. And Rollup cannot generate declaration and declarationMap
// 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,  }}

GitHub logo 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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To