Skip to content

Commit

Permalink
feat: migrate to vite v6 (#1012)
Browse files Browse the repository at this point in the history
Blocking issues:

- [x] `polyfillModulePreload is deprecated. Use modulePreload.polyfill
instead. (x6)`
- [x] rscRsdwPlugin parse code is somehow incorrect, I think the code it
receives is already a bundlered code, that's why `const index =
code.indexOf('function requireAsyncModule(id)');` will get `-1`
- [x] vite issue. vitejs/vite#18361 
- [x] no styling in dev. 21_create-pages_standalone spec fails.
- [x] react-error-boundary. ssr-catch-error spec fails.

Other issues:

- [x] proper fix for `resolve.conditions` hack
- [x] proper fix for `vite-loader.ts` hack
- [ ] drop `viteServer.hot`.
#1012 (comment)

---------

Co-authored-by: daishi <[email protected]>
Co-authored-by: Daishi Kato <[email protected]>
Co-authored-by: Tyler <[email protected]>
  • Loading branch information
4 people authored Dec 27, 2024
1 parent 25d418b commit ec87f81
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 315 deletions.
27 changes: 6 additions & 21 deletions e2e/fixtures/ssr-catch-error/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
export default ({ mode }: { mode: string }) => {
if (mode === 'development') {
return {
plugins: [
{
name: 'externalize-react-error-boundary',
configResolved(config: any) {
// TODO HACK temporary solution until v0.22.0
if (
config.cacheDir.endsWith(
'node_modules/.vite/waku-dev-server-main',
)
) {
config.ssr.noExternal = ['react-error-boundary'];
}
},
},
],
};
}
return {};
export default {
ssr: {
resolve: {
conditions: ['module', 'node'],
},
},
};
2 changes: 1 addition & 1 deletion examples/12_nossr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"autoprefixer": "10.4.20",
"tailwindcss": "3.4.17",
"typescript": "5.7.2",
"vite": "5.4.10"
"vite": "6.0.6"
}
}
2 changes: 1 addition & 1 deletion examples/37_css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@types/react-dom": "19.0.2",
"@vanilla-extract/vite-plugin": "4.0.18",
"typescript": "5.7.2",
"vite": "5.4.10",
"vite": "6.0.6",
"vite-plugin-stylex-dev": "0.7.5"
}
}
2 changes: 1 addition & 1 deletion examples/41_path-alias/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@types/react": "19.0.2",
"@types/react-dom": "19.0.2",
"typescript": "5.7.2",
"vite": "5.4.10",
"vite": "6.0.6",
"vite-tsconfig-paths": "5.1.4"
}
}
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,5 @@
"typescript": "^5.7.2",
"wait-port": "^1.1.0",
"waku": "workspace:*"
},
"pnpm": {
"overrides": {
"vite": "5.4.10"
}
}
}
4 changes: 2 additions & 2 deletions packages/waku/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@
"dotenv": "16.4.7",
"hono": "4.6.14",
"rsc-html-stream": "0.0.4",
"vite": "5.4.10"
"vite": "6.0.6"
},
"devDependencies": {
"@netlify/functions": "^3.0.0",
"@swc/cli": "^0.5.2",
"rollup": "^4.29.1",
"ts-expect": "^1.3.0",
"vitest": "^2.1.8"
"vitest": "^3.0.0-beta.3"
},
"peerDependencies": {
"react": "~19.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/waku/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ async function loadConfig(): Promise<Config> {
if (!existsSync(CONFIG_FILE)) {
return {};
}
// FIXME can we avoid using vite to load the config?
const { loadServerFile } = await import('./lib/utils/vite-loader.js');
const file = pathToFileURL(path.resolve(CONFIG_FILE)).toString();
return (await loadServerFile(file)).default;
Expand Down
4 changes: 1 addition & 3 deletions packages/waku/src/lib/plugins/vite-plugin-rsc-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export function rscEntriesPlugin(opts: {
moduleMap: Record<string, string>;
}): Plugin {
const codeToPrepend = `
try {
globalThis.AsyncLocalStorage = (await import('node:async_hooks')).AsyncLocalStorage;
} catch (e) {}
globalThis.AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage;
`;
let codeToAppend = `
export function loadModule(id) {
Expand Down
11 changes: 6 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 @@ -9,8 +9,9 @@ const patchRsdw = (code: string, type: 'SERVER' | 'CLIENT') => {
if (index === -1) {
throw new Error('rscRsdwPlugin: Unexpected code structure');
}
code =
code.slice(0, index) +

return code.replaceAll(
'function requireAsyncModule(id)',
`
globalThis.__WAKU_${type}_MODULE_LOADING__ ||= new Map();
globalThis.__WAKU_${type}_MODULE_CACHE__ ||= new Map();
Expand All @@ -26,9 +27,9 @@ globalThis.__WAKU_${type}_CHUNK_LOAD__ ||= (id) => {
return globalThis.__WAKU_${type}_MODULE_LOADING__.get(id);
};
globalThis.__WAKU_${type}_REQUIRE__ ||= (id) => globalThis.__WAKU_${type}_MODULE_CACHE__.get(id);
` +
code.slice(index);
return code;
function requireAsyncModule(id)
`,
);
};

export function rscRsdwPlugin(): Plugin {
Expand Down
4 changes: 3 additions & 1 deletion packages/waku/src/lib/utils/vite-loader.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { createServer as createViteServer } from 'vite';

import { fileURLToFilePath } from '../utils/path.js';

export const loadServerFile = async (fileURL: string) => {
const vite = await createViteServer({
ssr: {
external: ['waku'],
},
server: { middlewareMode: true, watch: null },
});
try {
return vite.ssrLoadModule(fileURLToFilePath(fileURL));
} finally {
await vite.close();
await vite.ws.close();
}
};
2 changes: 1 addition & 1 deletion packages/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"shiki": "^1.24.4",
"tailwindcss": "^3.4.17",
"typescript": "^5.7.2",
"vite": "5.4.10"
"vite": "6.0.6"
}
}
Loading

0 comments on commit ec87f81

Please sign in to comment.