Convert node package with Typescript and Rollup

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:

1
2
3
> 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:

1
2
3
4
5
6
* [email protected]
* [email protected]
* [email protected]
* [email protected]
* @rollup/[email protected]
* @rollup/[email protected]

Then I created a tsconfig.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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:

1
2
3
4
5
6
7
8
"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:

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

And ran:

1
> npm run build

ESLINT

I had to update eslint packages:

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

Ended up with:

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

And then modified my .eslintrc.js:

1
2
3
4
5
6
7
8
9
...
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!