Skip to content

Instantly share code, notes, and snippets.

@developit
Last active June 29, 2021 15:33
Show Gist options
  • Save developit/41f088b6294e2591f53b to your computer and use it in GitHub Desktop.
Save developit/41f088b6294e2591f53b to your computer and use it in GitHub Desktop.
Rollup Automatic External Dependencies

Hi!

This is an example of how to use [Rollup] with external dependencies, without hard-coding them.

It reads your installed NPM dependencies and treats them as external to Rollup. They still get bundled, but not as ES2015.

Make sure you have a .babelrc or a "babel":{} section in your package.json.

import path from 'path';
import fs from 'fs';
import babel from 'rollup-plugin-babel';
import npm from 'rollup-plugin-npm';
import commonjs from 'rollup-plugin-commonjs';
let pkg = JSON.parse(fs.readFileSync('./package.json')),
external = Object.keys(pkg.dependencies || {}),
babelRc = pkg.babel || JSON.parse(fs.readFileSync('./.babelrc'));
export default {
entry: pkg['jsnext:main'] || 'src/index.js',
dest: pkg.main,
sourceMap: path.resolve(pkg.main),
moduleName: pkg.amdName || pkg.name,
format: process.env.FORMAT || 'umd',
external,
plugins: [
babel({
babelrc: false,
...babelRc
}),
npm({
jsnext: true,
main: true,
skip: external
}),
commonjs({
include: 'node_modules/**',
exclude: '**/*.css'
})
]
};
@ArthurClemens
Copy link

rollup-plugin-npm is now rollup-plugin-node-resolve.

@ngryman
Copy link

ngryman commented Mar 24, 2017

You can let pkg = require('./package.json') 😎

@rmariuzzo
Copy link

Or import pkg from './package.json'

@rosskevin
Copy link

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