Skip to content

Commit

Permalink
Do *not* consider an empty include option means 'includes all'. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Septh committed Feb 3, 2023
1 parent cc9959c commit 8a8831d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function externals(options: ExternalsOptions = {}): Plugin {
const config: Config = Object.assign(Object.create(null), defaults, options)
let include: RegExp[],
exclude: RegExp[]
const isIncluded = (id: string) => include.length === 0 || include.some(rx => rx.test(id))
const isIncluded = (id: string) => include.some(rx => rx.test(id))
const isExcluded = (id: string) => exclude.some(rx => rx.test(id))

return {
Expand Down Expand Up @@ -245,7 +245,7 @@ function externals(options: ExternalsOptions = {}): Plugin {
}
}

// Handle npm dependencies.
// Handle other imports.
return isIncluded(id) && !isExcluded(id)
? false // external
: null // normal handling
Expand Down
43 changes: 37 additions & 6 deletions test/relative.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
import test from 'ava'
import { initPlugin, callHook } from './_common'
import type { ExternalsOptions } from '../src/index'

test("Filters out relative specifiers", async t => {
const noDeps: ExternalsOptions = {
builtins: false,
deps: false,
devDeps: false,
optDeps: false,
peerDeps: false
}

test.only("Does NOT filter out relative specifiers by default", async t => {
const relativeSpecifiers = [ './sibling.js', '../parent.js' ]

const { plugin } = await initPlugin(noDeps)
for (const identifier of relativeSpecifiers) {
t.is(await callHook(plugin, 'resolveId', identifier), null)
}
})

test("Filters out relative specifiers when asked to", async t => {
const relativeSpecifiers = [ './sibling.js', '../parent.js' ]

const { plugin } = await initPlugin({
...noDeps,
include: relativeSpecifiers
})
for (const builtin of relativeSpecifiers) {
t.false(await callHook(plugin, 'resolveId', builtin))
for (const identifier of relativeSpecifiers) {
t.false(await callHook(plugin, 'resolveId', identifier))
}
})

test("Does NOT filter out absolute specifiers by default", async t => {
const absoluteSpecifiers = [ '/root.js' ]
if (process.platform === 'win32')
absoluteSpecifiers.push('C:\\root.js', '\\root.js')

const { plugin } = await initPlugin(noDeps)
for (const identifier of absoluteSpecifiers) {
t.is(await callHook(plugin, 'resolveId', identifier), null, `Failed id: ${identifier}`)
}
})

test("Does NOT filter out absolute specifiers", async t => {
test("Filters out absolute specifiers when asked to", async t => {
const absoluteSpecifiers = [ '/root.js' ]
if (process.platform === 'win32')
absoluteSpecifiers.push('C:\\root.js', '\\root.js')

const { plugin } = await initPlugin({
...noDeps,
include: absoluteSpecifiers
})
for (const builtin of absoluteSpecifiers) {
t.is(await callHook(plugin, 'resolveId', builtin), null)
for (const identifier of absoluteSpecifiers) {
t.is(await callHook(plugin, 'resolveId', identifier), null, `Failed id: ${identifier}`)
}
})

0 comments on commit 8a8831d

Please sign in to comment.