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

Fix ts configuration reoslving #281

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
83 changes: 48 additions & 35 deletions src/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import type {
FullExportCondition,
} from './types'
import type { InputOptions, OutputOptions, Plugin } from 'rollup'
import type { TypescriptOptions } from './typescript'
import { type TypescriptOptions } from './typescript'

import { convertCompilerOptions } from './typescript'
import { resolve, dirname } from 'path'
import { wasm } from '@rollup/plugin-wasm'
import { swc } from 'rollup-plugin-swc3'
Expand Down Expand Up @@ -67,14 +66,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<InputOptions> {
const hasNoExternal = options.external === null
const externals = hasNoExternal
? []
Expand All @@ -91,7 +90,7 @@ function buildInputConfig(
minify: shouldMinify,
} = options
const hasSpecifiedTsTarget = Boolean(
tsCompilerOptions?.target && tsConfigPath,
tsCompilerOptions.target && tsConfigPath,
)

const swcParserConfig = {
Expand Down Expand Up @@ -124,38 +123,52 @@ function buildInputConfig(
// common plugins for both dts and ts assets that need to be processed
const commonPlugins = [sizePlugin]

let baseResolvedTsOptions
if (dts && useTypescript) {
baseResolvedTsOptions = convertCompilerOptions(cwd, {
declaration: true,
noEmit: false,
noEmitOnError: true,
emitDeclarationOnly: true,
checkJs: false,
declarationMap: false,
skipLibCheck: true,
preserveSymlinks: false,
target: 'esnext',
module: 'esnext',
incremental: false,
jsx: tsCompilerOptions.jsx || 'react',
const baseResolvedTsOptions: any = {
declaration: true,
noEmit: false,
noEmitOnError: true,
emitDeclarationOnly: true,
checkJs: false,
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',
}

const typesPlugins = [
...commonPlugins,
inlineCss({ skip: true }),
]

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.
if (!mergedOptions.incremental) {
delete mergedOptions.incremental
delete mergedOptions.tsBuildInfoFile
}

const dtsPlugin = (require('rollup-plugin-dts') as typeof import('rollup-plugin-dts')).default({
tsconfig: undefined,
compilerOptions: mergedOptions,
})
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/ }),
Expand Down Expand Up @@ -363,7 +376,7 @@ export async function buildEntryConfig(
return (await Promise.all(configs)).filter(nonNullable)
}

function buildConfig(
async function buildConfig(
entry: string,
pkg: PackageMetadata,
exportPaths: ExportPaths,
Expand All @@ -372,11 +385,11 @@ function buildConfig(
cwd: string,
tsOptions: TypescriptOptions,
dts: boolean,
): BuncheeRollupConfig {
): Promise<BuncheeRollupConfig> {
const { file } = bundleConfig
const useTypescript = Boolean(tsOptions.tsConfigPath)
const options = { ...bundleConfig, useTypescript }
const inputOptions = buildInputConfig(
const inputOptions = await buildInputConfig(
entry,
pkg,
options,
Expand Down
19 changes: 19 additions & 0 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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: [],
Expand Down
5 changes: 5 additions & 0 deletions test/integration/ts-incremental/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "ts-incremental",
"types": "./dist/index.d.ts",
"exports": "./dist/index.js"
}
1 change: 1 addition & 0 deletions test/integration/ts-incremental/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 'index'
5 changes: 5 additions & 0 deletions test/integration/ts-incremental/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"incremental": true
}
}