I am working on packaging a TypeScript library using rollup with declaration (d.ts) files, and I am aiming to generate both cjs and es versions.
After brainstorming, I came up with the following rollup configuration:
const {nodeResolve} = require('@rollup/plugin-node-resolve')
const nodeExternals = require('rollup-plugin-node-externals');
const json = require('@rollup/plugin-json');
const commonjs = require('@rollup/plugin-commonjs');
const typescript = require('rollup-plugin-typescript2');
const packagePlugin = require('@mpen/rollup-plugin-package');
const cleanPlugin = require('@mpen/rollup-plugin-clean');
const findUp = require('find-up');
const {readFileSync} = require('fs');
module.exports = function rollupPresetTslib(opts = {}) {
const tsconfigFile = findUp.sync(opts.tsconfig ?? 'tsconfig.json')
const tsconfig = JSON.parse(readFileSync(tsconfigFile))
return {
input: tsconfig.files,
plugins: [
commonjs({
include: 'node_modules/**',
}),
nodeExternals({
builtins: true,
deps: true,
devDeps: false,
peerDeps: true,
optDeps: false,
}),
json(),
typescript({
abortOnError: process.env.NODE_ENV === 'production',
tsconfig: tsconfigFile,
...opts.typescriptOptions,
}),
nodeResolve({
extensions: ['.ts', '.json']
}),
packagePlugin(),
cleanPlugin(),
...opts.plugins ?? [],
],
watch: {
buildDelay: 200,
...opts.watch,
},
preserveSymlinks: true,
preserveModules: false,
output: ['cjs', 'es'].map(format => ({
dir: `dist/${format}`,
format: format,
sourcemap: true,
inlineDynamicImports: false,
})),
}
}
The current output looks good but it only compiles to cjs:
https://i.sstatic.net/OaKSM.png
Attempting to output to both formats resulted in duplicate .d.ts files:
https://i.sstatic.net/PzP46.png
How can I achieve a single copy of the d.ts files along with cjs and es builds?