From 33c7a67661613e0a05d88daa86c0776a66b8bc34 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 8 Nov 2023 06:57:15 +0800 Subject: [PATCH 1/5] Fix ts configuration reoslving --- src/build-config.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/build-config.ts b/src/build-config.ts index 6f3732a5..c362f842 100644 --- a/src/build-config.ts +++ b/src/build-config.ts @@ -67,14 +67,14 @@ function getBuildEnv(envs: string[]) { return envVars } -function buildInputConfig( +async function buildInputConfig( entry: string, pkg: PackageMetadata, options: BundleOptions, cwd: string, { tsConfigPath, tsCompilerOptions }: TypescriptOptions, dts: boolean, -): InputOptions { +): Promise { const hasNoExternal = options.external === null const externals = hasNoExternal ? [] @@ -124,9 +124,9 @@ function buildInputConfig( // common plugins for both dts and ts assets that need to be processed const commonPlugins = [sizePlugin] - let baseResolvedTsOptions + let baseResolvedTsOptions: TypescriptOptions['tsCompilerOptions'] | undefined if (dts && useTypescript) { - baseResolvedTsOptions = convertCompilerOptions(cwd, { + baseResolvedTsOptions = (await convertCompilerOptions(cwd, { declaration: true, noEmit: false, noEmitOnError: true, @@ -139,7 +139,7 @@ function buildInputConfig( module: 'esnext', incremental: false, jsx: tsCompilerOptions.jsx || 'react', - }) + })).options } const plugins: Plugin[] = ( @@ -363,7 +363,7 @@ export async function buildEntryConfig( return (await Promise.all(configs)).filter(nonNullable) } -function buildConfig( +async function buildConfig( entry: string, pkg: PackageMetadata, exportPaths: ExportPaths, @@ -372,11 +372,11 @@ function buildConfig( cwd: string, tsOptions: TypescriptOptions, dts: boolean, -): BuncheeRollupConfig { +): Promise { const { file } = bundleConfig const useTypescript = Boolean(tsOptions.tsConfigPath) const options = { ...bundleConfig, useTypescript } - const inputOptions = buildInputConfig( + const inputOptions = await buildInputConfig( entry, pkg, options, From fe671cbc31c31aa5d1095465d4b51e2226d675ea Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 8 Nov 2023 07:59:19 +0800 Subject: [PATCH 2/5] add test, refactor --- src/build-config.ts | 63 ++++++++++--------- test/integration.test.ts | 19 ++++++ test/integration/ts-incremental/package.json | 5 ++ test/integration/ts-incremental/src/index.ts | 1 + test/integration/ts-incremental/tsconfig.json | 5 ++ 5 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 test/integration/ts-incremental/package.json create mode 100644 test/integration/ts-incremental/src/index.ts create mode 100644 test/integration/ts-incremental/tsconfig.json diff --git a/src/build-config.ts b/src/build-config.ts index c362f842..3221102c 100644 --- a/src/build-config.ts +++ b/src/build-config.ts @@ -8,9 +8,8 @@ import type { FullExportCondition, } from './types' import type { InputOptions, OutputOptions, Plugin } from 'rollup' -import type { TypescriptOptions } from './typescript' - -import { convertCompilerOptions } from './typescript' +import { convertCompilerOptions, type TypescriptOptions } from './typescript' +import type { CompilerOptions } from 'typescript' import { resolve, dirname } from 'path' import { wasm } from '@rollup/plugin-wasm' import { swc } from 'rollup-plugin-swc3' @@ -91,7 +90,7 @@ async function buildInputConfig( minify: shouldMinify, } = options const hasSpecifiedTsTarget = Boolean( - tsCompilerOptions?.target && tsConfigPath, + tsCompilerOptions.target && tsConfigPath, ) const swcParserConfig = { @@ -124,38 +123,44 @@ async function buildInputConfig( // common plugins for both dts and ts assets that need to be processed const commonPlugins = [sizePlugin] - let baseResolvedTsOptions: TypescriptOptions['tsCompilerOptions'] | undefined - if (dts && useTypescript) { - baseResolvedTsOptions = (await convertCompilerOptions(cwd, { - declaration: true, - noEmit: false, - noEmitOnError: true, - emitDeclarationOnly: true, - checkJs: false, - declarationMap: false, - skipLibCheck: true, - preserveSymlinks: false, + const baseResolvedTsOptions: CompilerOptions = { + declaration: true, + noEmit: false, + noEmitOnError: true, + emitDeclarationOnly: true, + checkJs: false, + declarationMap: false, + skipLibCheck: true, + preserveSymlinks: false, + incremental: false, + } + + const typesPlugins = [ + ...commonPlugins, + inlineCss({ skip: true }), + ] + + if (useTypescript) { + const { options: resolvedTsOptions } = await convertCompilerOptions(cwd, { + ...baseResolvedTsOptions, target: 'esnext', module: 'esnext', - incremental: false, jsx: tsCompilerOptions.jsx || 'react', - })).options + }) + const dtsPlugin = (require('rollup-plugin-dts') as typeof import('rollup-plugin-dts')).default({ + tsconfig: tsConfigPath, + compilerOptions: { + ...tsCompilerOptions, + ...resolvedTsOptions, + }, + }) + typesPlugins.push(dtsPlugin) } + const plugins: Plugin[] = ( dts - ? [ - ...commonPlugins, - inlineCss({ skip: true }), - useTypescript && - require('rollup-plugin-dts').default({ - tsconfig: tsConfigPath, - compilerOptions: { - ...tsCompilerOptions, - ...baseResolvedTsOptions, - }, - }), - ] + ? typesPlugins : [ ...commonPlugins, inlineCss({ exclude: /node_modules/ }), diff --git a/test/integration.test.ts b/test/integration.test.ts index 6eecff2f..3d882cf8 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -1,4 +1,5 @@ import fs from 'fs/promises' +import fsSync from 'fs' import { execSync, fork } from 'child_process' import { resolve, join } from 'path' import { stripANSIColor, existsFile, assertFilesContent } from './testing-utils' @@ -223,6 +224,24 @@ const testCases: { ) }, }, + { + name: 'ts-incremental', + args: [], + async expected(dir) { + const distFiles = [ + './dist/index.js', + './dist/index.d.ts', + ] + + for (const f of distFiles) { + expect(await existsFile(join(dir, f))).toBe(true) + } + expect(await fs.readFile(join(dir, distFiles[1]), 'utf-8')).toContain( + 'declare const _default: () => string;', + ) + expect(await existsFile(join(dir, './dist/.tsbuildinfo'))).toBe(false) + }, + }, { name: 'publint', args: [], diff --git a/test/integration/ts-incremental/package.json b/test/integration/ts-incremental/package.json new file mode 100644 index 00000000..d9bb004b --- /dev/null +++ b/test/integration/ts-incremental/package.json @@ -0,0 +1,5 @@ +{ + "name": "ts-incremental", + "types": "./dist/index.d.ts", + "exports": "./dist/index.js" +} diff --git a/test/integration/ts-incremental/src/index.ts b/test/integration/ts-incremental/src/index.ts new file mode 100644 index 00000000..203990cf --- /dev/null +++ b/test/integration/ts-incremental/src/index.ts @@ -0,0 +1 @@ +export default () => 'index' diff --git a/test/integration/ts-incremental/tsconfig.json b/test/integration/ts-incremental/tsconfig.json new file mode 100644 index 00000000..b315a2a5 --- /dev/null +++ b/test/integration/ts-incremental/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "incremental": true + } +} From c82f65ca3574898e67ce8643ef7fa280927298e1 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 8 Nov 2023 08:05:11 +0800 Subject: [PATCH 3/5] fix --- src/build-config.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/build-config.ts b/src/build-config.ts index 3221102c..e0a1010d 100644 --- a/src/build-config.ts +++ b/src/build-config.ts @@ -8,8 +8,8 @@ import type { FullExportCondition, } from './types' import type { InputOptions, OutputOptions, Plugin } from 'rollup' -import { convertCompilerOptions, type TypescriptOptions } from './typescript' -import type { CompilerOptions } from 'typescript' +import { type TypescriptOptions } from './typescript' + import { resolve, dirname } from 'path' import { wasm } from '@rollup/plugin-wasm' import { swc } from 'rollup-plugin-swc3' @@ -123,7 +123,7 @@ async function buildInputConfig( // common plugins for both dts and ts assets that need to be processed const commonPlugins = [sizePlugin] - const baseResolvedTsOptions: CompilerOptions = { + const baseResolvedTsOptions: any = { declaration: true, noEmit: false, noEmitOnError: true, @@ -133,6 +133,9 @@ async function buildInputConfig( skipLibCheck: true, preserveSymlinks: false, incremental: false, + target: 'esnext', + module: 'esnext', + jsx: tsCompilerOptions.jsx || 'react', } const typesPlugins = [ @@ -141,17 +144,11 @@ async function buildInputConfig( ] if (useTypescript) { - const { options: resolvedTsOptions } = await convertCompilerOptions(cwd, { - ...baseResolvedTsOptions, - target: 'esnext', - module: 'esnext', - jsx: tsCompilerOptions.jsx || 'react', - }) const dtsPlugin = (require('rollup-plugin-dts') as typeof import('rollup-plugin-dts')).default({ tsconfig: tsConfigPath, compilerOptions: { + ...baseResolvedTsOptions, ...tsCompilerOptions, - ...resolvedTsOptions, }, }) typesPlugins.push(dtsPlugin) From 28b2c9ffd22ab76928bd6ed6c3fda9357a6d5a73 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 10 Nov 2023 22:00:53 +0800 Subject: [PATCH 4/5] fix --- src/build-config.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/build-config.ts b/src/build-config.ts index e0a1010d..c49e6149 100644 --- a/src/build-config.ts +++ b/src/build-config.ts @@ -8,7 +8,7 @@ import type { FullExportCondition, } from './types' import type { InputOptions, OutputOptions, Plugin } from 'rollup' -import { type TypescriptOptions } from './typescript' +import { convertCompilerOptions, type TypescriptOptions } from './typescript' import { resolve, dirname } from 'path' import { wasm } from '@rollup/plugin-wasm' @@ -132,7 +132,10 @@ async function buildInputConfig( declarationMap: false, skipLibCheck: true, preserveSymlinks: false, + // disable incremental build incremental: false, + // use default tsBuildInfoFile value + tsBuildInfoFile: '.tsbuildinfo', target: 'esnext', module: 'esnext', jsx: tsCompilerOptions.jsx || 'react', @@ -144,17 +147,22 @@ async function buildInputConfig( ] if (useTypescript) { + const mergedOptions = { + ...baseResolvedTsOptions, + ...tsCompilerOptions, + } + + // error TS5074: Option '--incremental' can only be specified using tsconfig, emitting to single + // file or when option '--tsBuildInfoFile' is specified. + delete mergedOptions.incremental + const dtsPlugin = (require('rollup-plugin-dts') as typeof import('rollup-plugin-dts')).default({ - tsconfig: tsConfigPath, - compilerOptions: { - ...baseResolvedTsOptions, - ...tsCompilerOptions, - }, + tsconfig: undefined, + compilerOptions: mergedOptions, }) typesPlugins.push(dtsPlugin) } - const plugins: Plugin[] = ( dts ? typesPlugins From 9ff2414eb72b4c131e961edd3fa25262e2ac5ecb Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 10 Nov 2023 22:02:34 +0800 Subject: [PATCH 5/5] typecheck and delete default opt --- src/build-config.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/build-config.ts b/src/build-config.ts index c49e6149..2c2b5a33 100644 --- a/src/build-config.ts +++ b/src/build-config.ts @@ -8,7 +8,7 @@ import type { FullExportCondition, } from './types' import type { InputOptions, OutputOptions, Plugin } from 'rollup' -import { convertCompilerOptions, type TypescriptOptions } from './typescript' +import { type TypescriptOptions } from './typescript' import { resolve, dirname } from 'path' import { wasm } from '@rollup/plugin-wasm' @@ -154,7 +154,10 @@ async function buildInputConfig( // error TS5074: Option '--incremental' can only be specified using tsconfig, emitting to single // file or when option '--tsBuildInfoFile' is specified. - delete mergedOptions.incremental + if (!mergedOptions.incremental) { + delete mergedOptions.incremental + delete mergedOptions.tsBuildInfoFile + } const dtsPlugin = (require('rollup-plugin-dts') as typeof import('rollup-plugin-dts')).default({ tsconfig: undefined,