Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add alias e2e test #22

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ compiled/
coverage/
doc_build/
playwright-report/
tsconfig.tsbuildinfo

.vscode/**/*
!.vscode/settings.json
Expand Down
29 changes: 29 additions & 0 deletions e2e/cases/alias/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { join } from 'node:path';
import { build } from '@rslib/core';
import { expect, test } from 'vitest';
import { getEntryJsResults } from '#shared';
import { loadConfig } from '../../../packages/core/src/config';

test('alias in js', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname, 'js');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
const instance = await build(rslibConfig);
const results = await getEntryJsResults(instance);

expect(results.esm).toContain('hello world');
expect(results.cjs).toContain('hello world');
});

test('alias in ts', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname, 'ts');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
const instance = await build(rslibConfig);
const results = await getEntryJsResults(instance);

expect(results.esm).toContain('hello world');
expect(results.cjs).toContain('hello world');
});
15 changes: 15 additions & 0 deletions e2e/cases/alias/js/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: join(__dirname, 'src/index.js'),
},
alias: {
'@src': join(__dirname, 'src'),
},
},
});
1 change: 1 addition & 0 deletions e2e/cases/alias/js/src/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 'hello world';
3 changes: 3 additions & 0 deletions e2e/cases/alias/js/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { a } from '@src/a';

console.info(a);
15 changes: 15 additions & 0 deletions e2e/cases/alias/ts/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: join(__dirname, 'src/index.ts'),
},
alias: {
'@src': join(__dirname, 'src'),
},
},
});
2 changes: 2 additions & 0 deletions e2e/cases/alias/ts/src/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const a = 'hello world';
export type A = string;
5 changes: 5 additions & 0 deletions e2e/cases/alias/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { a } from '@src/a';

console.info(a);

export type { A } from './a';
10 changes: 10 additions & 0 deletions e2e/cases/alias/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@src/*": ["./src/*"]
}
},
"include": ["src"]
}
62 changes: 24 additions & 38 deletions e2e/cases/define/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
import { join } from 'node:path';
import { type RslibConfig, build } from '@rslib/core';
import { build } from '@rslib/core';
import { expect, test } from 'vitest';
import { globContentJSON } from '#helper';
import { getEntryJsResults } from '#shared';
import { loadConfig } from '../../../packages/core/src/config';

test('define', async () => {
test('define in js', async () => {
delete process.env.NODE_ENV;

const rslibConfig: RslibConfig = {
lib: [
{
format: 'esm',
output: {
distPath: {
root: join(__dirname, './dist/esm'),
},
},
},
{
format: 'cjs',
output: {
distPath: {
root: join(__dirname, './dist/cjs'),
},
},
},
],
source: {
entry: {
main: join(__dirname, './js/src/index.js'),
},
define: {
VERSION: JSON.stringify('1.0.0'),
},
},
};

const fixturePath = join(__dirname, 'js');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
const instance = await build(rslibConfig);
const results = await getEntryJsResults(instance);

expect(results.esm).not.toContain('console.info(VERSION)');
expect(results.esm).toContain('1.0.0');
expect(results.cjs).not.toContain('console.info(VERSION)');
expect(results.cjs).toContain('1.0.0');
});

const results = await globContentJSON(instance[0]!.context.distPath, {
absolute: true,
ignore: ['/**/*.map'],
});
test('define in ts', async () => {
delete process.env.NODE_ENV;

const fixturePath = join(__dirname, 'ts');
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
const instance = await build(rslibConfig);
const results = await getEntryJsResults(instance);

const entryJs = Object.keys(results).find((file) => file.endsWith('.js'));
expect(results[entryJs!]).not.toContain('console.info(VERSION)');
expect(results.esm).not.toContain('console.info(VERSION)');
expect(results.esm).toContain('1.0.0');
expect(results.cjs).not.toContain('console.info(VERSION)');
expect(results.cjs).toContain('1.0.0');
});
15 changes: 15 additions & 0 deletions e2e/cases/define/js/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: join(__dirname, 'src/index.js'),
},
define: {
VERSION: JSON.stringify('1.0.0'),
},
},
});
15 changes: 15 additions & 0 deletions e2e/cases/define/ts/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: join(__dirname, 'src/index.ts'),
},
define: {
VERSION: JSON.stringify('1.0.0'),
},
},
});
1 change: 1 addition & 0 deletions e2e/cases/define/ts/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare const VERSION: number;
1 change: 1 addition & 0 deletions e2e/cases/define/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.info(VERSION);
3 changes: 2 additions & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"private": true,
"type": "module",
"imports": {
"#helper": "./scripts/helper.ts"
"#helper": "./scripts/helper.ts",
"#shared": "./scripts/shared.ts"
},
"scripts": {
"test": "playwright test"
Expand Down
45 changes: 45 additions & 0 deletions e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { join } from 'node:path';
import type { LibConfig, RsbuildInstance } from '@rslib/core';
import { globContentJSON } from '#helper';

export function generateBundleEsmConfig(cwd: string): LibConfig {
return {
format: 'esm',
output: {
distPath: {
root: join(cwd, './dist/esm'),
},
},
};
}

export function generateBundleCjsConfig(cwd: string): LibConfig {
return {
format: 'cjs',
output: {
distPath: {
root: join(cwd, './dist/cjs'),
},
},
};
}

export async function getEntryJsResults(instance: RsbuildInstance[]) {
const results: Record<string, string> = {};

for (let i = 0; i < instance.length; i++) {
const result = await globContentJSON(instance[i]!.context.distPath, {
absolute: true,
ignore: ['/**/*.map'],
});

const entryJs = Object.keys(result).find((file) => file.endsWith('.js'));
const format = instance[i]!.context.distPath.split('/').pop();
Timeless0911 marked this conversation as resolved.
Show resolved Hide resolved

if (entryJs) {
results[format!] = result[entryJs]!;
}
}

return results;
}
2 changes: 1 addition & 1 deletion e2e/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"noEmit": true,
"composite": true
},
"include": ["cases", "playwright.config.ts", "cases/**/*.test.ts", "scripts"],
"include": ["cases/**/*.ts", "playwright.config.ts", "scripts"],
"exclude": ["**/node_modules", "**/.*/"],
"references": [
{
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"prebundle": "nx run-many -t prebundle",
"prepare": "pnpm run build && simple-git-hooks",
"sort-package-json": "npx sort-package-json \"packages/*/package.json\"",
"test:artifact": "vitest run --project artifact",
"test:artifact": "vitest run --project artifact",
"test:e2e": "cd e2e && pnpm run test",
"test:unit": "vitest run --project unit",
"test:unit": "vitest run --project unit",
"watch": "pnpm build --watch"
},
"simple-git-hooks": {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export * from './utils/helper';
export * from './types/config';

export const version = RSLIB_VERSION;

export type { RsbuildInstance } from '@rsbuild/core';
Loading