Skip to content
Draft
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
25 changes: 19 additions & 6 deletions packages/core/src/server/runner/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/**
* The following code is modified based on @rspack/test-tools/runner
*/
import { color } from '../../helpers';
import { color, require } from '../../helpers';
import { CommonJsRunner } from './cjs';
import { EsmRunner } from './esm';
import { SystemJsRunner } from './systemJs';
import type { Runner, RunnerFactory, RunnerFactoryOptions } from './type';

class BasicRunnerFactory implements RunnerFactory {
constructor(protected name: string) {}

create(options: RunnerFactoryOptions): Runner {
const runner = this.createRunner(options);
async create(options: RunnerFactoryOptions): Promise<Runner> {
const runner = await this.createRunner(options);
return runner;
}

protected createRunner(options: RunnerFactoryOptions): Runner {
protected async createRunner(options: RunnerFactoryOptions): Promise<Runner> {
const runnerOptions = {
name: this.name,
...options,
Expand All @@ -29,7 +31,18 @@ class BasicRunnerFactory implements RunnerFactory {
)} resource in Rsbuild server`,
);
}
return new EsmRunner(runnerOptions);

if (!compilerOptions.output.module) {
return new CommonJsRunner(runnerOptions);
}

// rslint-disable-next-line @typescript-eslint/no-require-imports
const vm = require('node:vm') as typeof import('node:vm');
if (vm.SourceTextModule) {
return new EsmRunner(runnerOptions);
}

return new SystemJsRunner(runnerOptions);
}
}

Expand All @@ -40,7 +53,7 @@ export const run = async <T>({
bundlePath: string;
}): Promise<T> => {
const runnerFactory = new BasicRunnerFactory(bundlePath);
const runner = runnerFactory.create(runnerFactoryOptions);
const runner = await runnerFactory.create(runnerFactoryOptions);
const mod = await runner.run(bundlePath);

return mod as T;
Expand Down
Loading