Skip to content

Commit

Permalink
refactor: vite-plugin-rsc-rsdw and related stuff (dai-shi#846)
Browse files Browse the repository at this point in the history
While working on dai-shi#841, I noticed it's a valid hack for now. So,
simplified a little bit.
  • Loading branch information
dai-shi authored Aug 21, 2024
1 parent 3453849 commit 22a7437
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/waku/src/lib/builder/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ const buildServerBundle = async (
}),
rscEntriesPlugin({
srcDir: config.srcDir,
ssrDir: DIST_SSR,
moduleMap: {
...Object.fromEntries(
Object.keys(SERVER_MODULE_MAP).map((key) => [key, `./${key}.js`]),
Expand Down
8 changes: 4 additions & 4 deletions packages/waku/src/lib/middleware/dev-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ export const devServer: Middleware = (options) => {
const env = options.env || {};
const configPromise = resolveConfig(options.config);

(globalThis as any).__WAKU_SERVER_HACK_IMPORT__ = (idOrFileURL: string) =>
loadServerModuleRsc(idOrFileURL);
(globalThis as any).__WAKU_SERVER_IMPORT__ = (id: string) =>
loadServerModuleRsc(id);

(globalThis as any).__WAKU_CLIENT_HACK_IMPORT__ = (idOrFileURL: string) =>
loadServerModuleMain(idOrFileURL);
(globalThis as any).__WAKU_CLIENT_IMPORT__ = (id: string) =>
loadServerModuleMain(id);

const {
vitePromise,
Expand Down
4 changes: 3 additions & 1 deletion packages/waku/src/lib/plugins/vite-plugin-rsc-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const CONFIG_FILE = 'waku.config.ts'; // XXX only ts extension

export function rscEntriesPlugin(opts: {
srcDir: string;
ssrDir: string;
moduleMap: Record<string, string>;
}): Plugin {
const codeToPrepend = `
Expand All @@ -34,7 +35,8 @@ export function loadModule(id) {
default: throw new Error('Cannot find module: ' + id);
}
}
globalThis.__WAKU_SERVER_HACK_IMPORT__ = loadModule;
globalThis.__WAKU_SERVER_IMPORT__ = loadModule;
globalThis.__WAKU_CLIENT_IMPORT__ = (id) => loadModule('${opts.ssrDir}/' + id);
`;
let entriesFile = '';
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/lib/plugins/vite-plugin-rsc-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ ${DEFAULT_HTML_HEAD}
// HACK: vite won't inject __vite__injectQuery anymore
// Vite optimizes `import()` so it adds `?import` to imported urls. That'd cause double module hazard! This way, I hack it to use a global function so it does not get optimized.
children: `
globalThis.__WAKU_CLIENT_HACK_IMPORT__ = (id) => import(id);
globalThis.__WAKU_CLIENT_IMPORT__ = (id) => import(id);
`,
},
...(opts.cssAssets || []).map((href) => ({
Expand Down
7 changes: 2 additions & 5 deletions packages/waku/src/lib/plugins/vite-plugin-rsc-rsdw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ const patchRsdw = (code: string, type: 'SERVER' | 'CLIENT') => {
`
globalThis.__WAKU_${type}_MODULE_LOADING__ ||= new Map();
globalThis.__WAKU_${type}_MODULE_CACHE__ ||= new Map();
globalThis.__WAKU_${type}_CHUNK_LOAD__ ||= (
id,
customImport = globalThis.__WAKU_${type}_HACK_IMPORT__
) => {
globalThis.__WAKU_${type}_CHUNK_LOAD__ ||= (id) => {
if (!globalThis.__WAKU_${type}_MODULE_LOADING__.has(id)) {
globalThis.__WAKU_${type}_MODULE_LOADING__.set(
id,
(customImport ? customImport(id) : import(id)).then((m) => {
globalThis.__WAKU_${type}_IMPORT__(id).then((m) => {
globalThis.__WAKU_${type}_MODULE_CACHE__.set(id, m);
})
);
Expand Down
5 changes: 1 addition & 4 deletions packages/waku/src/lib/renderers/html-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { encodeInput, hasStatusCode } from './utils.js';

// HACK depending on these constants is not ideal
import { SRC_MAIN } from '../plugins/vite-plugin-rsc-managed.js';
import { DIST_SSR } from '../builder/constants.js';
import { DEFAULT_HTML_HEAD } from '../plugins/vite-plugin-rsc-index.js';

export const CLIENT_MODULE_MAP = {
Expand Down Expand Up @@ -273,9 +272,7 @@ export const renderHtml = async (
}
// !isDev
const id = filePath.slice(config.basePath.length);
(globalThis as any).__WAKU_CLIENT_CHUNK_LOAD__(id, (id: string) =>
opts.loadModule(joinPath(DIST_SSR, id)),
);
(globalThis as any).__WAKU_CLIENT_CHUNK_LOAD__(id);
return { id, chunks: [id], name };
},
},
Expand Down

0 comments on commit 22a7437

Please sign in to comment.