Skip to content

Commit 4775c75

Browse files
authored
feat(core): support source.transformImport (#1124)
1 parent a3591b2 commit 4775c75

21 files changed

Lines changed: 238 additions & 69 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const foo = () => 'actual';
2+
export default foo;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { foo } from './foo.js';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@rstest/tests-transform-import",
3+
"version": "1.0.0",
4+
"private": true
5+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineConfig } from '@rstest/core';
2+
3+
export default defineConfig({
4+
include: ['tests/**/*.test.ts'],
5+
resolve: {
6+
alias: {
7+
'demo-lib': './demo-lib',
8+
},
9+
},
10+
source: {
11+
transformImport: [
12+
{
13+
libraryName: 'demo-lib',
14+
libraryDirectory: '.',
15+
camelToDashComponentName: false,
16+
},
17+
],
18+
},
19+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module 'demo-lib' {
2+
export function foo(): string;
3+
export default foo;
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, it } from '@rstest/core';
2+
import { foo } from 'demo-lib';
3+
4+
it('transformImport from rstest config', () => {
5+
expect(foo()).toBe('actual');
6+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { dirname, join } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { describe, expect, it } from '@rstest/core';
4+
import { runRstestCli } from '../scripts';
5+
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
8+
9+
describe('adapter transformImport', () => {
10+
it('should support source.transformImport in rstest config', async () => {
11+
const { cli, expectExecSuccess } = await runRstestCli({
12+
command: 'rstest',
13+
args: ['run'],
14+
options: {
15+
nodeOptions: {
16+
cwd: join(__dirname, 'fixtures'),
17+
},
18+
},
19+
});
20+
21+
await expectExecSuccess();
22+
23+
expect(cli.stdout).toContain('transformImport from rstest config');
24+
expect(cli.stdout).toContain('Test Files 1 passed');
25+
});
26+
});

packages/adapter-rsbuild/src/toRstestConfig.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ export function toRstestConfig({
3232

3333
const { rspack, swc, bundlerChain } = finalBuildConfig.tools || {};
3434
const { cssModules, target, module } = finalBuildConfig.output || {};
35-
const { assetsInclude, decorators, define, include, exclude, tsconfigPath } =
36-
finalBuildConfig.source || {};
35+
const {
36+
assetsInclude,
37+
decorators,
38+
define,
39+
include,
40+
exclude,
41+
tsconfigPath,
42+
transformImport,
43+
} = finalBuildConfig.source || {};
3744

3845
return {
3946
root: finalBuildConfig.root,
@@ -54,6 +61,7 @@ export function toRstestConfig({
5461
include,
5562
exclude,
5663
tsconfigPath,
64+
transformImport,
5765
},
5866
resolve: finalBuildConfig.resolve,
5967
output: {

packages/adapter-rsbuild/tests/toRstestConfig.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ describe('toRstestConfig', () => {
99
define: {
1010
'process.env.NODE_ENV': '"common"',
1111
},
12+
transformImport: [
13+
{
14+
libraryName: 'lodash',
15+
libraryDirectory: '',
16+
camelToDashComponentName: false,
17+
},
18+
],
1219
},
1320
resolve: {
1421
alias: {
@@ -38,6 +45,13 @@ describe('toRstestConfig', () => {
3845
expect(config.source?.define).toEqual({
3946
'process.env.NODE_ENV': '"common"',
4047
});
48+
expect(config.source?.transformImport).toEqual([
49+
{
50+
libraryName: 'lodash',
51+
libraryDirectory: '',
52+
camelToDashComponentName: false,
53+
},
54+
]);
4155
expect(config.resolve?.alias).toEqual({
4256
'@': './src',
4357
});

packages/adapter-rslib/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export function withRslibConfig(
9494
include,
9595
exclude,
9696
tsconfigPath,
97+
transformImport,
9798
} = finalLibConfig.source || {};
9899

99100
// Convert rslib config to rstest config
@@ -112,6 +113,7 @@ export function withRslibConfig(
112113
include,
113114
exclude,
114115
tsconfigPath,
116+
transformImport,
115117
},
116118
resolve: finalLibConfig.resolve,
117119
output: {

0 commit comments

Comments
 (0)