diff --git a/src/index.ts b/src/index.ts index 87e4fea..33c2253 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { @@ -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 diff --git a/test/relative.test.ts b/test/relative.test.ts index 475cc71..5c1a7e9 100644 --- a/test/relative.test.ts +++ b/test/relative.test.ts @@ -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}`) } })