Skip to content

Commit

Permalink
New config API, wrangler.json output and updated preview mode (#84)
Browse files Browse the repository at this point in the history
* New config API, `wrangler.json` output and updated preview mode

* Use beta release of wrangler and miniflare

* Remove partial support for modes (Wrangler environments)
  • Loading branch information
jamesopstad authored Dec 6, 2024
1 parent 2876d04 commit 451d487
Show file tree
Hide file tree
Showing 50 changed files with 746 additions and 731 deletions.
2 changes: 1 addition & 1 deletion packages/vite-plugin-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@hattip/adapter-node": "^0.0.48",
"miniflare": "^3.20241106.0"
"miniflare": "beta"
},
"devDependencies": {
"@cloudflare/workers-shared": "^0.7.0",
Expand Down
9 changes: 0 additions & 9 deletions packages/vite-plugin-cloudflare/src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import type { Miniflare } from 'miniflare';

export interface AssetConfig {
htmlHandling?:
| 'auto-trailing-slash'
| 'force-trailing-slash'
| 'drop-trailing-slash'
| 'none';
notFoundHandling?: 'single-page-application' | '404-page' | 'none';
}

export const ROUTER_WORKER_NAME = '__router-worker__';
export const ASSET_WORKER_NAME = '__asset-worker__';
export const ASSET_WORKERS_COMPATIBILITY_DATE = '2024-10-04';
Expand Down
138 changes: 70 additions & 68 deletions packages/vite-plugin-cloudflare/src/cloudflare-environment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { builtinModules } from 'node:module';
import * as path from 'node:path';
import * as vite from 'vite';
import { getNodeCompatExternals } from './node-js-compat';
import { INIT_PATH, invariant, UNKNOWN_HOST } from './shared';
import { toMiniflareRequest } from './utils';
import type { NormalizedPluginConfig, WorkerOptions } from './plugin-config';
import type { ResolvedPluginConfig, WorkerConfig } from './plugin-config';
import type { Fetcher } from '@cloudflare/workers-types/experimental';
import type {
MessageEvent,
Expand Down Expand Up @@ -123,83 +122,86 @@ const cloudflareBuiltInModules = [
];

export function createCloudflareEnvironmentOptions(
options: WorkerOptions,
workerConfig: WorkerConfig,
userConfig: vite.UserConfig,
): vite.EnvironmentOptions {
return vite.mergeConfig(
{
resolve: {
// Note: in order for ssr pre-bundling to take effect we need to ask vite to treat all
// dependencies as not external
noExternal: true,
// We want to use `workerd` package exports if available (e.g. for postgres).
conditions: ['workerd', 'module', 'browser', 'development|production'],
return {
resolve: {
// Note: in order for ssr pre-bundling to take effect we need to ask vite to treat all
// dependencies as not external
noExternal: true,
// We want to use `workerd` package exports if available (e.g. for postgres).
conditions: ['workerd', 'module', 'browser', 'development|production'],
},
dev: {
createEnvironment(name, config) {
return new CloudflareDevEnvironment(name, config);
},
dev: {
createEnvironment(name, config) {
return new CloudflareDevEnvironment(name, config);
},
},
build: {
createEnvironment(name, config) {
return new vite.BuildEnvironment(name, config);
},
build: {
createEnvironment(name, config) {
return new vite.BuildEnvironment(name, config);
},
// This is a bit of a hack to make sure the user can't override the output directory at the environment level
outDir: userConfig.build?.outDir ?? 'dist',
ssr: true,
rollupOptions: {
// Note: vite starts dev pre-bundling crawling from either optimizeDeps.entries or rollupOptions.input
// so the input value here serves both as the build input as well as the starting point for
// dev pre-bundling crawling (were we not to set this input field we'd have to appropriately set
// optimizeDeps.entries in the dev config)
input: userConfig.root
? path.resolve(userConfig.root, options.main)
: path.resolve(options.main),
external: [...cloudflareBuiltInModules, ...getNodeCompatExternals()],
},
// This is a bit of a hack to make sure the user can't override the output directory at the environment level
outDir: userConfig.build?.outDir ?? 'dist',
ssr: true,
rollupOptions: {
// Note: vite starts dev pre-bundling crawling from either optimizeDeps.entries or rollupOptions.input
// so the input value here serves both as the build input as well as the starting point for
// dev pre-bundling crawling (were we not to set this input field we'd have to appropriately set
// optimizeDeps.entries in the dev config)
input: workerConfig.main,
external: [...cloudflareBuiltInModules, ...getNodeCompatExternals()],
},
optimizeDeps: {
// Note: ssr pre-bundling is opt-in, and we need to enabled it by setting noDiscovery to false
noDiscovery: false,
exclude: [
...cloudflareBuiltInModules,
// we have to exclude all node modules to work in dev-mode not just the unenv externals...
...builtinModules.concat(builtinModules.map((m) => `node:${m}`)),
},
optimizeDeps: {
// Note: ssr pre-bundling is opt-in and we need to enable it by setting `noDiscovery` to false
noDiscovery: false,
exclude: [
...cloudflareBuiltInModules,
// we have to exclude all node modules to work in dev-mode not just the unenv externals...
...builtinModules.concat(builtinModules.map((m) => `node:${m}`)),
],
esbuildOptions: {
platform: 'neutral',
resolveExtensions: [
'.mjs',
'.js',
'.mts',
'.ts',
'.jsx',
'.tsx',
'.json',
'.cjs',
'.cts',
'.ctx',
],
esbuildOptions: {
platform: 'neutral',
resolveExtensions: [
'.mjs',
'.js',
'.mts',
'.ts',
'.jsx',
'.tsx',
'.json',
'.cjs',
'.cts',
'.ctx',
],
},
},
keepProcessEnv: true,
} satisfies vite.EnvironmentOptions,
options.overrides ?? {},
);
},
keepProcessEnv: true,
};
}

export function initRunners(
normalizedPluginConfig: NormalizedPluginConfig,
miniflare: Miniflare,
resolvedPluginConfig: ResolvedPluginConfig,
viteDevServer: vite.ViteDevServer,
): Promise<void[]> {
return Promise.all(
Object.keys(normalizedPluginConfig.workers).map(async (name) => {
const worker = await miniflare.getWorker(name);
miniflare: Miniflare,
): Promise<void[]> | undefined {
if (resolvedPluginConfig.type === 'assets-only') {
return;
}

return (
viteDevServer.environments[name] as CloudflareDevEnvironment
).initRunner(worker);
}),
return Promise.all(
Object.entries(resolvedPluginConfig.workers).map(
async ([environmentName, workerConfig]) => {
const worker = await miniflare.getWorker(workerConfig.name);

return (
viteDevServer.environments[
environmentName
] as CloudflareDevEnvironment
).initRunner(worker);
},
),
);
}
Loading

0 comments on commit 451d487

Please sign in to comment.