Skip to content

Commit c57a40e

Browse files
authored
refactor(build): dist entries file (#1292)
it's still a hack, but slightly less hacky.
1 parent c72d792 commit c57a40e

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
} from '../utils/path.js';
2323
import { extendViteConfig } from '../utils/vite-config.js';
2424
import {
25-
appendFile,
2625
copyFile,
2726
createWriteStream,
2827
existsSync,
@@ -629,11 +628,18 @@ const emitStaticFiles = async (
629628
}
630629
await waitForTasks();
631630
const dynamicHtmlPaths = Array.from(dynamicHtmlPathMap);
632-
const code = `
633-
export const dynamicHtmlPaths = ${JSON.stringify(dynamicHtmlPaths)};
634-
export const publicIndexHtml = ${JSON.stringify(defaultHtmlStr)};
635-
`;
636-
await appendFile(distEntriesFile, code);
631+
let distEntriesFileContent = await readFile(distEntriesFile, {
632+
encoding: 'utf8',
633+
});
634+
distEntriesFileContent = distEntriesFileContent.replace(
635+
'globalThis.__WAKU_DYNAMIC_HTML_PATHS__',
636+
JSON.stringify(dynamicHtmlPaths),
637+
);
638+
distEntriesFileContent = distEntriesFileContent.replace(
639+
'globalThis.__WAKU_PUBLIC_INDEX_HTML__',
640+
JSON.stringify(defaultHtmlStr),
641+
);
642+
await writeFile(distEntriesFile, distEntriesFileContent);
637643
};
638644

639645
// For Deploy

packages/waku/src/lib/builder/platform-data.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { INTERNAL_iterateSerializablePlatformData } from '../../server.js';
22
import { joinPath } from '../utils/path.js';
3-
import { appendFile, mkdir, writeFile } from '../utils/node-fs.js';
3+
import { mkdir, readFile, writeFile } from '../utils/node-fs.js';
44
import { DIST_ENTRIES_JS } from './constants.js';
55

66
const DIST_PLATFORM_DATA = 'platform-data';
@@ -15,10 +15,14 @@ export const emitPlatformData = async (distDir: string) => {
1515
const destFile = joinPath(distDir, DIST_PLATFORM_DATA, key + '.js');
1616
await writeFile(destFile, `export default ${JSON.stringify(data)};`);
1717
}
18-
await appendFile(
19-
joinPath(distDir, DIST_ENTRIES_JS),
18+
const distEntriesFile = joinPath(distDir, DIST_ENTRIES_JS);
19+
let distEntriesFileContent = await readFile(distEntriesFile, {
20+
encoding: 'utf8',
21+
});
22+
distEntriesFileContent = distEntriesFileContent.replace(
23+
'globalThis.__WAKU_LOAD_PLATFORM_DATA__',
2024
`
21-
export function loadPlatformData(key) {
25+
(key) => {
2226
switch (key) {
2327
${Array.from(keys)
2428
.map(
@@ -31,4 +35,5 @@ export function loadPlatformData(key) {
3135
}
3236
`,
3337
);
38+
await writeFile(distEntriesFile, distEntriesFileContent);
3439
};

packages/waku/src/lib/plugins/vite-plugin-rsc-entries.ts

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export function loadModule(id) {
3939
}
4040
globalThis.__WAKU_SERVER_IMPORT__ = loadModule;
4141
globalThis.__WAKU_CLIENT_IMPORT__ = (id) => loadModule('${opts.ssrDir}/' + id);
42+
export const dynamicHtmlPaths = globalThis.__WAKU_DYNAMIC_HTML_PATHS__;
43+
export const publicIndexHtml = globalThis.__WAKU_PUBLIC_INDEX_HTML__;
44+
export const loadPlatformData = globalThis.__WAKU_LOAD_PLATFORM_DATA__;
4245
`;
4346
let entriesFile = '';
4447
let configFile = '';

0 commit comments

Comments
 (0)