Skip to content

Commit bc938e0

Browse files
authored
fix(vercel): emit platform data (#1226)
close #1225
1 parent 7da850e commit bc938e0

File tree

4 files changed

+45
-74
lines changed

4 files changed

+45
-74
lines changed

packages/waku/src/lib/builder/build.ts

+3-36
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import type { LoggingFunction, RollupLog } from 'rollup';
77
import type { ReactNode } from 'react';
88

99
import type { Config } from '../../config.js';
10-
import {
11-
INTERNAL_setAllEnv,
12-
INTERNAL_iterateSerializablePlatformData,
13-
unstable_getBuildOptions,
14-
} from '../../server.js';
10+
import { INTERNAL_setAllEnv, unstable_getBuildOptions } from '../../server.js';
1511
import type { EntriesPrd } from '../types.js';
1612
import type { ResolvedConfig } from '../config.js';
1713
import { resolveConfig } from '../config.js';
@@ -65,6 +61,7 @@ import { deployCloudflarePlugin } from '../plugins/vite-plugin-deploy-cloudflare
6561
import { deployDenoPlugin } from '../plugins/vite-plugin-deploy-deno.js';
6662
import { deployPartykitPlugin } from '../plugins/vite-plugin-deploy-partykit.js';
6763
import { deployAwsLambdaPlugin } from '../plugins/vite-plugin-deploy-aws-lambda.js';
64+
import { emitPlatformData } from './platform-data.js';
6865

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

@@ -765,36 +762,6 @@ export async function build(options: {
765762
delete buildOptions.unstable_phase;
766763

767764
if (existsSync(distEntriesFile)) {
768-
const DIST_PLATFORM_DATA = 'platform-data';
769-
const keys = new Set<string>();
770-
await mkdir(joinPath(rootDir, config.distDir, DIST_PLATFORM_DATA), {
771-
recursive: true,
772-
});
773-
for (const [key, data] of INTERNAL_iterateSerializablePlatformData()) {
774-
keys.add(key);
775-
const destFile = joinPath(
776-
rootDir,
777-
config.distDir,
778-
DIST_PLATFORM_DATA,
779-
key + '.js',
780-
);
781-
await writeFile(destFile, `export default ${JSON.stringify(data)};`);
782-
}
783-
await appendFile(
784-
distEntriesFile,
785-
`
786-
export function loadPlatformData(key) {
787-
switch (key) {
788-
${Array.from(keys)
789-
.map(
790-
(k) =>
791-
`case '${k}': return import('./${DIST_PLATFORM_DATA}/${k}.js').then((m) => m.default);`,
792-
)
793-
.join('\n')}
794-
default: throw new Error('Cannot find platform data: ' + key);
795-
}
796-
}
797-
`,
798-
);
765+
await emitPlatformData(joinPath(rootDir, config.distDir));
799766
}
800767
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { INTERNAL_iterateSerializablePlatformData } from '../../server.js';
2+
import { joinPath } from '../utils/path.js';
3+
import { appendFile, mkdir, writeFile } from '../utils/node-fs.js';
4+
import { DIST_ENTRIES_JS } from './constants.js';
5+
6+
const DIST_PLATFORM_DATA = 'platform-data';
7+
8+
export const emitPlatformData = async (distDir: string) => {
9+
const keys = new Set<string>();
10+
await mkdir(joinPath(distDir, DIST_PLATFORM_DATA), {
11+
recursive: true,
12+
});
13+
for (const [key, data] of INTERNAL_iterateSerializablePlatformData()) {
14+
keys.add(key);
15+
const destFile = joinPath(distDir, DIST_PLATFORM_DATA, key + '.js');
16+
await writeFile(destFile, `export default ${JSON.stringify(data)};`);
17+
}
18+
await appendFile(
19+
joinPath(distDir, DIST_ENTRIES_JS),
20+
`
21+
export function loadPlatformData(key) {
22+
switch (key) {
23+
${Array.from(keys)
24+
.map(
25+
(k) =>
26+
`case '${k}': return import('./${DIST_PLATFORM_DATA}/${k}.js').then((m) => m.default);`,
27+
)
28+
.join('\n')}
29+
default: throw new Error('Cannot find platform data: ' + key);
30+
}
31+
}
32+
`,
33+
);
34+
};

packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts

+5-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { randomBytes } from 'node:crypto';
22
import {
3-
appendFileSync,
43
copyFileSync,
54
existsSync,
65
mkdirSync,
@@ -13,11 +12,9 @@ import path from 'node:path';
1312

1413
import type { Plugin } from 'vite';
1514

16-
import {
17-
INTERNAL_iterateSerializablePlatformData,
18-
unstable_getBuildOptions,
19-
} from '../../server.js';
20-
import { DIST_ENTRIES_JS, DIST_PUBLIC } from '../builder/constants.js';
15+
import { emitPlatformData } from '../builder/platform-data.js';
16+
import { unstable_getBuildOptions } from '../../server.js';
17+
import { DIST_PUBLIC } from '../builder/constants.js';
2118
import { SRC_ENTRIES } from '../constants.js';
2219

2320
const SERVE_JS = 'serve-cloudflare.js';
@@ -193,7 +190,7 @@ export function deployCloudflarePlugin(opts: {
193190
return getServeJsContent(entriesFile);
194191
}
195192
},
196-
closeBundle() {
193+
async closeBundle() {
197194
const { deploy, unstable_phase } = buildOptions;
198195
if (unstable_phase !== 'buildDeploy' || deploy !== 'cloudflare') {
199196
return;
@@ -210,36 +207,7 @@ export function deployCloudflarePlugin(opts: {
210207
functionDir: workerDistDir,
211208
});
212209

213-
const DIST_PLATFORM_DATA = 'platform-data';
214-
const keys = new Set<string>();
215-
mkdirSync(path.join(workerDistDir, DIST_PLATFORM_DATA), {
216-
recursive: true,
217-
});
218-
for (const [key, data] of INTERNAL_iterateSerializablePlatformData()) {
219-
keys.add(key);
220-
const destFile = path.join(
221-
workerDistDir,
222-
DIST_PLATFORM_DATA,
223-
key + '.js',
224-
);
225-
writeFileSync(destFile, `export default ${JSON.stringify(data)};`);
226-
}
227-
appendFileSync(
228-
path.join(workerDistDir, DIST_ENTRIES_JS),
229-
`
230-
export function loadPlatformData(key) {
231-
switch (key) {
232-
${Array.from(keys)
233-
.map(
234-
(k) =>
235-
`case '${k}': return import('./${DIST_PLATFORM_DATA}/${k}.js').then(m => m.default);`,
236-
)
237-
.join('\n')}
238-
default: throw new Error('Cannot find platform data: ' + key);
239-
}
240-
}
241-
`,
242-
);
210+
await emitPlatformData(workerDistDir);
243211

244212
const wranglerTomlFile = path.join(rootDir, 'wrangler.toml');
245213
if (!existsSync(wranglerTomlFile)) {

packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Plugin } from 'vite';
55
import { unstable_getBuildOptions } from '../../server.js';
66
import { SRC_ENTRIES } from '../constants.js';
77
import { DIST_PUBLIC } from '../builder/constants.js';
8+
import { emitPlatformData } from '../builder/platform-data.js';
89

910
const SERVE_JS = 'serve-vercel.js';
1011

@@ -84,7 +85,7 @@ export function deployVercelPlugin(opts: {
8485
return getServeJsContent(opts.distDir, DIST_PUBLIC, entriesFile);
8586
}
8687
},
87-
closeBundle() {
88+
async closeBundle() {
8889
const { deploy, unstable_phase } = buildOptions;
8990
if (
9091
unstable_phase !== 'buildDeploy' ||
@@ -112,6 +113,7 @@ export function deployVercelPlugin(opts: {
112113
path.join(serverlessDir, opts.distDir),
113114
{ recursive: true },
114115
);
116+
await emitPlatformData(path.join(serverlessDir, opts.distDir));
115117
if (existsSync(path.join(rootDir, opts.privateDir))) {
116118
cpSync(
117119
path.join(rootDir, opts.privateDir),

0 commit comments

Comments
 (0)