Skip to content

Commit

Permalink
recover isClientEntry check
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Feb 9, 2024
1 parent 370e090 commit 6e663cd
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions packages/waku/src/lib/plugins/vite-plugin-rsc-delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ import * as swc from '@swc/core';

import type { HotUpdatePayload } from './vite-plugin-rsc-hmr.js';

const isClientEntry = (id: string, code: string) => {
const ext = path.extname(id);
if (['.ts', '.tsx', '.js', '.jsx'].includes(ext)) {
const mod = swc.parseSync(code, {
syntax: ext === '.ts' || ext === '.tsx' ? 'typescript' : 'ecmascript',
tsx: ext === '.tsx',
});
for (const item of mod.body) {
if (
item.type === 'ExpressionStatement' &&
item.expression.type === 'StringLiteral' &&
item.expression.value === 'use client'
) {
return true;
}
}
}
return false;
};

// import { CSS_LANGS_RE } from "vite/dist/node/constants.js";
const CSS_LANGS_RE =
/\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/;
Expand All @@ -26,7 +46,6 @@ export function rscDelegatePlugin(
},
async handleHotUpdate(ctx) {
if (mode === 'development') {
console.log('---', moduleImports, ctx)
if (moduleImports.has(ctx.file)) {
// re-inject
const transformedResult = await server.transformRequest(ctx.file);
Expand All @@ -38,7 +57,10 @@ export function rscDelegatePlugin(
data: { ...transformedResult, source, id: ctx.file },
});
}
} else if (ctx.modules.length) {
} else if (
ctx.modules.length &&
!isClientEntry(ctx.file, await ctx.read())
) {
callback({ type: 'custom', event: 'rsc-reload' });
}
}
Expand Down

0 comments on commit 6e663cd

Please sign in to comment.