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 packages/core/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { prepareCli } from './prepare';

export { initCli } from './init';

export async function runCLI(): Promise<void> {
export function runCLI(): void {
// make it easier to identify the process via activity monitor or other tools
process.title = 'rstest-node';
prepareCli();
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/core/cliShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export async function setupCliShortcuts({
} catch {}
};

const promptInput = async (
const promptInput = (
promptText: string,
onComplete: (value: string | undefined) => Promise<void>,
): Promise<void> => {
): void => {
if (isPrompting) return;
isPrompting = true;

Expand Down Expand Up @@ -145,9 +145,9 @@ export async function setupCliShortcuts({
{
key: 't',
description: `${color.bold('t')} ${color.dim('filter by a test name regex pattern')}`,
action: async () => {
action: () => {
clearCurrentInputLine();
await promptInput(
promptInput(
'Enter test name pattern (empty to clear): ',
async (pattern) => {
await runWithTestNamePattern(pattern);
Expand All @@ -158,9 +158,9 @@ export async function setupCliShortcuts({
{
key: 'p',
description: `${color.bold('p')} ${color.dim('filter by a filename regex pattern')}`,
action: async () => {
action: () => {
clearCurrentInputLine();
await promptInput(
promptInput(
'Enter file name pattern (empty to clear): ',
async (input) => {
const filters = input
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/core/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function applyEnvChanges(changes: Record<string, string | undefined>) {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

async function createSetupPool() {
function createSetupPool() {
const options: Options = {
runtime: 'child_process',
filename: resolve(__dirname, './globalSetupWorker.js'),
Expand Down Expand Up @@ -61,7 +61,7 @@ export async function runGlobalSetup({
success: boolean;
errors?: any[];
}> {
const pool = await createSetupPool();
const pool = createSetupPool();

const result = await pool.run({
type: 'setup',
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/core/listTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const collectNodeTests = async ({
if (nodeProjects.length === 0) {
return {
list: [],
getSourceMap: async (_name: string) => null,
close: async () => {},
getSourceMap: async () => null,
close: async () => undefined,
};
}

Expand Down Expand Up @@ -183,7 +183,7 @@ const collectBrowserTests = async ({
if (browserProjects.length === 0) {
return {
list: [],
close: async () => {},
close: async () => undefined,
};
}

Expand Down Expand Up @@ -216,10 +216,10 @@ const collectTestFiles = async ({
);
}
return {
close: async () => {},
close: async () => undefined,
errors: [],
list,
getSourceMap: async (_name: string) => null,
getSourceMap: async () => null,
};
};

Expand Down
272 changes: 133 additions & 139 deletions packages/core/src/core/plugins/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,160 +28,154 @@ export const pluginBasic: (context: RstestContext) => RsbuildPlugin = (
.oneOf(CHAIN_ID.ONE_OF.JS_MAIN)
.delete('type');
});
api.modifyEnvironmentConfig(
async (config, { mergeEnvironmentConfig, name }) => {
const {
normalizedConfig: {
resolve,
source,
output,
tools,
dev,
testEnvironment,
},
outputModule,
rootPath,
} = context.projects.find((p) => p.environmentName === name)!;
return mergeEnvironmentConfig(
config,
{
tools,
resolve,
source,
output,
dev,
api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig, name }) => {
const {
normalizedConfig: {
resolve,
source,
output,
tools,
dev,
testEnvironment,
},
outputModule,
rootPath,
} = context.projects.find((p) => p.environmentName === name)!;
return mergeEnvironmentConfig(
config,
{
tools,
resolve,
source,
output,
dev,
},
{
source: {
define: {
'import.meta.rstest': "global['@rstest/core']",
'import.meta.env': 'process.env',
},
},
{
source: {
define: {
'import.meta.rstest': "global['@rstest/core']",
'import.meta.env': 'process.env',
},
output: {
// Pass resources to the worker on demand according to entry
manifest: `${name}-manifest.json`,
sourceMap: {
js: 'source-map',
},
output: {
// Pass resources to the worker on demand according to entry
manifest: `${name}-manifest.json`,
sourceMap: {
js: 'source-map',
},
module: outputModule,
filename: outputModule
? {
js: '[name].mjs',
}
: undefined,
distPath: {
root:
context.projects.length > 1
? `${TEMP_RSTEST_OUTPUT_DIR}/${name}`
: TEMP_RSTEST_OUTPUT_DIR,
},
module: outputModule,
filename: outputModule
? {
js: '[name].mjs',
}
: undefined,
distPath: {
root:
context.projects.length > 1
? `${TEMP_RSTEST_OUTPUT_DIR}/${name}`
: TEMP_RSTEST_OUTPUT_DIR,
},
tools: {
rspack: (config, { isProd, rspack }) => {
// keep windows path as native path
config.context = path.resolve(rootPath);
// treat `test` as development mode
config.mode = isProd ? 'production' : 'development';
config.output ??= {};
config.output.iife = false;
// polyfill interop
config.output.importFunctionName = outputModule
? 'import.meta.__rstest_dynamic_import__'
: '__rstest_dynamic_import__';
config.output.devtoolModuleFilenameTemplate =
'[absolute-resource-path]';
},
tools: {
rspack: (config, { isProd, rspack }) => {
// keep windows path as native path
config.context = path.resolve(rootPath);
// treat `test` as development mode
config.mode = isProd ? 'production' : 'development';
config.output ??= {};
config.output.iife = false;
// polyfill interop
config.output.importFunctionName = outputModule
? 'import.meta.__rstest_dynamic_import__'
: '__rstest_dynamic_import__';
config.output.devtoolModuleFilenameTemplate =
'[absolute-resource-path]';

if (!config.devtool || !config.devtool.includes('inline')) {
config.devtool = 'nosources-source-map';
}
if (!config.devtool || !config.devtool.includes('inline')) {
config.devtool = 'nosources-source-map';
}

config.plugins.push(
new rspack.experiments.RstestPlugin({
injectModulePathName: true,
importMetaPathName: true,
hoistMockModule: true,
manualMockRoot: pathe.resolve(rootPath, '__mocks__'),
}),
);

config.module.rules ??= [];
config.module.rules.push({
test: /\.mts$/,
// Treated mts as strict ES modules.
type: 'javascript/esm',
});

if (outputModule) {
config.plugins.push(
new rspack.experiments.RstestPlugin({
injectModulePathName: true,
importMetaPathName: true,
hoistMockModule: true,
manualMockRoot: pathe.resolve(rootPath, '__mocks__'),
new rspack.BannerPlugin({
banner: requireShim,
// Just before minify stage, to perform tree shaking.
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE - 1,
raw: true,
include: /\.(js|mjs)$/,
}),
);
}

config.module.rules ??= [];
config.module.rules.push({
test: /\.mts$/,
// Treated mts as strict ES modules.
type: 'javascript/esm',
});

if (outputModule) {
config.plugins.push(
new rspack.BannerPlugin({
banner: requireShim,
// Just before minify stage, to perform tree shaking.
stage:
rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE - 1,
raw: true,
include: /\.(js|mjs)$/,
}),
);
}

config.module.parser ??= {};
config.module.parser.javascript = {
// Keep dynamic import expressions.
// eg. (modulePath) => import(modulePath)
importDynamic: false,
// Keep dynamic require expressions.
// eg. (modulePath) => require(modulePath)
requireDynamic: false,
requireAsExpression: false,
// Keep require.resolve expressions.
requireResolve: false,
...(config.module.parser.javascript || {}),
// suppress ESModulesLinkingError for exports that might be implemented in mock
exportsPresence: 'warn',
};
config.module.parser ??= {};
config.module.parser.javascript = {
// Keep dynamic import expressions.
// eg. (modulePath) => import(modulePath)
importDynamic: false,
// Keep dynamic require expressions.
// eg. (modulePath) => require(modulePath)
requireDynamic: false,
requireAsExpression: false,
// Keep require.resolve expressions.
requireResolve: false,
...(config.module.parser.javascript || {}),
// suppress ESModulesLinkingError for exports that might be implemented in mock
exportsPresence: 'warn',
};

config.resolve ??= {};
config.resolve.extensions ??= [];
config.resolve.extensions.push('.cjs');
config.resolve ??= {};
config.resolve.extensions ??= [];
config.resolve.extensions.push('.cjs');

// TypeScript allows importing TS files with `.js` extension
config.resolve.extensionAlias ??= {};
config.resolve.extensionAlias['.js'] = ['.js', '.ts', '.tsx'];
config.resolve.extensionAlias['.jsx'] = ['.jsx', '.tsx'];
// TypeScript allows importing TS files with `.js` extension
config.resolve.extensionAlias ??= {};
config.resolve.extensionAlias['.js'] = ['.js', '.ts', '.tsx'];
config.resolve.extensionAlias['.jsx'] = ['.jsx', '.tsx'];

if (testEnvironment.name === 'node') {
// skip `module` field in Node.js environment.
// ESM module resolved by module field is not always a native ESM module
config.resolve.mainFields = config.resolve.mainFields?.filter(
(filed) => filed !== 'module',
) || ['main'];
}
if (testEnvironment.name === 'node') {
// skip `module` field in Node.js environment.
// ESM module resolved by module field is not always a native ESM module
config.resolve.mainFields = config.resolve.mainFields?.filter(
(filed) => filed !== 'module',
) || ['main'];
}

config.resolve.byDependency ??= {};
config.resolve.byDependency.commonjs ??= {};
// skip `module` field when commonjs require
// By default, rspack resolves the "module" field for commonjs first, but this is not always returned synchronously in esm
config.resolve.byDependency.commonjs.mainFields = [
'main',
'...',
];
config.resolve.byDependency ??= {};
config.resolve.byDependency.commonjs ??= {};
// skip `module` field when commonjs require
// By default, rspack resolves the "module" field for commonjs first, but this is not always returned synchronously in esm
config.resolve.byDependency.commonjs.mainFields = ['main', '...'];

config.optimization = {
moduleIds: 'named',
chunkIds: 'named',
nodeEnv: false,
...(config.optimization || {}),
// make sure setup file and test file share the runtime
runtimeChunk: {
name: `${name}-${RUNTIME_CHUNK_NAME}`,
},
};
},
config.optimization = {
moduleIds: 'named',
chunkIds: 'named',
nodeEnv: false,
...(config.optimization || {}),
// make sure setup file and test file share the runtime
runtimeChunk: {
name: `${name}-${RUNTIME_CHUNK_NAME}`,
},
};
},
},
);
},
);
},
);
});
},
});
2 changes: 1 addition & 1 deletion packages/core/src/core/plugins/css-filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const pluginCSSFilter = (): RsbuildPlugin => ({
setup(api) {
api.modifyBundlerChain({
order: 'post',
handler: async (chain, { target, CHAIN_ID, environment }) => {
handler: (chain, { target, CHAIN_ID, environment }) => {
const emitCss = environment.config.output.emitCss ?? target === 'web';
if (!emitCss) {
const ruleIds = [
Expand Down
Loading
Loading