@@ -195,15 +195,7 @@ function validateCommand(command, args = []) {
195195 */
196196function parseArgs ( ) {
197197 const args = process . argv . slice ( 2 ) ;
198- const config = {
199- entry : 'src/index.ts' ,
200- outDir : 'dist' ,
201- formats : [ 'esm' , 'cjs' ] ,
202- clean : false ,
203- watch : false ,
204- verbose : false ,
205- dryRun : false ,
206- } ;
198+ const config = { ...defaultConfig } ;
207199
208200 for ( let i = 0 ; i < args . length ; i ++ ) {
209201 const arg = args [ i ] ;
@@ -466,41 +458,40 @@ async function cleanOutputDir(outDir, config) {
466458/**
467459 * Create TypeScript configuration for a specific module format
468460 *
469- * Generates appropriate compiler options for either ESM or CommonJS output,
470- * including correct module settings, output directories, and build options.
461+ * Reads the existing tsconfig files and creates a temporary configuration
462+ * with the appropriate output directory for the build process. This approach
463+ * centralizes configuration in the dedicated tsconfig files rather than
464+ * duplicating compiler options.
471465 *
472466 * @param {string } format - Target format ('esm' or 'cjs')
473467 * @param {Object } config - Build configuration
474468 * @param {string } config.outDir - Base output directory
475- * @returns {Object } TypeScript configuration object
469+ * @returns {Promise< Object> } TypeScript configuration object
476470 */
477- function createTsConfig ( format , config ) {
471+ async function createTsConfig ( format , config ) {
478472 // Validate paths to prevent traversal
479473 const validatedOutDir = validatePath ( config . outDir ) ;
480474
481- const baseConfig = {
482- compilerOptions : {
483- target : 'ES2020' ,
484- lib : [ 'ES2020' ] ,
485- module : format === 'esm' ? 'ESNext' : 'CommonJS' ,
486- moduleResolution : 'node' ,
487- declaration : true ,
488- declarationMap : true ,
489- sourceMap : true ,
490- outDir : join ( validatedOutDir , format ) ,
491- rootDir : 'src' ,
492- strict : true ,
493- esModuleInterop : true ,
494- skipLibCheck : true ,
495- forceConsistentCasingInFileNames : true ,
496- removeComments : false ,
497- preserveConstEnums : true ,
498- } ,
499- include : [ 'src/**/*' ] ,
500- exclude : [ 'node_modules' , 'dist' , '**/*.test.ts' , '**/*.spec.ts' ]
501- } ;
502-
503- return baseConfig ;
475+ try {
476+ // Read the appropriate existing config file
477+ const configFileName = format === 'esm' ? 'tsconfig.esm.json' : 'tsconfig.cjs.json' ;
478+ const configPath = join ( process . cwd ( ) , configFileName ) ;
479+ const configContent = await readFile ( configPath , 'utf8' ) ;
480+ const existingConfig = JSON . parse ( configContent ) ;
481+
482+ // Create a new config based on the existing one, only overriding the outDir
483+ const tsConfig = {
484+ ...existingConfig ,
485+ compilerOptions : {
486+ ...existingConfig . compilerOptions ,
487+ outDir : join ( validatedOutDir , format )
488+ }
489+ } ;
490+
491+ return tsConfig ;
492+ } catch ( error ) {
493+ throw new Error ( `Failed to read TypeScript config for ${ format } : ${ error . message } ` ) ;
494+ }
504495}
505496
506497/**
@@ -731,7 +722,7 @@ async function createTempTsConfig(format, config) {
731722 const TEMP_CONFIG_CJS = 'tsconfig.cjs.temp.json' ;
732723
733724 const safeTempFilename = ( format === 'esm' ) ? TEMP_CONFIG_ESM : TEMP_CONFIG_CJS ;
734- const tsConfig = createTsConfig ( format , config ) ;
725+ const tsConfig = await createTsConfig ( format , config ) ;
735726 const workingDir = process . cwd ( ) ;
736727 const tempFilePath = join ( workingDir , safeTempFilename ) ;
737728
@@ -1180,4 +1171,20 @@ async function fixCjsRequires(cjsDir, config) {
11801171 return { filesFixed, requiresFixed } ;
11811172}
11821173
1174+ /**
1175+ * Default configuration object for Forklift builds
1176+ *
1177+ * This serves as the single source of truth for default configuration values,
1178+ * ensuring consistency between the main build module and the runner.
1179+ */
1180+ export const defaultConfig = {
1181+ entry : 'src/index.ts' ,
1182+ outDir : 'dist' ,
1183+ formats : [ 'esm' , 'cjs' ] ,
1184+ clean : false ,
1185+ watch : false ,
1186+ verbose : false ,
1187+ dryRun : false ,
1188+ } ;
1189+
11831190export { build , watchMode , validateConfig , fixEsmImports , createCjsPackageMarker } ;
0 commit comments