Skip to content

Instantly share code, notes, and snippets.

@Boo-urns
Created October 18, 2018 14:32
Show Gist options
  • Save Boo-urns/cdd437de900693278be504cc2a995daa to your computer and use it in GitHub Desktop.
Save Boo-urns/cdd437de900693278be504cc2a995daa to your computer and use it in GitHub Desktop.
Multiple output files with rollup
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import external from 'rollup-plugin-peer-deps-external';
import postcss from 'rollup-plugin-postcss';
import resolve from 'rollup-plugin-node-resolve';
import url from 'rollup-plugin-url';
import autoprefixer from 'autoprefixer';
import json from 'rollup-plugin-json';
import filesize from 'rollup-plugin-filesize';
import globby from 'globby';
const plugins = [
external(),
postcss({
plugins: [
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'iOS >= 8',
'not ie < 12',
'not ie_mob < 12'
]
}),
],
}),
url(),
json(),
babel({
exclude: 'node_modules/**',
plugins: ['external-helpers'],
}),
resolve(),
commonjs(),
filesize()
];
const capitalizeComponent = (name) => {
const splitName = name.split('-');
const componentName = splitName.map(str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`);
return componentName.join('');
}
async function getFiles() {
const paths = await globby(['src/components/**/index.js', '!**/__tests__']);
const exports = paths.map(path => {
const fileSearchArr = path.match('/(?<=components/).*(?=/)/');
const componentName = capitalizeComponent(fileSearchArr[0].replace(/[/]/g, ''));
return {
input: path,
output: [
{
file: `${componentName}.js`,
format: 'cjs',
sourcemap: true,
exports: 'named'
},
{
file: `${componentName}.es.js`,
format: 'es',
sourcemap: true,
exports: 'named'
}
],
plugins
}
})
return exports;
}
export default getFiles();
@Boo-urns
Copy link
Author

If you have packages that are in-use in the library and your project make sure to add external option
external: ['whatever-the-package-is', 'maybe-another']

Otherwise it'll add your package to the build and bloat the size up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment