Skip to content

Commit

Permalink
feat: deploy plugins (dai-shi#866)
Browse files Browse the repository at this point in the history
This PR moves deploy functions to vite plugins. It doesn't change the
way it works. Follow-up PRs may work on it.
  • Loading branch information
dai-shi authored Sep 10, 2024
1 parent 419c717 commit caf582d
Show file tree
Hide file tree
Showing 14 changed files with 729 additions and 359 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ Note: When rendering in static mode, please be sure to return `render: 'static'`

```
npm run build -- --with-cloudflare
npx wrangler dev # or deploy
npx wrangler pages dev # or deploy
```

### PartyKit (experimental)
Expand Down
127 changes: 69 additions & 58 deletions packages/waku/src/lib/builder/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,21 @@ import { rscAnalyzePlugin } from '../plugins/vite-plugin-rsc-analyze.js';
import { nonjsResolvePlugin } from '../plugins/vite-plugin-nonjs-resolve.js';
import { rscTransformPlugin } from '../plugins/vite-plugin-rsc-transform.js';
import { rscEntriesPlugin } from '../plugins/vite-plugin-rsc-entries.js';
import { rscServePlugin } from '../plugins/vite-plugin-rsc-serve.js';
import { rscEnvPlugin } from '../plugins/vite-plugin-rsc-env.js';
import { rscPrivatePlugin } from '../plugins/vite-plugin-rsc-private.js';
import { rscManagedPlugin } from '../plugins/vite-plugin-rsc-managed.js';
import { emitVercelOutput } from './output-vercel.js';
import { emitNetlifyOutput } from './output-netlify.js';
import { emitCloudflareOutput } from './output-cloudflare.js';
import { emitPartyKitOutput } from './output-partykit.js';
import { emitAwsLambdaOutput } from './output-aws-lambda.js';
import {
DIST_ENTRIES_JS,
DIST_SERVE_JS,
DIST_PUBLIC,
DIST_ASSETS,
DIST_SSR,
} from './constants.js';
import { deployVercelPlugin } from '../plugins/vite-plugin-deploy-vercel.js';
import { deployNetlifyPlugin } from '../plugins/vite-plugin-deploy-netlify.js';
import { deployCloudflarePlugin } from '../plugins/vite-plugin-deploy-cloudflare.js';
import { deployDenoPlugin } from '../plugins/vite-plugin-deploy-deno.js';
import { deployPartykitPlugin } from '../plugins/vite-plugin-deploy-partykit.js';
import { deployAwsLambdaPlugin } from '../plugins/vite-plugin-deploy-aws-lambda.js';

// TODO this file and functions in it are too long. will fix.

Expand All @@ -84,6 +83,15 @@ const onwarn = (warning: RollupLog, defaultHandler: LoggingFunction) => {
defaultHandler(warning);
};

const deployPlugins = (config: ResolvedConfig) => [
deployVercelPlugin(config),
deployNetlifyPlugin(config),
deployCloudflarePlugin(config),
deployDenoPlugin(config),
deployPartykitPlugin(config),
deployAwsLambdaPlugin(config),
];

const analyzeEntries = async (rootDir: string, config: ResolvedConfig) => {
const wakuClientDist = decodeFilePathFromAbsolute(
joinPath(fileURLToFilePath(import.meta.url), '../../../client.js'),
Expand Down Expand Up @@ -183,14 +191,6 @@ const buildServerBundle = async (
clientEntryFiles: Record<string, string>,
serverEntryFiles: Record<string, string>,
serverModuleFiles: Record<string, string>,
serve:
| 'vercel'
| 'netlify'
| 'cloudflare'
| 'partykit'
| 'deno'
| 'aws-lambda'
| false,
isNodeCompatible: boolean,
partial: boolean,
) => {
Expand Down Expand Up @@ -237,22 +237,7 @@ const buildServerBundle = async (
),
},
}),
...(serve
? [
rscServePlugin({
...config,
distServeJs: DIST_SERVE_JS,
distPublic: DIST_PUBLIC,
srcServeFile: decodeFilePathFromAbsolute(
joinPath(
fileURLToFilePath(import.meta.url),
`../serve-${serve}.js`,
),
),
serve,
}),
]
: []),
...deployPlugins(config),
],
ssr: isNodeCompatible
? {
Expand Down Expand Up @@ -670,6 +655,51 @@ export const publicIndexHtml = ${JSON.stringify(publicIndexHtml)};
await appendFile(distEntriesFile, code);
};

// For Deploy
const buildDeploy = async (rootDir: string, config: ResolvedConfig) => {
const DUMMY = 'dummy-entry';
await buildVite({
plugins: [
{
// FIXME This is too hacky. There must be a better way.
name: 'dummy-entry-plugin',
resolveId(source) {
if (source === DUMMY) {
return source;
}
},
load(id) {
if (id === DUMMY) {
return '';
}
},
generateBundle(_options, bundle) {
Object.entries(bundle).forEach(([key, value]) => {
if (value.name === DUMMY) {
delete bundle[key];
}
});
},
},
...deployPlugins(config),
],
publicDir: false,
build: {
emptyOutDir: false,
ssr: true,
rollupOptions: {
onwarn: (warning, warn) => {
if (!warning.message.startsWith('Generated an empty chunk:')) {
warn(warning);
}
},
input: { [DUMMY]: DUMMY },
},
outDir: joinPath(rootDir, config.distDir),
},
});
};

export async function build(options: {
config: Config;
env?: Record<string, string>;
Expand Down Expand Up @@ -700,24 +730,21 @@ export async function build(options: {
platformObject.buildOptions ||= {};
platformObject.buildOptions.deploy = options.deploy;

platformObject.buildOptions.unstable_phase = 'analyzeEntries';
const { clientEntryFiles, serverEntryFiles, serverModuleFiles } =
await analyzeEntries(rootDir, config);
platformObject.buildOptions.unstable_phase = 'buildServerBundle';
const serverBuildOutput = await buildServerBundle(
rootDir,
env,
config,
clientEntryFiles,
serverEntryFiles,
serverModuleFiles,
(options.deploy === 'vercel-serverless' ? 'vercel' : false) ||
(options.deploy === 'netlify-functions' ? 'netlify' : false) ||
(options.deploy === 'cloudflare' ? 'cloudflare' : false) ||
(options.deploy === 'partykit' ? 'partykit' : false) ||
(options.deploy === 'deno' ? 'deno' : false) ||
(options.deploy === 'aws-lambda' ? 'aws-lambda' : false),
isNodeCompatible,
!!options.partial,
);
platformObject.buildOptions.unstable_phase = 'buildSsrBundle';
await buildSsrBundle(
rootDir,
env,
Expand All @@ -728,6 +755,7 @@ export async function build(options: {
isNodeCompatible,
!!options.partial,
);
platformObject.buildOptions.unstable_phase = 'buildClientBundle';
const clientBuildOutput = await buildClientBundle(
rootDir,
env,
Expand All @@ -737,6 +765,7 @@ export async function build(options: {
serverBuildOutput,
!!options.partial,
);
delete platformObject.buildOptions.unstable_phase;

const distEntries = await import(filePathToFileURL(distEntriesFile));

Expand All @@ -763,27 +792,9 @@ export async function build(options: {
clientBuildOutput,
);

if (options.deploy?.startsWith('vercel-')) {
await emitVercelOutput(
rootDir,
config,
DIST_SERVE_JS,
options.deploy.slice('vercel-'.length) as 'static' | 'serverless',
);
} else if (options.deploy?.startsWith('netlify-')) {
await emitNetlifyOutput(
rootDir,
config,
DIST_SERVE_JS,
options.deploy.slice('netlify-'.length) as 'static' | 'functions',
);
} else if (options.deploy === 'cloudflare') {
await emitCloudflareOutput(rootDir, config, DIST_SERVE_JS);
} else if (options.deploy === 'partykit') {
await emitPartyKitOutput(rootDir, config, DIST_SERVE_JS);
} else if (options.deploy === 'aws-lambda') {
await emitAwsLambdaOutput(config);
}
platformObject.buildOptions.unstable_phase = 'buildDeploy';
await buildDeploy(rootDir, config);
delete platformObject.buildOptions.unstable_phase;

await appendFile(
distEntriesFile,
Expand Down
10 changes: 0 additions & 10 deletions packages/waku/src/lib/builder/output-aws-lambda.ts

This file was deleted.

108 changes: 0 additions & 108 deletions packages/waku/src/lib/builder/output-cloudflare.ts

This file was deleted.

52 changes: 0 additions & 52 deletions packages/waku/src/lib/builder/output-netlify.ts

This file was deleted.

Loading

0 comments on commit caf582d

Please sign in to comment.