Skip to content

Commit

Permalink
fix(rollup): support external alias and single string type
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Nov 3, 2019
1 parent 7e75de4 commit c4e21be
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/rollup/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ program
'Specify export mode (auto, default, named, none)',
)
.option(
'-x, --externals <package>',
'-x, --external, --externals <package>',
'extra external packages, peerDependencies, and dependencies for node by default',
parseArrayArgs,
)
Expand Down Expand Up @@ -90,6 +90,7 @@ const options: ConfigOptions = pick(
'formats',
'monorepo',
'exports',
'external',
'externals',
'globals',
'aliases',
Expand Down
25 changes: 16 additions & 9 deletions packages/rollup/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
PROD,
__DEV__,
__PROD__,
arrayify,
identify,
isTsAvailable,
monorepoPkgs,
Expand Down Expand Up @@ -131,14 +132,20 @@ const onwarn = (warning: string | RollupWarning, warn: WarningHandler) => {

export type Format = 'cjs' | 'es2015' | 'es5' | 'esm' | 'umd'

export type External =
| string
| string[]
| ((id: string, collectedExternals?: string[]) => boolean)

export interface ConfigOptions {
formats?: ModuleFormat[]
monorepo?: boolean | string[]
input?: string
exclude?: string[]
outputDir?: string
exports?: OutputOptions['exports']
externals?: string[] | ((id: string, collected?: string[]) => boolean)
external?: External
externals?: External
globals?: StringMap
aliases?: StringMap | AliasOptions['entries']
copies?: StringMap | CopyOptions['targets'] | CopyOptions
Expand Down Expand Up @@ -171,7 +178,8 @@ export const config = ({
exclude = [],
outputDir = 'lib',
exports,
externals = [],
external,
externals = external || [],
globals: umdGlobals,
aliases = [],
copies = [],
Expand Down Expand Up @@ -266,10 +274,10 @@ ConfigOptions = {}): RollupOptions[] => {

const deps = Object.keys(dependencies)

const external =
const collectedExternals =
typeof externals === 'function'
? []
: externals.concat(
: arrayify(externals).concat(
Object.keys(peerDependencies),
node ? deps.concat(builtinModules) : [],
)
Expand All @@ -279,7 +287,7 @@ ConfigOptions = {}): RollupOptions[] => {
formats && formats.length
? formats
: DEFAULT_FORMATS.concat(node ? [] : 'umd')
const pkgGlobals = external.reduce((pkgGlobals, pkg) => {
const pkgGlobals = collectedExternals.reduce((pkgGlobals, pkg) => {
if (pkgGlobals[pkg] == null) {
pkgGlobals[pkg] = upperCamelCase(normalizePkg(pkg))
}
Expand Down Expand Up @@ -314,14 +322,13 @@ ConfigOptions = {}): RollupOptions[] => {
},
external(id: string) {
if (typeof externals === 'function') {
return externals.call(this, id, external)
return externals.call(this, id, collectedExternals)
}
return external.some(pkg => {
return collectedExternals.some(pkg => {
const pkgRegExp = tryRegExp(pkg)
return pkgRegExp instanceof RegExp
? pkgRegExp.test(id)
: // TODO: should we drop glob support in favor of regexp?
isGlob(pkg)
: isGlob(pkg)
? isMatch(id, pkg)
: id === pkg || id.startsWith(pkg + '/')
})
Expand Down
8 changes: 8 additions & 0 deletions packages/utils/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,11 @@ export const findUp = (searchEntry: string, searchFile = 'package.json') => {

return ''
}

export const arrayify = <T, R = T extends Array<infer S> ? S : T>(
...args: T[]
) =>
args.reduce<R[]>((arr, curr) => {
arr.push(...(Array.isArray(curr) ? curr : [curr]))
return arr
}, [])

0 comments on commit c4e21be

Please sign in to comment.