Skip to content

Commit c4999fc

Browse files
committed
test: add transformImport tests
1 parent a7bd23d commit c4999fc

File tree

12 files changed

+397
-7
lines changed

12 files changed

+397
-7
lines changed

e2e/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Rslib will try to cover the common scenarios in the [integration test cases of M
1414
| asset | ⚪️ | |
1515
| autoExtension | 🟢 | |
1616
| autoExternal | 🟢 | |
17-
| banner-footer | ⚪️ | |
17+
| banner-footer | 🟢 | |
1818
| buildType | 🟢 | |
1919
| copy | 🟢 | |
2020
| decorator | ⚪️ | |
@@ -38,8 +38,8 @@ Rslib will try to cover the common scenarios in the [integration test cases of M
3838
| splitting | ⚪️ | |
3939
| style | ⚪️ | |
4040
| target | 🟢 | |
41-
| transformImport | ⚪️ | |
42-
| transformLodash | ⚪️ | |
41+
| transformImport | 🟢 | |
42+
| transformLodash | 🟢 | |
4343
| tsconfig | ⚪️ | |
4444
| tsconfigExtends | ⚪️ | |
4545
| umdGlobals | ⚪️ | |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "transform-import-arco-design-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"dependencies": {
7+
"@arco-design/web-react": "^2.64.0"
8+
}
9+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
output: {
8+
distPath: {
9+
root: './dist/esm/bundle',
10+
},
11+
},
12+
}),
13+
generateBundleEsmConfig({
14+
output: {
15+
distPath: {
16+
root: './dist/esm/bundleless',
17+
},
18+
},
19+
bundle: false,
20+
}),
21+
generateBundleCjsConfig({
22+
output: {
23+
distPath: {
24+
root: './dist/cjs/bundle',
25+
},
26+
},
27+
}),
28+
generateBundleCjsConfig({
29+
output: {
30+
distPath: {
31+
root: './dist/cjs/bundleless',
32+
},
33+
},
34+
bundle: false,
35+
}),
36+
],
37+
source: {
38+
entry: {
39+
index: './src/index.ts',
40+
},
41+
transformImport: [
42+
{
43+
libraryName: '@arco-design/web-react',
44+
style: true,
45+
libraryDirectory: 'es',
46+
},
47+
],
48+
},
49+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Button } from '@arco-design/web-react';
2+
3+
console.log(Button);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./"
5+
},
6+
"include": ["src"]
7+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { join } from 'node:path';
2+
import { type FormatType, buildAndGetResults } from '@e2e/helper';
3+
import { expect, test } from 'vitest';
4+
5+
test('transformImport with arco-design', async () => {
6+
const fixturePath = join(__dirname, 'arco-design');
7+
const { contents } = await buildAndGetResults(fixturePath);
8+
const formats: FormatType[] = ['esm0', 'esm1', 'cjs0', 'cjs1'];
9+
10+
for (const format of formats) {
11+
expect(Object.values(contents[format]!)[0]).toContain(
12+
format.startsWith('esm')
13+
? 'import * as __WEBPACK_EXTERNAL_MODULE__arco_design_web_react_es_button__ from "@arco-design/web-react/es/button"'
14+
: 'var button_namespaceObject = require("@arco-design/web-react/es/button")',
15+
);
16+
expect(Object.values(contents[format]!)[0]).toContain(
17+
format.startsWith('esm')
18+
? 'import "@arco-design/web-react/es/button/style"'
19+
: 'require("@arco-design/web-react/es/button/style")',
20+
);
21+
}
22+
});
23+
24+
test('transformImport with lodash', async () => {
25+
const fixturePath = join(__dirname, 'lodash');
26+
const { contents } = await buildAndGetResults(fixturePath);
27+
const formats: FormatType[] = ['esm0', 'esm1', 'cjs0', 'cjs1'];
28+
29+
for (const format of formats) {
30+
expect(Object.values(contents[format]!)[0]).toContain(
31+
format.startsWith('esm')
32+
? 'import * as __WEBPACK_EXTERNAL_MODULE_lodash_get__ from "lodash/get"'
33+
: 'var get_namespaceObject = require("lodash/get")',
34+
);
35+
expect(Object.values(contents[format]!)[0]).toContain(
36+
format.startsWith('esm')
37+
? 'import * as __WEBPACK_EXTERNAL_MODULE_lodash_fp_add__ from "lodash/fp/add"'
38+
: 'var add_namespaceObject = require("lodash/fp/add")',
39+
);
40+
}
41+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "transform-import-lodash-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"dependencies": {
7+
"lodash": "^4.17.21"
8+
},
9+
"devDependencies": {
10+
"@types/lodash": "^4.17.7"
11+
}
12+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
output: {
8+
distPath: {
9+
root: './dist/esm/bundle',
10+
},
11+
},
12+
}),
13+
generateBundleEsmConfig({
14+
output: {
15+
distPath: {
16+
root: './dist/esm/bundleless',
17+
},
18+
},
19+
bundle: false,
20+
}),
21+
generateBundleCjsConfig({
22+
output: {
23+
distPath: {
24+
root: './dist/cjs/bundle',
25+
},
26+
},
27+
}),
28+
generateBundleCjsConfig({
29+
output: {
30+
distPath: {
31+
root: './dist/cjs/bundleless',
32+
},
33+
},
34+
bundle: false,
35+
}),
36+
],
37+
source: {
38+
entry: {
39+
index: './src/index.ts',
40+
},
41+
transformImport: [
42+
{
43+
libraryName: 'lodash',
44+
customName: 'lodash/{{ member }}',
45+
},
46+
// Rsbuild customName only support string type for performance reasons
47+
{
48+
libraryName: 'lodash/fp',
49+
customName: 'lodash/fp/{{ member }}',
50+
},
51+
],
52+
},
53+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { get } from 'lodash';
2+
import { add } from 'lodash/fp';
3+
4+
var object = { a: [{ b: { c: 'foo' } }] };
5+
6+
export const addOne = add(1);
7+
export const foo = get(object, 'a[0].b.c');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./"
5+
},
6+
"include": ["src"]
7+
}

e2e/scripts/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function generateBundleCjsConfig(config: LibConfig = {}): LibConfig {
3434
return mergeConfig(cjsBasicConfig, config)!;
3535
}
3636

37-
type FormatType = Format | `${Format}${number}`;
37+
export type FormatType = Format | `${Format}${number}`;
3838
type FilePath = string;
3939

4040
type BuildResult = {

0 commit comments

Comments
 (0)