Skip to content

Commit

Permalink
feat!: change function signature
Browse files Browse the repository at this point in the history
BREAKING CHANGE: more convenient and future proof function signature
  • Loading branch information
recursive-beast committed Aug 8, 2023
1 parent 60fea97 commit df7ad53
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
import fs from "fs/promises";
import path from "path";
import { Plugin } from "rollup";
import { createFilter, FilterPattern, normalizePath } from "@rollup/pluginutils";
import { createFilter, normalizePath } from "@rollup/pluginutils";
import { parse, print, types, visit } from "recast";
import { getOutputId, getRelativeImportPath } from "./helpers";

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

interface Options {
/**
* Optionally resolves the patterns against a directory other than `process.cwd()`.
* If a `string` is specified, then the value will be used as the base directory.
* Relative paths will be resolved against `process.cwd()` first.
* If `false`, then the patterns will not be resolved against any directory.
*/
resolve?: string | false | null;
export type FilterPattern = string | RegExp | (string | RegExp)[];

export function isFilterPattern(value: unknown): value is FilterPattern {
let tmp: unknown[];

if (Array.isArray(value)) tmp = value;
else tmp = [value];

return tmp.every((e) => typeof e === "string" || e instanceof RegExp);
}

export interface ExternalAssetsOptions {
/** A pattern, or array of patterns, to match files the plugin should ignore. */
include: FilterPattern;
/** A pattern, or array of patterns, to match files the plugin should operate on. */
exclude?: FilterPattern;
/** The value will be used as the base directory for resolving patterns. By default it's `process.cwd()`. */
resolve?: string;
}

/**
* Make assets external but include them in the output.
* @param include A valid picomatch pattern, or array of patterns.
* If `include` is omitted or has zero length, all imports will be processed.
*
* **Note**: patterns that include windows paths are normalized to be valid picomatch patterns.
* @param exclude If an asset matches one of the `exclude` patterns, its import will not be processed.
*
* **Note**: patterns that include windows paths are normalized to be valid picomatch patterns.
* @param options The options object.
*/
export default function externalAssets(
include?: FilterPattern,
exclude?: FilterPattern,
options?: Options,
): Plugin {
const idFilter = createFilter(include, exclude, options);
function externalAssets(options: ExternalAssetsOptions): Plugin;

/**
* Make assets external but include them in the output.
* @param pattern A pattern, or array of patterns, to match files the plugin should ignore.
*/
function externalAssets(pattern: FilterPattern): Plugin;

function externalAssets(arg: FilterPattern | ExternalAssetsOptions): Plugin {
let idFilter: ReturnType<typeof createFilter>;

if (isFilterPattern(arg)) {
idFilter = createFilter(arg);
} else {
const { include, exclude, resolve } = arg;
idFilter = createFilter(include, exclude, { resolve });
}

const assets = new Map<string, Buffer>();

return {
Expand Down Expand Up @@ -145,3 +160,6 @@ export default function externalAssets(
},
};
}

export { externalAssets };
export default externalAssets;

0 comments on commit df7ad53

Please sign in to comment.