Skip to content

Commit

Permalink
feat: allow regular expressions in externals array
Browse files Browse the repository at this point in the history
  • Loading branch information
dwightjack committed Nov 15, 2022
1 parent ba4a09b commit 9e929e6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import dts from 'rollup-plugin-dts'
import replace from '@rollup/plugin-replace'
import { resolve, dirname, normalize, extname } from 'pathe'
import { resolvePath, resolveModuleExportNames } from 'mlly'
import { getpkg, tryResolve, warn } from '../utils'
import { arrayIncludes, getpkg, tryResolve, warn } from '../utils'
import type { BuildContext } from '../types'
import { esbuild } from './plugins/esbuild'
import { JSONPlugin } from './plugins/json'
Expand Down Expand Up @@ -168,7 +168,7 @@ export function getRollupOptions (ctx: BuildContext): RollupOptions {

external (id) {
const pkg = getpkg(id)
const isExplicitExternal = ctx.options.externals.includes(pkg)
const isExplicitExternal = arrayIncludes(ctx.options.externals, pkg)
if (isExplicitExternal) {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface BuildOptions {
declaration?: boolean
outDir: string
stub: boolean
externals: string[]
externals: (string|RegExp)[]
dependencies: string[]
peerDependencies: string[]
devDependencies: string[]
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,7 @@ export function extractExportFilenames (exports: PackageJson['exports'], conditi
: extractExportFilenames(exports, [...conditions, condition])
)
}

export function arrayIncludes (arr: (string|RegExp)[], searchElement: string) {
return arr.some(entry => entry instanceof RegExp ? entry.test(searchElement) : entry === searchElement)
}
4 changes: 2 additions & 2 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs'
import chalk from 'chalk'
import { resolve } from 'pathe'
import { PackageJson } from 'pkg-types'
import { extractExportFilenames, getpkg, warn } from './utils'
import { extractExportFilenames, getpkg, arrayIncludes, warn } from './utils'
import { BuildContext } from './types'

export function validateDependencies (ctx: BuildContext) {
Expand All @@ -20,7 +20,7 @@ export function validateDependencies (ctx: BuildContext) {
}
for (const id of usedDependencies) {
if (
!ctx.options.externals.includes(id) &&
!arrayIncludes(ctx.options.externals, id) &&
!id.startsWith('chunks/') &&
!ctx.options.dependencies.includes(getpkg(id)) &&
!ctx.options.peerDependencies.includes(getpkg(id))
Expand Down
13 changes: 12 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { extractExportFilenames, inferExportType } from '../src/utils'
import { extractExportFilenames, inferExportType, arrayIncludes } from '../src/utils'

describe('inferExportType', () => {
it('infers export type by condition', () => {
Expand All @@ -26,3 +26,14 @@ describe('extractExportFilenames', () => {
expect(extractExportFilenames({ require: { node: 'test', other: { import: 'this', require: 'that' } } })).to.deep.equal([{ file: 'test', type: 'cjs' }, { file: 'this', type: 'esm' }, { file: 'that', type: 'cjs' }])
})
})

describe('arrayIncludes', () => {
it('handles strings', () => {
expect(arrayIncludes(['test1', 'test2'], 'test1')).to.eq(true)
expect(arrayIncludes(['test1', 'test2'], 'test3')).to.eq(false)
})
it('handles regular expressions', () => {
expect(arrayIncludes([/t1$/, 'test2'], 'test1')).to.eq(true)
expect(arrayIncludes([/t3$/, 'test2'], 'test1')).to.eq(false)
})
})

0 comments on commit 9e929e6

Please sign in to comment.