Skip to content

Instantly share code, notes, and snippets.

@kripod
Created May 6, 2020 10:30
Show Gist options
  • Save kripod/8a01a8a7f5baa1d121dcd07eb8a943b9 to your computer and use it in GitHub Desktop.
Save kripod/8a01a8a7f5baa1d121dcd07eb8a943b9 to your computer and use it in GitHub Desktop.
Creating minified ESM+CJS bundles from TypeScript with Rollup and Babel in a monorepo, utilizing conditional exports
{
"name": "to-be-added",
"version": "0.0.0",
"sideEffects": false,
"exports": {
".": {
"import": "./dist-esm/bundle.min.mjs",
"require": "./dist-cjs/bundle.min.cjs"
},
"./server": "./server/index.js"
},
"main": "./dist-cjs/bundle.min.cjs",
"module": "./dist-esm/bundle.min.mjs",
"source": "./src/index.ts",
"types": "./dist-cjs/bundle.min.cjs.d.ts",
"files": [
"dist-*/",
"server/",
"src/"
],
"scripts": {
"build": "rollup --config --sourcemap",
"develop": "yarn build --watch"
}
}
import resolve from '@rollup/plugin-node-resolve';
import ts from '@wessberg/rollup-plugin-ts';
import { terser } from 'rollup-plugin-terser';
import pkg from './package.json';
const minifiedOutputs = [
{
file: pkg.exports['.'].import,
format: 'esm',
},
{
file: pkg.exports['.'].require,
format: 'cjs',
},
];
const unminifiedOutputs = minifiedOutputs.map(({ file, ...rest }) => ({
...rest,
file: file.replace('.min.', '.'),
}));
const commonPlugins = [
ts({
transpiler: 'babel',
babelConfig: '../..', // TODO: Use `{ rootMode: 'upward' }` instead
}),
];
export default [
{
input: './src/index.ts',
output: [...unminifiedOutputs, ...minifiedOutputs],
plugins: [
...commonPlugins,
resolve(),
terser({ include: /\.min\.[^.]+$/ }),
],
external: [/^@babel\/runtime\//],
},
{
input: './src/server.ts',
output: {
file: pkg.exports['./server'],
format: 'cjs',
},
plugins: commonPlugins,
},
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment