diff --git a/src/helpers.ts b/src/helpers.ts index 21f3742..1f659b7 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,3 +1,4 @@ +import { FilterPattern, normalizePath } from "@rollup/pluginutils"; import path from "path"; import { OutputOptions } from "rollup"; @@ -24,3 +25,16 @@ export function getRelativeImportPath(from: string, to: string) { return import_path; } + +export function normalizeFilterPattern(pattern?: FilterPattern): FilterPattern | undefined { + if (typeof pattern === "string") return normalizePath(pattern); + + if (Array.isArray(pattern)) { + return pattern.map(element => { + if (typeof element === "string") return normalizePath(element); + return element; + }); + } + + return pattern; +} diff --git a/src/index.ts b/src/index.ts index 3a8a48f..4a3c6b3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import path from "path"; import { Plugin } from "rollup"; import { createFilter, FilterPattern } from "@rollup/pluginutils"; import { parse, print, types, visit } from "recast"; -import { getOutputId, getRelativeImportPath } from "./helpers"; +import { getOutputId, getRelativeImportPath, normalizeFilterPattern } from "./helpers"; const PLUGIN_NAME = "external-assets"; const PREFIX = `\0${PLUGIN_NAME}:`; @@ -34,6 +34,8 @@ export default function externalAssets( exclude?: FilterPattern, options?: Options, ): Plugin { + include = normalizeFilterPattern(include); + exclude = normalizeFilterPattern(exclude); const idFilter = createFilter(include, exclude, options); const assets = new Map(); diff --git a/tests/unit.test.ts b/tests/unit.test.ts index 85a3313..9e91f5f 100644 --- a/tests/unit.test.ts +++ b/tests/unit.test.ts @@ -1,5 +1,5 @@ import path from "path"; -import { getOutputId } from "../src/helpers"; +import { getOutputId, normalizeFilterPattern } from "../src/helpers"; test("getOutputId", () => { expect(getOutputId("a.ext", { dir: "out" })).toBe(path.resolve("out/a.ext")); @@ -12,3 +12,23 @@ test("getOutputId", () => { expect(getOutputId("a.ext", {})).toBe(path.resolve("a.ext")); expect(getOutputId("a/b.ext", {})).toBe(path.resolve("a/b.ext")); }); + +test("normalizeFilterPattern", () => { + expect(normalizeFilterPattern()).toBeUndefined(); + expect(normalizeFilterPattern(null)).toBeNull(); + expect(normalizeFilterPattern(/\.txt/g)).toStrictEqual(/\.txt/g); + expect(normalizeFilterPattern("abc/def/**/*")).toBe("abc/def/**/*"); + expect(normalizeFilterPattern("abc\\def\\**\\*")).toBe("abc/def/**/*"); + expect(normalizeFilterPattern(path.resolve("abc/def/**/*"))).toBe(path.resolve("abc/def/**/*").replace(/\\/g, "/")); + expect([ + normalizeFilterPattern(/\.txt/g), + normalizeFilterPattern("abc/def/**/*"), + normalizeFilterPattern("abc\\def\\**\\*"), + normalizeFilterPattern(path.resolve("abc/def/**/*")), + ]).toStrictEqual([ + /\.txt/g, + "abc/def/**/*", + "abc/def/**/*", + path.resolve("abc/def/**/*").replace(/\\/g, "/"), + ]); +});