1const POSTFIX_REGEX = /[?#].*$/;
2
3export function sanitizePath(path: string): string {
4 return path.replace(POSTFIX_REGEX, "");
5}
6
7/**
8 * Normalizes a path to use forward slashes. This ensures paths are valid
9 * module specifiers and produce consistent output across platforms (e.g. Windows,
10 * where `path` APIs return backslash-separated paths).
11 */
12export function toPosixPath(path: string): string {
13 return path.replace(/\\/g, "/");
14}
15
16export function hasNodejsCompat(
17 flags?: ReadonlyArray<string>,
18 compatibilityDate?: string,
19): boolean {
20 // Both nodejs_compat modes are runtime defaults from 2026-08-04, so builds
21 // for newer dates must enable the same transforms without a redundant flag.
22 if (
23 compatibilityDate !== undefined &&
24 compatibilityDate >= "2026-08-04" &&
25 !flags?.includes("no_nodejs_compat")
26 ) {
27 return true;
28 }
29 return (
30 flags?.some(
31 (flag) => flag === "nodejs_compat" || flag === "nodejs_compat_v2",
32 ) ?? false
33 );
34}
35
36export function hasNodejsAls(flags?: ReadonlyArray<string>): boolean {
37 return flags?.some((flag) => flag === "nodejs_als") ?? false;
38}
39
40export function resolvePluginApi<Api>(
41 plugins: ReadonlyArray<unknown>,
42 name: string,
43) {
44 return plugins.find(
45 (plugin): plugin is { name: string; api: Api } =>
46 typeof plugin === "object" &&
47 plugin !== null &&
48 "name" in plugin &&
49 plugin.name === name,
50 )?.api;
51}