@cloudflare/vite-plugin
Version:
Cloudflare plugin for Vite
177 lines (174 loc) • 6.92 kB
text/typescript
import { Miniflare, Request, Response } from "miniflare";
import { Unstable_Config } from "wrangler";
import * as vite from "vite";
import { CompatDate } from "@cloudflare/workers-utils";
//#region src/utils.d.ts
type Defined<T> = Exclude<T, undefined>;
//#endregion
//#region src/workers-configs.d.ts
type WorkerConfig = Omit<Unstable_Config, keyof NonApplicableConfig>;
type NonApplicableWorkerConfigsInfo = typeof nonApplicableWorkerConfigs;
type NonApplicableConfig = NonApplicableConfigReplacedByVite | NonApplicableConfigNotRelevant;
type NonApplicableConfigReplacedByVite = keyof NonApplicableWorkerConfigsInfo["replacedByVite"];
type NonApplicableConfigNotRelevant = NonApplicableWorkerConfigsInfo["notRelevant"][number];
/**
* Set of worker config options that are not applicable when using Vite
*/
declare const nonApplicableWorkerConfigs: {
/**
* Object containing configs that have a vite replacement, the object's field contain details about the config's replacement
*/
readonly replacedByVite: {
readonly alias: {
readonly viteReplacement: "resolve.alias";
readonly viteDocs: "https://vite.dev/config/shared-options.html#resolve-alias";
};
readonly define: {
readonly viteReplacement: "define";
readonly viteDocs: "https://vite.dev/config/shared-options.html#define";
};
readonly minify: {
readonly viteReplacement: "build.minify";
readonly viteDocs: "https://vite.dev/config/build-options.html#build-minify";
};
};
/**
* All the configs that are not relevant when using Vite (meaning that in the context of a Vite
* application they lose their purpose/meaning)
*/
readonly notRelevant: readonly ["base_dir", "build", "find_additional_modules", "no_bundle", "preserve_file_names", "rules", "site", "tsconfig"];
/**
* Configs that are only supported on the entry worker and will be ignored on auxiliary workers
*/
readonly notSupportedOnAuxiliary: readonly ["assets"];
};
//#endregion
//#region src/plugin-config.d.ts
type PersistState = boolean | {
path: string;
};
type TunnelConfig = {
autoStart?: boolean;
name?: string;
};
interface BaseWorkerConfig {
viteEnvironment?: {
name?: string;
childEnvironments?: string[];
};
}
/**
* Whether this Worker is only used during development and should not be built for production.
* Can be a boolean or a function that returns a boolean. The function is evaluated lazily
* at build time, allowing frameworks to provide the value after initialization.
*/
type DevOnly = boolean | (() => boolean);
interface EntryWorkerConfig extends BaseWorkerConfig {
configPath?: string;
config?: WorkerConfigCustomizer<true>;
/**
* Whether the entry Worker should be omitted from the production build.
* Can be a boolean or a function that returns a boolean. The function is
* evaluated lazily at build time, allowing frameworks to provide the value
* after initialization.
*
* When set, an assets-only Wrangler config is emitted to the client output
* directory. This enables using server-side code in development but producing
* a fully static app for deployment.
*/
assetsOnly?: DevOnly;
}
interface AuxiliaryWorkerFileConfig extends BaseWorkerConfig {
configPath: string;
devOnly?: DevOnly;
}
interface AuxiliaryWorkerInlineConfig extends BaseWorkerConfig {
configPath?: string;
config: WorkerConfigCustomizer<false>;
devOnly?: DevOnly;
}
type AuxiliaryWorkerConfig = AuxiliaryWorkerFileConfig | AuxiliaryWorkerInlineConfig;
interface PrerenderWorkerFileConfig extends BaseWorkerConfig {
configPath: string;
}
interface PrerenderWorkerInlineConfig extends BaseWorkerConfig {
configPath?: string;
config: WorkerConfigCustomizer<false>;
}
type PrerenderWorkerConfig = PrerenderWorkerFileConfig | PrerenderWorkerInlineConfig;
interface ExperimentalNewConfig {
/** Options for type generation. */
types?: {
/**
* Whether to auto-generate `worker-configuration.d.ts` at the project
* root. Defaults to `true`.
*/
generate?: boolean;
};
/**
* Whether to emit the experimental Build Output API (`.cloudflare/output/v0/`)
* intended for consumption by the new `cf` CLI.
*/
cfBuildOutput?: boolean;
}
interface Experimental {
/** Experimental support for handling the _headers and _redirects files during Vite dev mode. */
headersAndRedirectsDevModeSupport?: boolean;
/** Experimental support for a dedicated prerender Worker */
prerenderWorker?: PrerenderWorkerConfig;
/**
* Experimental support for loading the entry Worker's configuration from
* `cloudflare.config.ts` instead of `wrangler.json` /
* `wrangler.jsonc` / `wrangler.toml`.
*
* Pass `true` for defaults, or an object to customize behaviour.
*/
newConfig?: boolean | ExperimentalNewConfig;
}
type FilteredEntryWorkerConfig = Omit<ResolvedAssetsOnlyConfig, "topLevelName" | "name">;
type WorkerConfigCustomizer<TIsEntryWorker extends boolean> = Partial<WorkerConfig> | ((...args: TIsEntryWorker extends true ? [config: WorkerConfig] : [config: WorkerConfig, {
entryWorkerConfig: FilteredEntryWorkerConfig;
}]) => Partial<WorkerConfig> | void);
interface PluginConfig extends EntryWorkerConfig {
auxiliaryWorkers?: AuxiliaryWorkerConfig[];
persistState?: PersistState;
inspectorPort?: number | false;
remoteBindings?: boolean;
tunnel?: boolean | TunnelConfig;
experimental?: Experimental;
}
interface ResolvedAssetsOnlyConfig extends WorkerConfig {
topLevelName: Defined<WorkerConfig["topLevelName"]>;
name: Defined<WorkerConfig["name"]>;
compatibility_date: Defined<WorkerConfig["compatibility_date"]>;
}
//#endregion
//#region src/index.d.ts
/**
* @deprecated Use today's date instead (as `YYYY-MM-DD`)
*
* Gets the compatibility date to use with the local workerd version.
*
* Note: the function's signature is as is because it needs to be backward compatibly with
* a previous iteration of this, it will be simplified in the next major version of this package.
*
* @param _options Unused argument (present only for backward compatibility)
* @returns Object containing the compatibility date (this is not the date directly for backward compatibility)
*/
declare function getLocalWorkerdCompatibilityDate(_options?: {
projectPath?: string;
}): {
date: CompatDate;
source: "workerd" | "fallback";
};
/**
* Vite plugin that enables a full-featured integration between Vite and the Cloudflare Workers runtime.
*
* See the [README](https://github.com/cloudflare/workers-sdk/tree/main/packages/vite-plugin-cloudflare#readme) for more details.
*
* @param pluginConfig An optional {@link PluginConfig} object.
*/
declare function cloudflare(pluginConfig?: PluginConfig): vite.Plugin[];
//#endregion
export { type PluginConfig, type WorkerConfig, cloudflare, getLocalWorkerdCompatibilityDate };
//# sourceMappingURL=index.d.mts.map