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
2 changes: 1 addition & 1 deletion e2e/cases/server/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"version": "1.0.0",
"scripts": {
"dev": "npx rsbuild dev",
"dev:esm": "TEST_ESM_LIBRARY=true npx rsbuild dev"
"dev:esm": "cross-env NODE_OPTIONS=\"--experimental-vm-modules\" TEST_ESM_LIBRARY=true npx rsbuild dev"
}
}
9 changes: 4 additions & 5 deletions e2e/cases/server/ssr/src/assert.server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
function assertQueueMicroTask() {
export function assert() {
if (typeof queueMicrotask !== 'function') {
throw Error('not support queueMicrotask in this environment');
}
}

export function assert() {
assertQueueMicroTask();
if (typeof TextEncoder !== 'function') {
throw Error('not support TextEncoder in this environment');
}
}
30 changes: 14 additions & 16 deletions packages/core/src/server/runner/asModule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import vm from 'node:vm';

const SYNTHETIC_MODULES_STORE = '__SYNTHETIC_MODULES_STORE';

export const asModule = async (
something: Record<string, any>,
context: Record<string, any>,
Expand All @@ -10,20 +8,20 @@ export const asModule = async (
if (something instanceof vm.Module) {
return something;
}
context[SYNTHETIC_MODULES_STORE] = context[SYNTHETIC_MODULES_STORE] || [];
const i = context[SYNTHETIC_MODULES_STORE].length;
context[SYNTHETIC_MODULES_STORE].push(something);
const code = [...new Set(['default', ...Object.keys(something)])]
.map(
(name) =>
`const _${name} = ${SYNTHETIC_MODULES_STORE}[${i}]${
name === 'default' ? '' : `[${JSON.stringify(name)}]`
}; export { _${name} as ${name}};`,
)
.join('\n');
const m = new vm.SourceTextModule(code, {
context,
});

const exports = [...new Set(['default', ...Object.keys(something)])];

const m = new vm.SyntheticModule(
exports,
() => {
for (const name of exports) {
m.setExport(name, name === 'default' ? something : something[name]);
}
},
{
context,
},
);
if (unlinked) return m;
await m.link((() => {}) as () => vm.Module);
// @ts-expect-error copy from webpack
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/server/runner/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ export class EsmRunner extends CommonJsRunner {
}

protected createEsmRequirer(): RunnerRequirer {
const esmContext = vm.createContext(this.baseModuleScope!, {
name: 'context for esm',
});
const esmCache = new Map<string, SourceTextModule>();
const esmIdentifier = this._options.name;
return (currentDirectory, modulePath, context = {}) => {
Expand All @@ -58,7 +55,7 @@ export class EsmRunner extends CommonJsRunner {
identifier: `${esmIdentifier}-${file.path}`,
// no attribute
url: `${pathToFileURL(file.path).href}?${esmIdentifier}`,
context: esmContext,
// run in current execution context
initializeImportMeta: (meta: { url: string }, _: any) => {
meta.url = pathToFileURL(file!.path).href;
},
Expand Down
Loading