Skip to content

Commit

Permalink
feat: use tsconfck
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Sep 10, 2024
1 parent 70400ea commit c1d5359
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"prebundle": "1.2.2",
"rslib": "npm:@rslib/[email protected]",
"rslog": "^1.2.3",
"tsconfck": "3.1.3",
"typescript": "^5.5.4"
},
"peerDependencies": {
Expand Down
14 changes: 4 additions & 10 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
rspack,
} from '@rsbuild/core';
import glob from 'fast-glob';
import ts from 'typescript';
import {
DEFAULT_CONFIG_NAME,
DEFAULT_EXTENSIONS,
Expand Down Expand Up @@ -41,7 +40,7 @@ import {
} from './utils/helper';
import { logger } from './utils/logger';
import { transformSyntaxToBrowserslist } from './utils/syntax';
import { getTsconfigCompilerOptions } from './utils/tsconfig';
import { loadTsconfig } from './utils/tsconfig';

/**
* This function helps you to autocomplete configuration types.
Expand Down Expand Up @@ -363,12 +362,12 @@ export function composeBannerFooterConfig(
}

export function composeDecoratorsConfig(
options: ts.CompilerOptions,
compilerOptions?: Record<string, any>,
version?: NonNullable<
NonNullable<RsbuildConfig['source']>['decorators']
>['version'],
): RsbuildConfig {
if (version || !options.experimentalDecorators) {
if (version || !compilerOptions?.experimentalDecorators) {
return {};
}

Expand Down Expand Up @@ -811,15 +810,10 @@ const composeExternalHelpersConfig = (
async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
const rootPath = dirname(configPath);
const pkgJson = readPackageJson(rootPath);
let compilerOptions: ts.CompilerOptions = {};
const tsconfigPath = ts.findConfigFile(
const { compilerOptions } = await loadTsconfig(
rootPath,
ts.sys.fileExists,
config.source?.tsconfigPath,
);
if (tsconfigPath) {
compilerOptions = getTsconfigCompilerOptions(tsconfigPath);
}

const {
format,
Expand Down
34 changes: 17 additions & 17 deletions packages/core/src/utils/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import path from 'node:path';
import ts from 'typescript';
import { basename, join } from 'node:path';
import { find, parse } from 'tsconfck';

export function loadTsconfig(tsconfigPath: string): ts.ParsedCommandLine {
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
const configFileContent = ts.parseJsonConfigFileContent(
configFile.config,
ts.sys,
path.dirname(tsconfigPath),
);
export async function loadTsconfig(
root: string,
tsconfigPath = 'tsconfig.json',
): Promise<{
compilerOptions?: Record<string, any>;
}> {
const tsconfigFileName = await find(join(root, tsconfigPath), {
root,
configName: basename(tsconfigPath),
});

return configFileContent;
}

export function getTsconfigCompilerOptions(
tsconfigPath: string,
): ts.CompilerOptions {
const { options } = loadTsconfig(tsconfigPath);
if (tsconfigFileName) {
const { tsconfig } = await parse(tsconfigFileName);
return tsconfig;
}

return options;
return {};
}
19 changes: 13 additions & 6 deletions packages/core/tests/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { describe, expect, it, vi } from 'vitest';
import type { Format, PkgJson } from '../src/types';
import { getDefaultExtension } from '../src/utils/extension';

type Options = {
format: Format;
pkgJson?: PkgJson;
autoExtension: boolean;
};

vi.mock('rslog');

describe('should get extension correctly', () => {
it('autoExtension is false', () => {
const options = {
const options: Options = {
format: 'cjs',
pkgJson: {},
autoExtension: false,
Expand All @@ -20,7 +27,7 @@ describe('should get extension correctly', () => {
});

it('package.json is broken', () => {
const options = {
const options: Options = {
format: 'cjs',
autoExtension: true,
};
Expand All @@ -34,7 +41,7 @@ describe('should get extension correctly', () => {
});

it('format is cjs and type is module in package.json', () => {
const options = {
const options: Options = {
format: 'cjs',
pkgJson: {
type: 'module',
Expand All @@ -52,7 +59,7 @@ describe('should get extension correctly', () => {
});

it('format is cjs and type is commonjs in package.json', () => {
const options = {
const options: Options = {
format: 'cjs',
pkgJson: {
type: 'commonjs',
Expand All @@ -70,7 +77,7 @@ describe('should get extension correctly', () => {
});

it('format is esm and type is commonjs in package.json', () => {
const options = {
const options: Options = {
format: 'esm',
pkgJson: {
type: 'commonjs',
Expand All @@ -88,7 +95,7 @@ describe('should get extension correctly', () => {
});

it('format is esm and type is module in package.json', () => {
const options = {
const options: Options = {
format: 'esm',
pkgJson: {
type: 'module',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"rootDir": "custom"
}
}
5 changes: 5 additions & 0 deletions packages/core/tests/fixtures/tsconfig/exist/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"rootDir": "./"
}
}
1 change: 1 addition & 0 deletions packages/core/tests/fixtures/tsconfig/no-exist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
33 changes: 33 additions & 0 deletions packages/core/tests/tsconfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import { loadTsconfig } from '../src/utils/tsconfig';

describe('loadTsconfig', () => {
it('should return tsconfig when a valid tsconfig file is found', async () => {
const fixtureDir = join(__dirname, 'fixtures/tsconfig/exist');
const result1 = await loadTsconfig(fixtureDir);
const result2 = await loadTsconfig(fixtureDir, './tsconfig.custom.json');

expect(result1).toMatchInlineSnapshot(`
{
"compilerOptions": {
"rootDir": "./",
},
}
`);
expect(result2).toMatchInlineSnapshot(`
{
"compilerOptions": {
"rootDir": "custom",
},
}
`);
});

it('should return an empty object when no tsconfig file is found', async () => {
const fixtureDir = join(__dirname, 'fixtures/tsconfig/no-exist');
const result = await loadTsconfig(fixtureDir);

expect(result).toEqual({});
});
});
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripts/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ templating
transpiling
treeshaking
tsbuildinfo
tsconfck
tsdoc
tsup
unocss
Expand Down

0 comments on commit c1d5359

Please sign in to comment.