Skip to content

Commit df7ad53

Browse files
feat!: change function signature
BREAKING CHANGE: more convenient and future proof function signature
1 parent 60fea97 commit df7ad53

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/index.ts

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
11
import fs from "fs/promises";
22
import path from "path";
33
import { Plugin } from "rollup";
4-
import { createFilter, FilterPattern, normalizePath } from "@rollup/pluginutils";
4+
import { createFilter, normalizePath } from "@rollup/pluginutils";
55
import { parse, print, types, visit } from "recast";
66
import { getOutputId, getRelativeImportPath } from "./helpers";
77

88
const PLUGIN_NAME = "external-assets";
99
const PREFIX = `\0${PLUGIN_NAME}:`;
1010

11-
interface Options {
12-
/**
13-
* Optionally resolves the patterns against a directory other than `process.cwd()`.
14-
* If a `string` is specified, then the value will be used as the base directory.
15-
* Relative paths will be resolved against `process.cwd()` first.
16-
* If `false`, then the patterns will not be resolved against any directory.
17-
*/
18-
resolve?: string | false | null;
11+
export type FilterPattern = string | RegExp | (string | RegExp)[];
12+
13+
export function isFilterPattern(value: unknown): value is FilterPattern {
14+
let tmp: unknown[];
15+
16+
if (Array.isArray(value)) tmp = value;
17+
else tmp = [value];
18+
19+
return tmp.every((e) => typeof e === "string" || e instanceof RegExp);
20+
}
21+
22+
export interface ExternalAssetsOptions {
23+
/** A pattern, or array of patterns, to match files the plugin should ignore. */
24+
include: FilterPattern;
25+
/** A pattern, or array of patterns, to match files the plugin should operate on. */
26+
exclude?: FilterPattern;
27+
/** The value will be used as the base directory for resolving patterns. By default it's `process.cwd()`. */
28+
resolve?: string;
1929
}
2030

2131
/**
2232
* Make assets external but include them in the output.
23-
* @param include A valid picomatch pattern, or array of patterns.
24-
* If `include` is omitted or has zero length, all imports will be processed.
25-
*
26-
* **Note**: patterns that include windows paths are normalized to be valid picomatch patterns.
27-
* @param exclude If an asset matches one of the `exclude` patterns, its import will not be processed.
28-
*
29-
* **Note**: patterns that include windows paths are normalized to be valid picomatch patterns.
3033
* @param options The options object.
3134
*/
32-
export default function externalAssets(
33-
include?: FilterPattern,
34-
exclude?: FilterPattern,
35-
options?: Options,
36-
): Plugin {
37-
const idFilter = createFilter(include, exclude, options);
35+
function externalAssets(options: ExternalAssetsOptions): Plugin;
36+
37+
/**
38+
* Make assets external but include them in the output.
39+
* @param pattern A pattern, or array of patterns, to match files the plugin should ignore.
40+
*/
41+
function externalAssets(pattern: FilterPattern): Plugin;
42+
43+
function externalAssets(arg: FilterPattern | ExternalAssetsOptions): Plugin {
44+
let idFilter: ReturnType<typeof createFilter>;
45+
46+
if (isFilterPattern(arg)) {
47+
idFilter = createFilter(arg);
48+
} else {
49+
const { include, exclude, resolve } = arg;
50+
idFilter = createFilter(include, exclude, { resolve });
51+
}
52+
3853
const assets = new Map<string, Buffer>();
3954

4055
return {
@@ -145,3 +160,6 @@ export default function externalAssets(
145160
},
146161
};
147162
}
163+
164+
export { externalAssets };
165+
export default externalAssets;

0 commit comments

Comments
 (0)