Skip to content

Commit 8a8831d

Browse files
committed
Do *not* consider an empty include option means 'includes all'. Fixes #20.
1 parent cc9959c commit 8a8831d

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function externals(options: ExternalsOptions = {}): Plugin {
131131
const config: Config = Object.assign(Object.create(null), defaults, options)
132132
let include: RegExp[],
133133
exclude: RegExp[]
134-
const isIncluded = (id: string) => include.length === 0 || include.some(rx => rx.test(id))
134+
const isIncluded = (id: string) => include.some(rx => rx.test(id))
135135
const isExcluded = (id: string) => exclude.some(rx => rx.test(id))
136136

137137
return {
@@ -245,7 +245,7 @@ function externals(options: ExternalsOptions = {}): Plugin {
245245
}
246246
}
247247

248-
// Handle npm dependencies.
248+
// Handle other imports.
249249
return isIncluded(id) && !isExcluded(id)
250250
? false // external
251251
: null // normal handling

test/relative.test.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
11
import test from 'ava'
22
import { initPlugin, callHook } from './_common'
3+
import type { ExternalsOptions } from '../src/index'
34

4-
test("Filters out relative specifiers", async t => {
5+
const noDeps: ExternalsOptions = {
6+
builtins: false,
7+
deps: false,
8+
devDeps: false,
9+
optDeps: false,
10+
peerDeps: false
11+
}
12+
13+
test.only("Does NOT filter out relative specifiers by default", async t => {
14+
const relativeSpecifiers = [ './sibling.js', '../parent.js' ]
15+
16+
const { plugin } = await initPlugin(noDeps)
17+
for (const identifier of relativeSpecifiers) {
18+
t.is(await callHook(plugin, 'resolveId', identifier), null)
19+
}
20+
})
21+
22+
test("Filters out relative specifiers when asked to", async t => {
523
const relativeSpecifiers = [ './sibling.js', '../parent.js' ]
624

725
const { plugin } = await initPlugin({
26+
...noDeps,
827
include: relativeSpecifiers
928
})
10-
for (const builtin of relativeSpecifiers) {
11-
t.false(await callHook(plugin, 'resolveId', builtin))
29+
for (const identifier of relativeSpecifiers) {
30+
t.false(await callHook(plugin, 'resolveId', identifier))
31+
}
32+
})
33+
34+
test("Does NOT filter out absolute specifiers by default", async t => {
35+
const absoluteSpecifiers = [ '/root.js' ]
36+
if (process.platform === 'win32')
37+
absoluteSpecifiers.push('C:\\root.js', '\\root.js')
38+
39+
const { plugin } = await initPlugin(noDeps)
40+
for (const identifier of absoluteSpecifiers) {
41+
t.is(await callHook(plugin, 'resolveId', identifier), null, `Failed id: ${identifier}`)
1242
}
1343
})
1444

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

2050
const { plugin } = await initPlugin({
51+
...noDeps,
2152
include: absoluteSpecifiers
2253
})
23-
for (const builtin of absoluteSpecifiers) {
24-
t.is(await callHook(plugin, 'resolveId', builtin), null)
54+
for (const identifier of absoluteSpecifiers) {
55+
t.is(await callHook(plugin, 'resolveId', identifier), null, `Failed id: ${identifier}`)
2556
}
2657
})

0 commit comments

Comments
 (0)