diff --git a/src/rollup/plugins/externals-legacy.ts b/src/rollup/plugins/externals-legacy.ts index 06d248fe35..f2e8b88d3a 100644 --- a/src/rollup/plugins/externals-legacy.ts +++ b/src/rollup/plugins/externals-legacy.ts @@ -6,10 +6,19 @@ import type { Plugin } from "rollup"; import { resolvePath, isValidNodeImport, normalizeid } from "mlly"; import semver from "semver"; import { isDirectory, retry } from "../../utils"; +import { normalizeMatcher } from "./externals"; export interface NodeExternalsOptions { - inline?: string[]; - external?: string[]; + inline?: Array< + | string + | RegExp + | ((id: string, importer?: string) => Promise | boolean) + >; + external?: Array< + | string + | RegExp + | ((id: string, importer?: string) => Promise | boolean) + >; outDir?: string; trace?: boolean; traceOptions?: NodeFileTraceOptions; @@ -36,8 +45,10 @@ export function externals(opts: NodeExternalsOptions): Plugin { }; // Normalize options - opts.inline = (opts.inline || []).map((p) => normalize(p)); - opts.external = (opts.external || []).map((p) => normalize(p)); + const inlineMatchers = (opts.inline || []).map((p) => normalizeMatcher(p)); + const externalMatchers = (opts.external || []).map((p) => + normalizeMatcher(p) + ); return { name: "node-externals", @@ -60,25 +71,18 @@ export function externals(opts: NodeExternalsOptions): Plugin { // Normalize path (windows) const id = normalize(originalId); - // Id without .../node_modules/ - const idWithoutNodeModules = id.split("node_modules/").pop(); - // Check for explicit inlines - if ( - opts.inline.some( - (i) => id.startsWith(i) || idWithoutNodeModules.startsWith(i) - ) - ) { - return null; + for (const matcher of inlineMatchers) { + if (matcher(id, importer)) { + return null; + } } // Check for explicit externals - if ( - opts.external.some( - (i) => id.startsWith(i) || idWithoutNodeModules.startsWith(i) - ) - ) { - return { id, external: true }; + for (const matcher of externalMatchers) { + if (matcher(id, importer)) { + return { id, external: true }; + } } // Resolve id using rollup resolver diff --git a/src/rollup/plugins/externals.ts b/src/rollup/plugins/externals.ts index 7325250aa2..033bad9cab 100644 --- a/src/rollup/plugins/externals.ts +++ b/src/rollup/plugins/externals.ts @@ -9,8 +9,16 @@ import semver from "semver"; import { isDirectory } from "../../utils"; export interface NodeExternalsOptions { - inline?: string[]; - external?: string[]; + inline?: Array< + | string + | RegExp + | ((id: string, importer?: string) => Promise | boolean) + >; + external?: Array< + | string + | RegExp + | ((id: string, importer?: string) => Promise | boolean) + >; outDir?: string; trace?: boolean; traceOptions?: NodeFileTraceOptions; @@ -37,8 +45,10 @@ export function externals(opts: NodeExternalsOptions): Plugin { }; // Normalize options - opts.inline = (opts.inline || []).map((p) => normalize(p)); - opts.external = (opts.external || []).map((p) => normalize(p)); + const inlineMatchers = (opts.inline || []).map((p) => normalizeMatcher(p)); + const externalMatchers = (opts.external || []).map((p) => + normalizeMatcher(p) + ); return { name: "node-externals", @@ -65,21 +75,17 @@ export function externals(opts: NodeExternalsOptions): Plugin { const idWithoutNodeModules = id.split("node_modules/").pop(); // Check for explicit inlines - if ( - opts.inline.some( - (i) => id.startsWith(i) || idWithoutNodeModules.startsWith(i) - ) - ) { - return null; + for (const matcher of inlineMatchers) { + if (matcher(id, importer)) { + return null; + } } // Check for explicit externals - if ( - opts.external.some( - (i) => id.startsWith(i) || idWithoutNodeModules.startsWith(i) - ) - ) { - return { id, external: true }; + for (const matcher of externalMatchers) { + if (matcher(id, importer)) { + return { id, external: true }; + } } // Resolve id using rollup resolver @@ -508,3 +514,24 @@ async function isFile(file: string) { throw err; } } + +export function normalizeMatcher( + pattern: + | string + | RegExp + | ((id: string, importer?: string) => Promise | boolean) +) { + if (typeof pattern === "string") { + const _pattern = normalize(pattern); + return (id: string) => { + const idWithoutNodeModules = id.split("node_modules/").pop(); + return ( + id.startsWith(_pattern) || idWithoutNodeModules.startsWith(_pattern) + ); + }; + } + if (typeof pattern === "function") { + return pattern; + } + return (id: string) => pattern.test(id); +}