From bc9244922e551f40dd7e9841a127fbdd63f7d495 Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 8 Apr 2024 17:10:21 +0800 Subject: [PATCH] test(e2e): add case for jsconfig#paths (#2032) --- e2e/cases/source/jsconfig-paths/index.test.ts | 53 +++++++++++++++++++ e2e/cases/source/jsconfig-paths/jsconfig.json | 12 +++++ .../source/jsconfig-paths/src/common/test.js | 1 + .../source/jsconfig-paths/src/common2/test.js | 1 + e2e/cases/source/jsconfig-paths/src/index.js | 12 +++++ 5 files changed, 79 insertions(+) create mode 100644 e2e/cases/source/jsconfig-paths/index.test.ts create mode 100644 e2e/cases/source/jsconfig-paths/jsconfig.json create mode 100644 e2e/cases/source/jsconfig-paths/src/common/test.js create mode 100644 e2e/cases/source/jsconfig-paths/src/common2/test.js create mode 100644 e2e/cases/source/jsconfig-paths/src/index.js diff --git a/e2e/cases/source/jsconfig-paths/index.test.ts b/e2e/cases/source/jsconfig-paths/index.test.ts new file mode 100644 index 0000000000..86c000e187 --- /dev/null +++ b/e2e/cases/source/jsconfig-paths/index.test.ts @@ -0,0 +1,53 @@ +import { expect, test } from '@playwright/test'; +import { build, gotoPage, rspackOnlyTest } from '@e2e/helper'; + +rspackOnlyTest( + 'tsconfig paths should work and override the alias config', + async ({ page }) => { + const rsbuild = await build({ + cwd: __dirname, + runServer: true, + rsbuildConfig: { + source: { + alias: { + '@common': './src/common2', + }, + tsconfigPath: './jsconfig.json', + }, + }, + }); + + await gotoPage(page, rsbuild); + + const foo = page.locator('#foo'); + await expect(foo).toHaveText('tsconfig paths worked'); + + await rsbuild.close(); + }, +); + +rspackOnlyTest( + 'tsconfig paths should not work when aliasStrategy is "prefer-alias"', + async ({ page }) => { + const rsbuild = await build({ + cwd: __dirname, + runServer: true, + rsbuildConfig: { + source: { + alias: { + '@/common': './src/common2', + }, + aliasStrategy: 'prefer-alias', + tsconfigPath: './jsconfig.json', + }, + }, + }); + + await gotoPage(page, rsbuild); + + const foo = page.locator('#foo'); + await expect(foo).toHaveText('source.alias worked'); + + await rsbuild.close(); + }, +); diff --git a/e2e/cases/source/jsconfig-paths/jsconfig.json b/e2e/cases/source/jsconfig-paths/jsconfig.json new file mode 100644 index 0000000000..7e36bbecb0 --- /dev/null +++ b/e2e/cases/source/jsconfig-paths/jsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "@rsbuild/tsconfig/base", + "compilerOptions": { + "jsx": "react-jsx", + "baseUrl": "./", + "outDir": "./dist", + "paths": { + "@/common/*": ["./src/common/*"] + } + }, + "include": ["src"] +} diff --git a/e2e/cases/source/jsconfig-paths/src/common/test.js b/e2e/cases/source/jsconfig-paths/src/common/test.js new file mode 100644 index 0000000000..4b88550946 --- /dev/null +++ b/e2e/cases/source/jsconfig-paths/src/common/test.js @@ -0,0 +1 @@ +export const content = 'tsconfig paths worked'; diff --git a/e2e/cases/source/jsconfig-paths/src/common2/test.js b/e2e/cases/source/jsconfig-paths/src/common2/test.js new file mode 100644 index 0000000000..dd29732ad5 --- /dev/null +++ b/e2e/cases/source/jsconfig-paths/src/common2/test.js @@ -0,0 +1 @@ +export const content = 'source.alias worked'; diff --git a/e2e/cases/source/jsconfig-paths/src/index.js b/e2e/cases/source/jsconfig-paths/src/index.js new file mode 100644 index 0000000000..2e5878b881 --- /dev/null +++ b/e2e/cases/source/jsconfig-paths/src/index.js @@ -0,0 +1,12 @@ +import { content } from '@/common/test'; + +const testAliasEl = document.createElement('div'); +testAliasEl.id = 'foo'; +testAliasEl.innerHTML = content; +document.body.appendChild(testAliasEl); + +const testEl = document.createElement('div'); +testEl.id = 'test'; +testEl.innerHTML = 'Hello Rsbuild!'; + +document.body.appendChild(testEl);