Skip to content

Commit a645e60

Browse files
authored
test: add alias e2e test (#22)
1 parent 1a8f78e commit a645e60

20 files changed

+196
-52
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ compiled/
1010
coverage/
1111
doc_build/
1212
playwright-report/
13+
tsconfig.tsbuildinfo
1314

1415
.vscode/**/*
1516
!.vscode/settings.json

e2e/cases/alias/index.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { join } from 'node:path';
2+
import { build } from '@rslib/core';
3+
import { expect, test } from 'vitest';
4+
import { getEntryJsResults } from '#shared';
5+
import { loadConfig } from '../../../packages/core/src/config';
6+
7+
test('alias in js', async () => {
8+
delete process.env.NODE_ENV;
9+
10+
const fixturePath = join(__dirname, 'js');
11+
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
12+
await build(rslibConfig);
13+
const results = await getEntryJsResults(rslibConfig);
14+
15+
expect(results.esm).toContain('hello world');
16+
expect(results.cjs).toContain('hello world');
17+
});
18+
19+
test('alias in ts', async () => {
20+
delete process.env.NODE_ENV;
21+
22+
const fixturePath = join(__dirname, 'ts');
23+
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
24+
await build(rslibConfig);
25+
const results = await getEntryJsResults(rslibConfig);
26+
27+
expect(results.esm).toContain('hello world');
28+
expect(results.cjs).toContain('hello world');
29+
});

e2e/cases/alias/js/rslib.config.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { join } from 'node:path';
2+
import { defineConfig } from '@rslib/core';
3+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';
4+
5+
export default defineConfig({
6+
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
7+
source: {
8+
entry: {
9+
main: join(__dirname, 'src/index.js'),
10+
},
11+
alias: {
12+
'@src': join(__dirname, 'src'),
13+
},
14+
},
15+
});

e2e/cases/alias/js/src/a.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 'hello world';

e2e/cases/alias/js/src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { a } from '@src/a';
2+
3+
console.info(a);

e2e/cases/alias/ts/rslib.config.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { join } from 'node:path';
2+
import { defineConfig } from '@rslib/core';
3+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';
4+
5+
export default defineConfig({
6+
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
7+
source: {
8+
entry: {
9+
main: join(__dirname, 'src/index.ts'),
10+
},
11+
alias: {
12+
'@src': join(__dirname, 'src'),
13+
},
14+
},
15+
});

e2e/cases/alias/ts/src/a.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const a = 'hello world';
2+
export type A = string;

e2e/cases/alias/ts/src/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { a } from '@src/a';
2+
3+
console.info(a);
4+
5+
export type { A } from './a';

e2e/cases/alias/ts/tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"compilerOptions": {
4+
"baseUrl": "./",
5+
"paths": {
6+
"@src/*": ["./src/*"]
7+
}
8+
},
9+
"include": ["src"]
10+
}

e2e/cases/define/index.test.ts

+24-38
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,33 @@
11
import { join } from 'node:path';
2-
import { type RslibConfig, build } from '@rslib/core';
2+
import { build } from '@rslib/core';
33
import { expect, test } from 'vitest';
4-
import { globContentJSON } from '#helper';
4+
import { getEntryJsResults } from '#shared';
5+
import { loadConfig } from '../../../packages/core/src/config';
56

6-
test('define', async () => {
7+
test('define in js', async () => {
78
delete process.env.NODE_ENV;
89

9-
const rslibConfig: RslibConfig = {
10-
lib: [
11-
{
12-
format: 'esm',
13-
output: {
14-
distPath: {
15-
root: join(__dirname, './dist/esm'),
16-
},
17-
},
18-
},
19-
{
20-
format: 'cjs',
21-
output: {
22-
distPath: {
23-
root: join(__dirname, './dist/cjs'),
24-
},
25-
},
26-
},
27-
],
28-
source: {
29-
entry: {
30-
main: join(__dirname, './js/src/index.js'),
31-
},
32-
define: {
33-
VERSION: JSON.stringify('1.0.0'),
34-
},
35-
},
36-
};
10+
const fixturePath = join(__dirname, 'js');
11+
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
12+
await build(rslibConfig);
13+
const results = await getEntryJsResults(rslibConfig);
3714

38-
const instance = await build(rslibConfig);
15+
expect(results.esm).not.toContain('console.info(VERSION)');
16+
expect(results.esm).toContain('1.0.0');
17+
expect(results.cjs).not.toContain('console.info(VERSION)');
18+
expect(results.cjs).toContain('1.0.0');
19+
});
20+
21+
test('define in ts', async () => {
22+
delete process.env.NODE_ENV;
3923

40-
const results = await globContentJSON(instance[0]!.context.distPath, {
41-
absolute: true,
42-
ignore: ['/**/*.map'],
43-
});
24+
const fixturePath = join(__dirname, 'ts');
25+
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
26+
await build(rslibConfig);
27+
const results = await getEntryJsResults(rslibConfig);
4428

45-
const entryJs = Object.keys(results).find((file) => file.endsWith('.js'));
46-
expect(results[entryJs!]).not.toContain('console.info(VERSION)');
29+
expect(results.esm).not.toContain('console.info(VERSION)');
30+
expect(results.esm).toContain('1.0.0');
31+
expect(results.cjs).not.toContain('console.info(VERSION)');
32+
expect(results.cjs).toContain('1.0.0');
4733
});

e2e/cases/define/js/rslib.config.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { join } from 'node:path';
2+
import { defineConfig } from '@rslib/core';
3+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';
4+
5+
export default defineConfig({
6+
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
7+
source: {
8+
entry: {
9+
main: join(__dirname, 'src/index.js'),
10+
},
11+
define: {
12+
VERSION: JSON.stringify('1.0.0'),
13+
},
14+
},
15+
});

e2e/cases/define/ts/rslib.config.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { join } from 'node:path';
2+
import { defineConfig } from '@rslib/core';
3+
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';
4+
5+
export default defineConfig({
6+
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
7+
source: {
8+
entry: {
9+
main: join(__dirname, 'src/index.ts'),
10+
},
11+
define: {
12+
VERSION: JSON.stringify('1.0.0'),
13+
},
14+
},
15+
});

e2e/cases/define/ts/src/env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare const VERSION: number;

e2e/cases/define/ts/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.info(VERSION);

e2e/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"private": true,
44
"type": "module",
55
"imports": {
6-
"#helper": "./scripts/helper.ts"
6+
"#helper": "./scripts/helper.ts",
7+
"#shared": "./scripts/shared.ts"
78
},
89
"scripts": {
910
"test": "playwright test"

e2e/scripts/shared.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { join } from 'node:path';
2+
import type { LibConfig, RslibConfig } from '@rslib/core';
3+
import { globContentJSON } from '#helper';
4+
5+
export function generateBundleEsmConfig(cwd: string): LibConfig {
6+
return {
7+
format: 'esm',
8+
output: {
9+
distPath: {
10+
root: join(cwd, './dist/esm'),
11+
},
12+
},
13+
};
14+
}
15+
16+
export function generateBundleCjsConfig(cwd: string): LibConfig {
17+
return {
18+
format: 'cjs',
19+
output: {
20+
distPath: {
21+
root: join(cwd, './dist/cjs'),
22+
},
23+
},
24+
};
25+
}
26+
27+
export async function getEntryJsResults(rslibConfig: RslibConfig) {
28+
const results: Record<string, string> = {};
29+
30+
for (const libConfig of rslibConfig.lib) {
31+
const result = await globContentJSON(libConfig?.output?.distPath?.root!, {
32+
absolute: true,
33+
ignore: ['/**/*.map'],
34+
});
35+
36+
const entryJs = Object.keys(result).find((file) => file.endsWith('.js'));
37+
38+
if (entryJs) {
39+
results[libConfig.format!] = result[entryJs]!;
40+
}
41+
}
42+
43+
return results;
44+
}

e2e/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"noEmit": true,
55
"composite": true
66
},
7-
"include": ["cases", "playwright.config.ts", "cases/**/*.test.ts", "scripts"],
7+
"include": ["cases/**/*.ts", "playwright.config.ts", "scripts"],
88
"exclude": ["**/node_modules", "**/.*/"],
99
"references": [
1010
{

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"prebundle": "nx run-many -t prebundle",
1111
"prepare": "pnpm run build && simple-git-hooks",
1212
"sort-package-json": "npx sort-package-json \"packages/*/package.json\"",
13-
"test:artifact": "vitest run --project artifact",
13+
"test:artifact": "vitest run --project artifact",
1414
"test:e2e": "cd e2e && pnpm run test",
15-
"test:unit": "vitest run --project unit",
15+
"test:unit": "vitest run --project unit",
1616
"watch": "pnpm build --watch"
1717
},
1818
"simple-git-hooks": {

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"prebundle": "prebundle"
3939
},
4040
"dependencies": {
41-
"@rsbuild/core": "0.7.9"
41+
"@rsbuild/core": "0.7.10"
4242
},
4343
"devDependencies": {
4444
"@rslib/tsconfig": "workspace:*",

pnpm-lock.yaml

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)