diff --git a/package.json b/package.json index e229dff..dde25cf 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,10 @@ "import": "./dist/index.mjs", "require": "./dist/index.cjs" }, + "./builder": { + "import": "./dist/builder.mjs", + "require": "./dist/builder.cjs" + }, "./package.json": "./package.json" } } diff --git a/src/builder.ts b/src/builder.ts new file mode 100644 index 0000000..66ea302 --- /dev/null +++ b/src/builder.ts @@ -0,0 +1 @@ +export { Builder } from "./builder/index"; diff --git a/src/builder/index.ts b/src/builder/index.ts index a0fc4be..c5a02aa 100644 --- a/src/builder/index.ts +++ b/src/builder/index.ts @@ -14,15 +14,6 @@ import { import { APIBuilder } from "./api-builder"; import type picomatch from "picomatch"; -let pm: typeof picomatch | null = null; -/* c8 ignore next 6 */ -try { - require.resolve("picomatch"); - pm = require("picomatch"); -} catch { - // do nothing -} - export class Builder< TReturnType extends Output = PathsOutput, TGlobFunction = typeof picomatch, @@ -166,7 +157,8 @@ export class Builder< patterns: string[], ...options: GlobParams | [] ) { - const globFn = (this.globFunction || pm) as GlobFunction | null; + const globFn = (this.globFunction || + Builder.defaultGlobFunction) as GlobFunction | null; /* c8 ignore next 5 */ if (!globFn) { throw new Error("Please specify a glob function to use glob matching."); @@ -180,4 +172,6 @@ export class Builder< this.options.filters.push((path) => isMatch(path)); return this; } + + static defaultGlobFunction: typeof picomatch | null = null; } diff --git a/src/index.ts b/src/index.ts index fba8ef8..21c9772 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,18 @@ -import { Builder } from "./builder"; +import type picomatch from "picomatch"; +import { Builder } from "./builder/index"; export { Builder as fdir }; export type Fdir = typeof Builder; export * from "./types"; + +let pm: typeof picomatch | null = null; +/* c8 ignore next 6 */ +try { + require.resolve("picomatch"); + pm = require("picomatch"); +} catch { + // do nothing +} + +Builder.defaultGlobFunction = pm || null; diff --git a/tsdown.config.ts b/tsdown.config.ts index aa0ab76..73012df 100644 --- a/tsdown.config.ts +++ b/tsdown.config.ts @@ -1,11 +1,11 @@ import { defineConfig } from 'tsdown/config' export default defineConfig({ - entry: 'src/index.ts', + entry: ['src/index.ts', 'src/builder.ts'], format: ['cjs', 'esm'], target: 'node12', removeNodeProtocol: true, dts: true, exports: true, fixedExtension: true -}) \ No newline at end of file +})