Skip to content

Instantly share code, notes, and snippets.

@asselin
Forked from aleclarson/rollup-typescript.md
Last active September 29, 2022 18:18
Show Gist options
  • Select an option

  • Save asselin/6662c065eec3de21abd243a484823250 to your computer and use it in GitHub Desktop.

Select an option

Save asselin/6662c065eec3de21abd243a484823250 to your computer and use it in GitHub Desktop.
The best Rollup config for TypeScript libraries

Features

πŸ”₯ Blazing fast builds
πŸ˜‡ CommonJS bundle
🌲 .mjs bundle
✨ .d.ts bundle + type-checking
🧐 Source maps

Install

  1. Run this in your terminal:

    npm i rollup rollup-plugin-typescript2 typescript tslib -D
    wget -O rollup.config.js https://gist.githubusercontent.com/asselin/6662c065eec3de21abd243a484823250/raw/rollup.config.js
  2. Ensure your tsconfig.json contains these values:

    "compilerOptions": {
      "target": "esnext"
    }
  3. Ensure your package.json contains these values (and replace the my-lib part):

    "main": "dist/my-lib.js",
    "module": "dist/my-lib.mjs",
    "typings": "dist/my-lib.d.ts",
  4. Change the input: 'src/index.ts' line in rollup.config.js if needed.

  5. All done! Now do npm rollup -c to build, or add this to your package.json:

    "scripts": {
      "build": "rollup -c"
    }
import typescript from 'rollup-plugin-typescript2';
const name = require('./package.json').main.replace(/\.js$/, '');
export default {
input: 'src/index.ts',
plugins: [typescript()],
output: [
{
file: `${name}.js`,
format: 'cjs',
sourcemap: true,
},
{
file: `${name}.mjs`,
format: 'es',
sourcemap: true,
},
],
external: (id) => !/^[./]/.test(id),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment