Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): hmr problem with ssr when using rspack #5893

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/kind-forks-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/server': patch
---

fix(ssr): hmr problem with ssr when using rspack
fix(ssr): 使用 rspack 时,ssr 的 hmr 问题
3 changes: 1 addition & 2 deletions packages/server/server/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const devPlugin = <O extends ServerBaseOptions>(
const {
middlewares,
distDirectory,
routes,
nodeServer,
apiDirectory,
sharedDirectory,
Expand All @@ -49,7 +48,7 @@ export const devPlugin = <O extends ServerBaseOptions>(

rsbuild?.onDevCompileDone(({ stats }) => {
if (stats.toJson({ all: false }).name !== 'server') {
onRepack(distDirectory, runner, routes || []);
onRepack(distDirectory, runner);
}
});

Expand Down
31 changes: 6 additions & 25 deletions packages/server/server/src/helpers/repack.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import path from 'path';
import { ServerRoute } from '@modern-js/types';
import { LOADABLE_STATS_FILE } from '@modern-js/utils';
import { fileReader } from '@modern-js/runtime-utils/fileReader';
import { ServerHookRunner } from '@modern-js/server-core';

const getBundles = (routes: ServerRoute[]) => {
return routes.filter(route => route.isSSR).map(route => route.bundle);
};

const cleanSSRCache = (distDir: string, routes: ServerRoute[]) => {
const bundles = getBundles(routes);

bundles.forEach(bundle => {
const filepath = path.join(distDir, bundle as string);
if (require.cache[filepath]) {
delete require.cache[filepath];
const cleanSSRCache = (distDir: string) => {
Object.keys(require.cache).forEach(key => {
if (key.startsWith(distDir)) {
delete require.cache[key];
}
});

const loadable = path.join(distDir, LOADABLE_STATS_FILE);
if (require.cache[loadable]) {
delete require.cache[loadable];
}
};

export const onRepack = (
distDir: string,
runner: ServerHookRunner,
routes: ServerRoute[],
) => {
cleanSSRCache(distDir, routes);
export const onRepack = (distDir: string, runner: ServerHookRunner) => {
cleanSSRCache(distDir);
fileReader.reset();
runner.reset({
event: {
Expand Down
Loading