Skip to content

Commit

Permalink
refactor: let rollup normalize patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
recursive-beast committed Aug 8, 2023
1 parent 15d9969 commit 60fea97
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 38 deletions.
14 changes: 0 additions & 14 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FilterPattern, normalizePath } from "@rollup/pluginutils";
import path from "path";
import { OutputOptions } from "rollup";

Expand All @@ -25,16 +24,3 @@ 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: 1 addition & 3 deletions 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, normalizePath } from "@rollup/pluginutils";
import { parse, print, types, visit } from "recast";
import { getOutputId, getRelativeImportPath, normalizeFilterPattern } from "./helpers";
import { getOutputId, getRelativeImportPath } from "./helpers";

const PLUGIN_NAME = "external-assets";
const PREFIX = `\0${PLUGIN_NAME}:`;
Expand Down Expand Up @@ -34,8 +34,6 @@ 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: 1 addition & 21 deletions tests/unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import { getOutputId, normalizeFilterPattern } from "../src/helpers";
import { getOutputId } from "../src/helpers";

test("getOutputId", () => {
expect(getOutputId("a.ext", { dir: "out" })).toBe(path.resolve("out/a.ext"));
Expand All @@ -12,23 +12,3 @@ 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 60fea97

Please sign in to comment.