Skip to content
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
3 changes: 0 additions & 3 deletions rsbuild/ssr-express-with-manifest/rsbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { pluginReact } from "@rsbuild/plugin-react";

export default defineConfig({
plugins: [pluginReact()],
dev: {
writeToDisk: true
},
environments: {
web: {
output: {
Expand Down
21 changes: 17 additions & 4 deletions rsbuild/ssr-express-with-manifest/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import { createRsbuild, loadConfig, logger } from "@rsbuild/core";

const templateHtml = fs.readFileSync('./template.html', 'utf-8');

let manifest;

const serverRender = (serverAPI) => async (_req, res) => {
const indexModule = await serverAPI.environments.ssr.loadBundle("index");

const markup = indexModule.render();

const { entries } = JSON.parse(fs.readFileSync('./dist/manifest.json', 'utf-8'));
const { entries } = JSON.parse(manifest);

const { js, css } = entries['index'].initial;
const { js = [], css = []} = entries['index'].initial;

const scriptTags = js.map(file => `<script src="${file}" defer></script>`).join('\n');
const styleTags = css.map(file => `<link rel="stylesheet" href="${file}">`).join('\n');
const scriptTags = js
.map((url) => `<script src="${url}" defer></script>`)
.join('\n');
const styleTags = css
.map((file) => `<link rel="stylesheet" href="${file}">`)
.join('\n');

const html = templateHtml.replace("<!--app-content-->", markup).replace('<!--app-head-->', `${scriptTags}\n${styleTags}`);

Expand All @@ -32,6 +38,11 @@ export async function startDevServer() {
rsbuildConfig: content,
});

rsbuild.onDevCompileDone(async () => {
// update manifest info when rebuild
manifest = await fs.promises.readFile('./dist/manifest.json', 'utf-8');
})

const app = express();

// Create Rsbuild DevServer instance
Expand All @@ -54,6 +65,8 @@ export async function startDevServer() {
const httpServer = app.listen(rsbuildServer.port, () => {
// Notify Rsbuild that the custom server has started
rsbuildServer.afterListen();

console.log(`Server started at http://localhost:${rsbuildServer.port}`);
});

rsbuildServer.connectWebSocket({ server: httpServer });
Expand Down