Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions e2e/cases/server/load-bundle-cjs/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test } from '@e2e/helper';

test('should load CJS bundle with runtime globals', async ({
Comment thread
chenjiahan marked this conversation as resolved.
devOnly,
request,
}) => {
const rsbuild = await devOnly();
const baseUrl = `http://localhost:${rsbuild.port}`;

const response1 = await request.get(`${baseUrl}/check`);
const response2 = await request.get(`${baseUrl}/check`);

expect(response1.status()).toBe(200);
expect(await response1.text()).toBe('index.js:function');
expect(await response2.text()).toBe('index.js:function');

expect(
rsbuild.logs.filter((log) => log.includes('load bundle cjs')).length,
).toBe(1);
});
51 changes: 51 additions & 0 deletions e2e/cases/server/load-bundle-cjs/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { defineConfig, type RequestHandler } from '@rsbuild/core';

export default defineConfig({
server: {
setup: ({ action, server }) => {
if (action !== 'dev') {
return;
}

const middleware: RequestHandler = async (req, res, next) => {
if (req.method !== 'GET' || req.url !== '/check') {
next();
return;
}

try {
const bundle = await server.environments.node.loadBundle<{
getPayload: () => Promise<string>;
}>('index');

res.setHeader('Content-Type', 'text/plain');
res.end(await bundle.getPayload());
} catch (error) {
res.statusCode = 500;
res.end(String(error));
}
};

server.middlewares.use(middleware);
},
},
environments: {
web: {
source: {
entry: {
index: './src/index.js',
},
},
},
node: {
output: {
target: 'node',
},
Comment thread
chenjiahan marked this conversation as resolved.
source: {
Comment thread
chenjiahan marked this conversation as resolved.
entry: {
index: './src/index.server.cjs',
},
},
},
},
});
1 change: 1 addition & 0 deletions e2e/cases/server/load-bundle-cjs/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
document.body.innerHTML = '<div id="root">load-bundle-cjs</div>';
8 changes: 8 additions & 0 deletions e2e/cases/server/load-bundle-cjs/src/index.server.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
console.log('load bundle cjs');

module.exports.getPayload = async function getPayload() {
const path = await import('node:path');

// Check that queueMicrotask is available inside the loaded CJS bundle.
return `${path.basename('/tmp/index.js')}:${typeof queueMicrotask}`;
};
Loading