Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rollup): allow regular expressions in externals array #145

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dts from "rollup-plugin-dts";
import replace from "@rollup/plugin-replace";
import { resolve, dirname, normalize, extname, isAbsolute } from "pathe";
import { resolvePath, resolveModuleExportNames } from "mlly";
import { getpkg, tryResolve, warn } from "../utils";
import { arrayIncludes, getpkg, tryResolve, warn } from "../utils";
import type { BuildContext } from "../types";
import { esbuild } from "./plugins/esbuild";
import { JSONPlugin } from "./plugins/json";
Expand Down Expand Up @@ -227,7 +227,7 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {

external(id) {
const pkg = getpkg(id);
const isExplicitExternal = ctx.options.externals.includes(pkg);
const isExplicitExternal = arrayIncludes(ctx.options.externals, pkg);
if (isExplicitExternal) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface BuildOptions {
declaration?: boolean;
outDir: string;
stub: boolean;
externals: string[];
externals: (string|RegExp)[];
dependencies: string[];
peerDependencies: string[];
devDependencies: string[];
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,7 @@ export function extractExportFilenames(
)
);
}

export function arrayIncludes (arr: (string|RegExp)[], searchElement: string) {
return arr.some(entry => entry instanceof RegExp ? entry.test(searchElement) : entry === searchElement);
}
4 changes: 2 additions & 2 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from "node:fs";
import chalk from "chalk";
import { resolve } from "pathe";
import { PackageJson } from "pkg-types";
import { extractExportFilenames, getpkg, warn } from "./utils";
import { arrayIncludes, extractExportFilenames, getpkg, warn } from "./utils";
import { BuildContext } from "./types";

export function validateDependencies(ctx: BuildContext) {
Expand All @@ -22,7 +22,7 @@ export function validateDependencies(ctx: BuildContext) {
}
for (const id of usedDependencies) {
if (
!ctx.options.externals.includes(id) &&
!arrayIncludes(ctx.options.externals, id) &&
!id.startsWith("chunks/") &&
!ctx.options.dependencies.includes(getpkg(id)) &&
!ctx.options.peerDependencies.includes(getpkg(id))
Expand Down
13 changes: 12 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import { extractExportFilenames, inferExportType } from "../src/utils";
import { arrayIncludes, extractExportFilenames, inferExportType } from "../src/utils";

describe("inferExportType", () => {
it("infers export type by condition", () => {
Expand Down Expand Up @@ -38,3 +38,14 @@ describe("extractExportFilenames", () => {
]);
});
});

describe("arrayIncludes", () => {
it("handles strings", () => {
expect(arrayIncludes(["test1", "test2"], "test1")).to.eq(true);
expect(arrayIncludes(["test1", "test2"], "test3")).to.eq(false);
});
it("handles regular expressions", () => {
expect(arrayIncludes([/t1$/, "test2"], "test1")).to.eq(true);
expect(arrayIncludes([/t3$/, "test2"], "test1")).to.eq(false);
});
});