diff --git a/.gitignore b/.gitignore index 364c57a0e..e86631309 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ compiled/ coverage/ doc_build/ playwright-report/ +tsconfig.tsbuildinfo .vscode/**/* !.vscode/settings.json diff --git a/e2e/cases/alias/index.test.ts b/e2e/cases/alias/index.test.ts new file mode 100644 index 000000000..af29127e8 --- /dev/null +++ b/e2e/cases/alias/index.test.ts @@ -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')); + await build(rslibConfig); + const results = await getEntryJsResults(rslibConfig); + + 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')); + await build(rslibConfig); + const results = await getEntryJsResults(rslibConfig); + + expect(results.esm).toContain('hello world'); + expect(results.cjs).toContain('hello world'); +}); diff --git a/e2e/cases/alias/js/rslib.config.ts b/e2e/cases/alias/js/rslib.config.ts new file mode 100644 index 000000000..db365fba0 --- /dev/null +++ b/e2e/cases/alias/js/rslib.config.ts @@ -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'), + }, + }, +}); diff --git a/e2e/cases/alias/js/src/a.js b/e2e/cases/alias/js/src/a.js new file mode 100644 index 000000000..29aa55808 --- /dev/null +++ b/e2e/cases/alias/js/src/a.js @@ -0,0 +1 @@ +export const a = 'hello world'; diff --git a/e2e/cases/alias/js/src/index.js b/e2e/cases/alias/js/src/index.js new file mode 100644 index 000000000..3228a48e3 --- /dev/null +++ b/e2e/cases/alias/js/src/index.js @@ -0,0 +1,3 @@ +import { a } from '@src/a'; + +console.info(a); diff --git a/e2e/cases/alias/ts/rslib.config.ts b/e2e/cases/alias/ts/rslib.config.ts new file mode 100644 index 000000000..1dd1a34f0 --- /dev/null +++ b/e2e/cases/alias/ts/rslib.config.ts @@ -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'), + }, + }, +}); diff --git a/e2e/cases/alias/ts/src/a.ts b/e2e/cases/alias/ts/src/a.ts new file mode 100644 index 000000000..ac65b18f2 --- /dev/null +++ b/e2e/cases/alias/ts/src/a.ts @@ -0,0 +1,2 @@ +export const a = 'hello world'; +export type A = string; diff --git a/e2e/cases/alias/ts/src/index.ts b/e2e/cases/alias/ts/src/index.ts new file mode 100644 index 000000000..37a8b95b2 --- /dev/null +++ b/e2e/cases/alias/ts/src/index.ts @@ -0,0 +1,5 @@ +import { a } from '@src/a'; + +console.info(a); + +export type { A } from './a'; diff --git a/e2e/cases/alias/ts/tsconfig.json b/e2e/cases/alias/ts/tsconfig.json new file mode 100644 index 000000000..ae0b87df5 --- /dev/null +++ b/e2e/cases/alias/ts/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "baseUrl": "./", + "paths": { + "@src/*": ["./src/*"] + } + }, + "include": ["src"] +} diff --git a/e2e/cases/define/index.test.ts b/e2e/cases/define/index.test.ts index f201352c2..f58fece24 100644 --- a/e2e/cases/define/index.test.ts +++ b/e2e/cases/define/index.test.ts @@ -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')); + await build(rslibConfig); + const results = await getEntryJsResults(rslibConfig); - const instance = await build(rslibConfig); + 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'); +}); + +test('define in ts', async () => { + delete process.env.NODE_ENV; - const results = await globContentJSON(instance[0]!.context.distPath, { - absolute: true, - ignore: ['/**/*.map'], - }); + const fixturePath = join(__dirname, 'ts'); + const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts')); + await build(rslibConfig); + const results = await getEntryJsResults(rslibConfig); - 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'); }); diff --git a/e2e/cases/define/js/rslib.config.ts b/e2e/cases/define/js/rslib.config.ts new file mode 100644 index 000000000..70f34caa3 --- /dev/null +++ b/e2e/cases/define/js/rslib.config.ts @@ -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'), + }, + }, +}); diff --git a/e2e/cases/define/ts/rslib.config.ts b/e2e/cases/define/ts/rslib.config.ts new file mode 100644 index 000000000..ca3e8e402 --- /dev/null +++ b/e2e/cases/define/ts/rslib.config.ts @@ -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'), + }, + }, +}); diff --git a/e2e/cases/define/ts/src/env.d.ts b/e2e/cases/define/ts/src/env.d.ts new file mode 100644 index 000000000..60a8b2451 --- /dev/null +++ b/e2e/cases/define/ts/src/env.d.ts @@ -0,0 +1 @@ +declare const VERSION: number; diff --git a/e2e/cases/define/ts/src/index.ts b/e2e/cases/define/ts/src/index.ts new file mode 100644 index 000000000..89e161b53 --- /dev/null +++ b/e2e/cases/define/ts/src/index.ts @@ -0,0 +1 @@ +console.info(VERSION); diff --git a/e2e/package.json b/e2e/package.json index 4093e92d7..fe91f47d2 100644 --- a/e2e/package.json +++ b/e2e/package.json @@ -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" diff --git a/e2e/scripts/shared.ts b/e2e/scripts/shared.ts new file mode 100644 index 000000000..7bba7a009 --- /dev/null +++ b/e2e/scripts/shared.ts @@ -0,0 +1,44 @@ +import { join } from 'node:path'; +import type { LibConfig, RslibConfig } 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(rslibConfig: RslibConfig) { + const results: Record = {}; + + for (const libConfig of rslibConfig.lib) { + const result = await globContentJSON(libConfig?.output?.distPath?.root!, { + absolute: true, + ignore: ['/**/*.map'], + }); + + const entryJs = Object.keys(result).find((file) => file.endsWith('.js')); + + if (entryJs) { + results[libConfig.format!] = result[entryJs]!; + } + } + + return results; +} diff --git a/e2e/tsconfig.json b/e2e/tsconfig.json index 9abcd7e59..e794b8be8 100644 --- a/e2e/tsconfig.json +++ b/e2e/tsconfig.json @@ -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": [ { diff --git a/package.json b/package.json index 67976ab90..9a52c3d14 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/packages/core/package.json b/packages/core/package.json index 4c1846671..479c94341 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -38,7 +38,7 @@ "prebundle": "prebundle" }, "dependencies": { - "@rsbuild/core": "0.7.9" + "@rsbuild/core": "0.7.10" }, "devDependencies": { "@rslib/tsconfig": "workspace:*", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4720bbca4..66d0f3e9e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -84,8 +84,8 @@ importers: packages/core: dependencies: '@rsbuild/core': - specifier: 0.7.9 - version: 0.7.9 + specifier: 0.7.10 + version: 0.7.10 devDependencies: '@rslib/tsconfig': specifier: workspace:* @@ -1094,13 +1094,13 @@ packages: cpu: [x64] os: [win32] - '@rsbuild/core@0.7.9': - resolution: {integrity: sha512-g29A68ZtrydLbWJgGlKUywyYi5ELnmuCM8CJWTxKa9itoIxAZG5h9NZ+P3VVBCaSfsY7DtFKMJ4D5cxvwJtVuw==} + '@rsbuild/core@0.7.10': + resolution: {integrity: sha512-m+JbPpuMFuVsMRcsjMxvVk6yc//OW+h72kV2DAD4neoiM0YhkEAN4TXBz3RSOomXHODhhxqhpCqz9nIw6PtvBA==} engines: {node: '>=16.0.0'} hasBin: true - '@rsbuild/shared@0.7.9': - resolution: {integrity: sha512-g/xQoa1PJR5305EXqXW5v+gFaYjzlwFvOzv0hOrmF0n2tEO1gO5PD/dwdWw1F3I9I79OshCZk0ru0XLshg/waQ==} + '@rsbuild/shared@0.7.10': + resolution: {integrity: sha512-FwTm11DP7KxQKT2mWLvwe80O5KpikgMSlqnw9CQhBaIHSYEypdJU9ZotbNsXsHdML3xcqg+S9ae3bpovC7KlwQ==} '@rspack/binding-darwin-arm64@0.7.5-canary-0d03907-20240624125011': resolution: {integrity: sha512-9e2dMR8BFB0rlfAKIVPmGCb9oaS86EWRbetWzKcqVTw46tHmi30De1eN5ElAfo3q0AtEruCfX7DDa0Shdja1xQ==} @@ -3994,16 +3994,16 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.18.0': optional: true - '@rsbuild/core@0.7.9': + '@rsbuild/core@0.7.10': dependencies: - '@rsbuild/shared': 0.7.9(@swc/helpers@0.5.3) + '@rsbuild/shared': 0.7.10(@swc/helpers@0.5.3) '@rspack/core': 0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.3) '@swc/helpers': 0.5.3 core-js: 3.36.1 html-webpack-plugin: html-rspack-plugin@5.7.2(@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.3)) postcss: 8.4.38 - '@rsbuild/shared@0.7.9(@swc/helpers@0.5.3)': + '@rsbuild/shared@0.7.10(@swc/helpers@0.5.3)': dependencies: '@rspack/core': 0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.3) caniuse-lite: 1.0.30001636