Skip to content

Commit

Permalink
simplify buildOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Feb 1, 2025
1 parent e396405 commit e8a514f
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 64 deletions.
23 changes: 11 additions & 12 deletions packages/waku/src/lib/builder/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Config } from '../../config.js';
import {
setAllEnvInternal,
iterateSerializablePlatformDataInternal,
unstable_getPlatformObject,
unstable_getBuildOptions,
} from '../../server.js';
import type { EntriesPrd } from '../types.js';
import type { ResolvedConfig } from '../config.js';
Expand Down Expand Up @@ -705,14 +705,13 @@ export async function build(options: {
).root;
const distEntriesFile = joinPath(rootDir, config.distDir, DIST_ENTRIES_JS);

const platformObject = unstable_getPlatformObject();
platformObject.buildOptions ||= {};
platformObject.buildOptions.deploy = options.deploy;
const buildOptions = unstable_getBuildOptions();
buildOptions.deploy = options.deploy;

platformObject.buildOptions.unstable_phase = 'analyzeEntries';
buildOptions.unstable_phase = 'analyzeEntries';
const { clientEntryFiles, serverEntryFiles, serverModuleFiles } =
await analyzeEntries(rootDir, config);
platformObject.buildOptions.unstable_phase = 'buildServerBundle';
buildOptions.unstable_phase = 'buildServerBundle';
const serverBuildOutput = await buildServerBundle(
rootDir,
env,
Expand All @@ -722,7 +721,7 @@ export async function build(options: {
serverModuleFiles,
!!options.partial,
);
platformObject.buildOptions.unstable_phase = 'buildSsrBundle';
buildOptions.unstable_phase = 'buildSsrBundle';
await buildSsrBundle(
rootDir,
env,
Expand All @@ -732,7 +731,7 @@ export async function build(options: {
serverBuildOutput,
!!options.partial,
);
platformObject.buildOptions.unstable_phase = 'buildClientBundle';
buildOptions.unstable_phase = 'buildClientBundle';
const clientBuildOutput = await buildClientBundle(
rootDir,
env,
Expand All @@ -742,7 +741,7 @@ export async function build(options: {
serverBuildOutput,
!!options.partial,
);
delete platformObject.buildOptions.unstable_phase;
delete buildOptions.unstable_phase;

const distEntries: EntriesPrd = await import(
filePathToFileURL(distEntriesFile)
Expand All @@ -752,7 +751,7 @@ export async function build(options: {
const cssAssets = clientBuildOutput.output.flatMap(({ type, fileName }) =>
type === 'asset' && fileName.endsWith('.css') ? [fileName] : [],
);
platformObject.buildOptions.unstable_phase = 'emitStaticFiles';
buildOptions.unstable_phase = 'emitStaticFiles';
await emitStaticFiles(
rootDir,
config,
Expand All @@ -761,9 +760,9 @@ export async function build(options: {
cssAssets,
);

platformObject.buildOptions.unstable_phase = 'buildDeploy';
buildOptions.unstable_phase = 'buildDeploy';
await buildDeploy(rootDir, config);
delete platformObject.buildOptions.unstable_phase;
delete buildOptions.unstable_phase;

if (existsSync(distEntriesFile)) {
const DIST_PLATFORM_DATA = 'platform-data';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'node:path';
import { writeFileSync } from 'node:fs';
import type { Plugin } from 'vite';

import { unstable_getPlatformObject } from '../../server.js';
import { unstable_getBuildOptions } from '../../server.js';
import { SRC_ENTRIES } from '../constants.js';
import { DIST_PUBLIC } from '../builder/constants.js';

Expand Down Expand Up @@ -57,12 +57,12 @@ export function deployAwsLambdaPlugin(opts: {
srcDir: string;
distDir: string;
}): Plugin {
const platformObject = unstable_getPlatformObject();
const buildOptions = unstable_getBuildOptions();
let entriesFile: string;
return {
name: 'deploy-aws-lambda-plugin',
config(viteConfig) {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (unstable_phase !== 'buildServerBundle' || deploy !== 'aws-lambda') {
return;
}
Expand All @@ -85,7 +85,7 @@ export function deployAwsLambdaPlugin(opts: {
}
},
closeBundle() {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (unstable_phase !== 'buildDeploy' || deploy !== 'aws-lambda') {
return;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { Plugin } from 'vite';

import {
iterateSerializablePlatformDataInternal,
unstable_getPlatformObject,
unstable_getBuildOptions,
} from '../../server.js';
import { SRC_ENTRIES } from '../constants.js';
import { DIST_ENTRIES_JS, DIST_PUBLIC } from '../builder/constants.js';
Expand Down Expand Up @@ -150,13 +150,13 @@ export function deployCloudflarePlugin(opts: {
distDir: string;
privateDir: string;
}): Plugin {
const platformObject = unstable_getPlatformObject();
const buildOptions = unstable_getBuildOptions();
let rootDir: string;
let entriesFile: string;
return {
name: 'deploy-cloudflare-plugin',
config(viteConfig) {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (unstable_phase !== 'buildServerBundle' || deploy !== 'cloudflare') {
return;
}
Expand All @@ -168,7 +168,7 @@ export function deployCloudflarePlugin(opts: {
configResolved(config) {
rootDir = config.root;
entriesFile = `${rootDir}/${opts.srcDir}/${SRC_ENTRIES}`;
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (
(unstable_phase !== 'buildServerBundle' &&
unstable_phase !== 'buildSsrBundle') ||
Expand All @@ -194,7 +194,7 @@ export function deployCloudflarePlugin(opts: {
}
},
closeBundle() {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (unstable_phase !== 'buildDeploy' || deploy !== 'cloudflare') {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Plugin } from 'vite';

import { unstable_getPlatformObject } from '../../server.js';
import { unstable_getBuildOptions } from '../../server.js';
import { SRC_ENTRIES } from '../constants.js';
import { DIST_PUBLIC } from '../builder/constants.js';

Expand Down Expand Up @@ -49,12 +49,12 @@ export function deployDenoPlugin(opts: {
srcDir: string;
distDir: string;
}): Plugin {
const platformObject = unstable_getPlatformObject();
const buildOptions = unstable_getBuildOptions();
let entriesFile: string;
return {
name: 'deploy-deno-plugin',
config(viteConfig) {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (unstable_phase !== 'buildServerBundle' || deploy !== 'deno') {
return;
}
Expand All @@ -65,7 +65,7 @@ export function deployDenoPlugin(opts: {
},
configResolved(config) {
entriesFile = `${config.root}/${opts.srcDir}/${SRC_ENTRIES}`;
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (
(unstable_phase !== 'buildServerBundle' &&
unstable_phase !== 'buildSsrBundle') ||
Expand Down
8 changes: 4 additions & 4 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'node:path';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import type { Plugin } from 'vite';

import { unstable_getPlatformObject } from '../../server.js';
import { unstable_getBuildOptions } from '../../server.js';
import { SRC_ENTRIES } from '../constants.js';
import { DIST_PUBLIC } from '../builder/constants.js';

Expand Down Expand Up @@ -40,13 +40,13 @@ export function deployNetlifyPlugin(opts: {
distDir: string;
privateDir: string;
}): Plugin {
const platformObject = unstable_getPlatformObject();
const buildOptions = unstable_getBuildOptions();
let rootDir: string;
let entriesFile: string;
return {
name: 'deploy-netlify-plugin',
config(viteConfig) {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (
unstable_phase !== 'buildServerBundle' ||
(deploy !== 'netlify-functions' && deploy !== 'netlify-static')
Expand All @@ -73,7 +73,7 @@ export function deployNetlifyPlugin(opts: {
}
},
closeBundle() {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (
unstable_phase !== 'buildDeploy' ||
(deploy !== 'netlify-functions' && deploy !== 'netlify-static')
Expand Down
10 changes: 5 additions & 5 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-partykit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'node:path';
import { existsSync, writeFileSync } from 'node:fs';
import type { Plugin } from 'vite';

import { unstable_getPlatformObject } from '../../server.js';
import { unstable_getBuildOptions } from '../../server.js';
import { SRC_ENTRIES } from '../constants.js';
import { DIST_PUBLIC } from '../builder/constants.js';

Expand Down Expand Up @@ -58,13 +58,13 @@ export function deployPartykitPlugin(opts: {
srcDir: string;
distDir: string;
}): Plugin {
const platformObject = unstable_getPlatformObject();
const buildOptions = unstable_getBuildOptions();
let rootDir: string;
let entriesFile: string;
return {
name: 'deploy-partykit-plugin',
config(viteConfig) {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (unstable_phase !== 'buildServerBundle' || deploy !== 'partykit') {
return;
}
Expand All @@ -76,7 +76,7 @@ export function deployPartykitPlugin(opts: {
configResolved(config) {
rootDir = config.root;
entriesFile = `${rootDir}/${opts.srcDir}/${SRC_ENTRIES}`;
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (
(unstable_phase !== 'buildServerBundle' &&
unstable_phase !== 'buildSsrBundle') ||
Expand All @@ -102,7 +102,7 @@ export function deployPartykitPlugin(opts: {
}
},
closeBundle() {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (unstable_phase !== 'buildDeploy' || deploy !== 'partykit') {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'node:path';
import { cpSync, existsSync, mkdirSync, writeFileSync } from 'node:fs';
import type { Plugin } from 'vite';

import { unstable_getPlatformObject } from '../../server.js';
import { unstable_getBuildOptions } from '../../server.js';
import { SRC_ENTRIES } from '../constants.js';
import { DIST_PUBLIC } from '../builder/constants.js';

Expand Down Expand Up @@ -52,13 +52,13 @@ export function deployVercelPlugin(opts: {
rscBase: string;
privateDir: string;
}): Plugin {
const platformObject = unstable_getPlatformObject();
const buildOptions = unstable_getBuildOptions();
let rootDir: string;
let entriesFile: string;
return {
name: 'deploy-vercel-plugin',
config(viteConfig) {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (
unstable_phase !== 'buildServerBundle' ||
(deploy !== 'vercel-serverless' && deploy !== 'vercel-static')
Expand All @@ -85,7 +85,7 @@ export function deployVercelPlugin(opts: {
}
},
closeBundle() {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
const { deploy, unstable_phase } = buildOptions;
if (
unstable_phase !== 'buildDeploy' ||
(deploy !== 'vercel-serverless' && deploy !== 'vercel-static')
Expand Down
6 changes: 3 additions & 3 deletions packages/waku/src/router/fs-router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
unstable_getPlatformData,
unstable_setPlatformData,
unstable_getPlatformObject,
unstable_getBuildOptions,
} from '../server.js';
import { createPages, METHODS } from './create-pages.js';
import type { Method } from './create-pages.js';
Expand All @@ -15,7 +15,7 @@ export function fsRouter(
loadPage: (file: string) => Promise<any> | undefined,
pages: string,
) {
const platformObject = unstable_getPlatformObject();
const buildOptions = unstable_getBuildOptions();
return createPages(
async ({ createPage, createLayout, createRoot, createApi }) => {
let files = await unstable_getPlatformData<string[]>('fsRouterFiles');
Expand Down Expand Up @@ -58,7 +58,7 @@ export function fsRouter(
});
}
// build only - skip in dev
if (platformObject.buildOptions?.unstable_phase) {
if (buildOptions.unstable_phase) {
await unstable_setPlatformData('fsRouterFiles', files, true);
}
for (const file of files) {
Expand Down
44 changes: 21 additions & 23 deletions packages/waku/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,29 @@ export function unstable_getHeaders(): Readonly<Record<string, string>> {
return getContext().req.headers;
}

type PlatformObject = {
buildOptions?: {
deploy?:
| 'vercel-static'
| 'vercel-serverless'
| 'netlify-static'
| 'netlify-functions'
| 'cloudflare'
| 'partykit'
| 'deno'
| 'aws-lambda'
| undefined;
unstable_phase?:
| 'analyzeEntries'
| 'buildServerBundle'
| 'buildSsrBundle'
| 'buildClientBundle'
| 'buildDeploy'
| 'emitStaticFiles';
};
} & Record<string, unknown>;
type BuildOptions = {
deploy?:
| 'vercel-static'
| 'vercel-serverless'
| 'netlify-static'
| 'netlify-functions'
| 'cloudflare'
| 'partykit'
| 'deno'
| 'aws-lambda'
| undefined;
unstable_phase?:
| 'analyzeEntries'
| 'buildServerBundle'
| 'buildSsrBundle'
| 'buildClientBundle'
| 'buildDeploy'
| 'emitStaticFiles';
};

// TODO tentative name
export function unstable_getPlatformObject(): PlatformObject {
return ((globalThis as any).__WAKU_PLATFORM_OBJECT__ ||= {});
export function unstable_getBuildOptions(): BuildOptions {
return ((globalThis as any).__WAKU_BUILD_OPTIONS__ ||= {});
}

export function unstable_createAsyncIterable<T extends () => unknown>(
Expand Down

0 comments on commit e8a514f

Please sign in to comment.