Hector Yeomans Blog

Convert node package with Typescript and Rollup

May 30, 2020

I already had a library that I wanted to convert to Typescript. I picked Rollup.js to do my build process.

First I installed the following packages:

> npm i -E -D rollup typescript @rollup/plugin-commonjs @rollup/plugin-node-resolve rollup-plugin-typescript2 rollup-plugin-peer-deps-external

I ended up with:

Then I created a tsconfig.json:

{
  "compilerOptions": {
    "outDir": "build",
    "module": "esnext",
    "target": "es5",
    "lib": ["es6", "dom", "es2016", "es2017"],
    "sourceMap": true,
    "allowJs": false,
    "declaration": true,
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "build"]
}

Also a rollup.config.js:

import typescript from 'rollup-plugin-typescript2'
import external from 'rollup-plugin-peer-deps-external'
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'

import pkg from './package.json'

export default {
  input: 'src/index.ts',
  output: [
    {
      file: pkg.main,
      format: 'cjs',
      exports: 'named',
      sourcemap: true
    },
    {
      file: pkg.module,
      format: 'es',
      exports: 'named',
      sourcemap: true
    }
  ],
  plugins: [
    external(),
    resolve(),
    typescript({
      rollupCommonJSResolveHack: true,
      exclude: '**/__tests__/**',
      clean: true
    }),
    commonjs({
      include: ['node_modules/**']
    })
  ]
}

Modified my package.json:

  "main": "build/index.js",
  "module": "build/index.es.js",
  "jsnext:main": "build/index.es.js",
  "files": ["build"],
  "scripts": {
    "build": "rollup -c",
    ...
  }

Finally I created a dummy index.ts for testing:

function index() {
  console.log('hello world')
}

And ran:

> npm run build

ESLINT

I had to update eslint packages:

npm i -E -D @typescript-eslint/parser @typescript-eslint/eslint-plugin

Ended up with:

* @typescript-eslint/[email protected]
* @typescript-eslint/[email protected]

And then modified my .eslintrc.js:

...
parser: '@typescript-eslint/parser',
  extends: [
    'standard',
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  plugins: ['@typescript-eslint'],
  ...

You can see the resulting branch here:

https://github.com/hyeomans/zuora-js/tree/ts-rollup

Thanks for reading!


Hector Yeomans

Written by Hector Yeomans who lives in Mexico and works remotely for Invision. Follow him on Twitter, or add him on LinkedIn.