Skip to content

Instantly share code, notes, and snippets.

@bluwy
Created October 24, 2020 14:22
Show Gist options
  • Select an option

  • Save bluwy/7ff73d4381d211b6e0419a9f353f0071 to your computer and use it in GitHub Desktop.

Select an option

Save bluwy/7ff73d4381d211b6e0419a9f353f0071 to your computer and use it in GitHub Desktop.
Rollup automatically set dependencies to external
import pkg from './package.json'
export function autoExternal() {
return {
name: 'auto-external',
options(opts) {
const deps = [
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies)
]
const external = (id, ...args) => {
// Bwlow adapted from https://github.com/rollup/rollup/blob/02954f5628bf8e9db57de823e92322d450f0fab4/src/utils/options/normalizeInputOptions.ts#L120-L140
if (opts.external === true) {
return true
}
if (typeof opts.external === 'function' && opts.external(id, ...args)) {
return true
}
if (opts.external) {
const externalArray = Array.isArray(opts.external)
? opts.external
: [opts.external]
const ids = new Set()
const matchers = []
for (const value of externalArray) {
if (value instanceof RegExp) {
matchers.push(value)
} else {
ids.add(value)
}
}
if (ids.has(id) || matchers.some((matcher) => matcher.test(id))) {
return true
}
}
return deps.some((dep) => id.startsWith(dep))
}
return { ...opts, external }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment