|
1 | 1 | import fs from "fs/promises"; |
2 | 2 | import path from "path"; |
3 | 3 | import { Plugin } from "rollup"; |
4 | | -import { createFilter, FilterPattern, normalizePath } from "@rollup/pluginutils"; |
| 4 | +import { createFilter, normalizePath } from "@rollup/pluginutils"; |
5 | 5 | import { parse, print, types, visit } from "recast"; |
6 | 6 | import { getOutputId, getRelativeImportPath } from "./helpers"; |
7 | 7 |
|
8 | 8 | const PLUGIN_NAME = "external-assets"; |
9 | 9 | const PREFIX = `\0${PLUGIN_NAME}:`; |
10 | 10 |
|
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; |
19 | 29 | } |
20 | 30 |
|
21 | 31 | /** |
22 | 32 | * 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. |
30 | 33 | * @param options The options object. |
31 | 34 | */ |
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 | + |
38 | 53 | const assets = new Map<string, Buffer>(); |
39 | 54 |
|
40 | 55 | return { |
@@ -145,3 +160,6 @@ export default function externalAssets( |
145 | 160 | }, |
146 | 161 | }; |
147 | 162 | } |
| 163 | + |
| 164 | +export { externalAssets }; |
| 165 | +export default externalAssets; |
0 commit comments