Rollup: Support globs/regex in config.external

Created on 30 May 2019  ·  3Comments  ·  Source: rollup/rollup

I wasn't able to find any discussion on this ~ just an unanswered SO question.

Feature Use Case

It would be nice if the external config could accept regex or glob patterns. My use case is importing modules within a package, such as lodash-es/isMatch.

I'm adding all of my dependencies to my external config by just mapping out the keys from my package.json dependencies:

import typescript from 'rollup-plugin-typescript2'

import pkg from './package.json'

export default {
  input: 'src/index.ts',
  output: [{ file: pkg.main, format: 'cjs' }, { file: pkg.module, format: 'es' }],
  external: [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependnencies || {})],
  plugins: [
    typescript({
      typescript: require('typescript'),
    }),
  ],
}

But I need to manually add all of my package/something imports.

Or, simpler, if I'm already have lodash-es as an external, it shouldn't warn me about `lodash-es/isMatch.

Also, I haven't dug around in my exports enough to know if lodash-es/isMatch is being included --- I'm just trying to clear out all of these warnings in my output.

Feature Proposal

Accept glob patterns in external:

external: ['rxjs/**', 'lodash-es/**']
t³ ✨ enhancement

Most helpful comment

Also check out the SO post, I added a slightly longer answer there.

All 3 comments

You could also just use a function:

external(id) {
  return id.includes('node_modules');
}

Of course this will only work if you do not want some devDependencies to be bundled. In that case, a little more work is necessary:

external(id) {
  return Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependnencies || {})).includes(id.split('/')[0]);
}

For a slight performance boost, you could extract the constant Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependnencies || {}) as this function will be called a lot.

Also check out the SO post, I added a slightly longer answer there.

That'll do it, thank you! I'd consider this closed for what I need.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FruitieX picture FruitieX  ·  3Comments

iam-peekay picture iam-peekay  ·  3Comments

azdavis picture azdavis  ·  3Comments

alendit picture alendit  ·  3Comments

ashubham picture ashubham  ·  3Comments