Skip to content

Commit

Permalink
feat: support for windows absolute paths in filter patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
recursive-beast committed Apr 24, 2021
1 parent f16c69d commit 76249e4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FilterPattern, normalizePath } from "@rollup/pluginutils";
import path from "path";
import { OutputOptions } from "rollup";

Expand All @@ -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;
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}:`;
Expand Down Expand Up @@ -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<string, Buffer>();

Expand Down
22 changes: 21 additions & 1 deletion tests/unit.test.ts
Original file line number Diff line number Diff line change
@@ -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"));
Expand All @@ -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, "/"),
]);
});

0 comments on commit 76249e4

Please sign in to comment.