Last active
January 6, 2021 13:21
-
-
Save craigphicks/6904ca369de12390517215d1a064d226 to your computer and use it in GitHub Desktop.
Typescript - How to compile only one (or more) files from a project using all the project settings from the tsconfig file(s)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as ts from 'typescript'; | |
import * as fs from 'fs'; | |
import * as path from 'path'; | |
import * as cp from 'child_process'; | |
import * as dm from '../src-ts/deep-merge-objects'; | |
function compile(fileNames: string[], options: ts.CompilerOptions): void { | |
const program = ts.createProgram(fileNames, options); | |
const emitResult = program.emit(); | |
const allDiagnostics = ts | |
.getPreEmitDiagnostics(program) | |
.concat(emitResult.diagnostics); | |
allDiagnostics.forEach((diagnostic) => { | |
if (diagnostic.file) { | |
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition( | |
diagnostic.start! | |
); | |
const message = ts.flattenDiagnosticMessageText( | |
diagnostic.messageText, | |
'\n' | |
); | |
console.log( | |
`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}` | |
); | |
} else { | |
console.log( | |
ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n') | |
); | |
} | |
}); | |
const exitCode = emitResult.emitSkipped ? 1 : 0; | |
console.log(`Process exiting with code '${exitCode}'.`); | |
process.exit(exitCode); | |
} | |
function main(): void { | |
fs.mkdirSync('./dev-out',{recursive:true}) | |
cp.execSync('cp -r ./testlib-js ./dev-out/'); | |
const tscfgFilenames = [ | |
'@tsconfig/node14/tsconfig.json', | |
path.join(process.cwd(), 'tsconfig.base.json'), | |
path.join(process.cwd(), 'tsconfig.json'), | |
]; | |
const tscfg = tscfgFilenames.map((fn) => require(fn).compilerOptions); | |
const compilerOptions: ts.CompilerOptions = dm.deepMerge( | |
dm.deepMergeInnerDedupeArrays, | |
...tscfg | |
); | |
if (compilerOptions.lib && compilerOptions.lib.length) | |
compilerOptions.lib = compilerOptions.lib.map((s) => 'lib.' + s + '.d.ts'); | |
console.log(JSON.stringify(compilerOptions, null, 2)); | |
compile(process.argv.slice(2), compilerOptions); | |
} | |
try { | |
main(); | |
if (process.exitCode === 1) console.log('compiler produced no output'); | |
} catch (e) { | |
console.error(e.message); | |
process.exitCode = 2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as ts from 'typescript'; | |
import * as fs from 'fs'; | |
import * as path from 'path'; | |
import * as cp from 'child_process'; | |
import * as dm from '../src-ts/deep-merge-objects'; | |
function compile(fileNames: string[], options: ts.CompilerOptions): void { | |
const program = ts.createProgram(fileNames, options); | |
const emitResult = program.emit(); | |
const allDiagnostics = ts | |
.getPreEmitDiagnostics(program) | |
.concat(emitResult.diagnostics); | |
allDiagnostics.forEach((diagnostic) => { | |
if (diagnostic.file) { | |
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition( | |
diagnostic.start! | |
); | |
const message = ts.flattenDiagnosticMessageText( | |
diagnostic.messageText, | |
'\n' | |
); | |
console.log( | |
`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}` | |
); | |
} else { | |
console.log( | |
ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n') | |
); | |
} | |
}); | |
const exitCode = emitResult.emitSkipped ? 1 : 0; | |
console.log(`Process exiting with code '${exitCode}'.`); | |
process.exit(exitCode); | |
} | |
function main(): void { | |
fs.mkdirSync('./dev-out',{recursive:true}) | |
cp.execSync('cp -r ./testlib-js ./dev-out/'); | |
const tscfgFilenames = [ | |
'@tsconfig/node14/tsconfig.json', | |
path.join(process.cwd(), 'tsconfig.base.json'), | |
path.join(process.cwd(), 'tsconfig.json'), | |
]; | |
const tscfg = tscfgFilenames.map((fn) => require(fn).compilerOptions); | |
const compilerOptions: ts.CompilerOptions = dm.deepMerge( | |
dm.deepMergeInnerDedupeArrays, | |
...tscfg | |
); | |
if (compilerOptions.lib && compilerOptions.lib.length) | |
compilerOptions.lib = compilerOptions.lib.map((s) => 'lib.' + s + '.d.ts'); | |
console.log(JSON.stringify(compilerOptions, null, 2)); | |
compile(process.argv.slice(2), compilerOptions); | |
} | |
try { | |
main(); | |
if (process.exitCode === 1) console.log('compiler produced no output'); | |
} catch (e) { | |
console.error(e.message); | |
process.exitCode = 2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"compilerOptions": { | |
"outDir": "dev-out", | |
"rootDir": "./" | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Started off with the minimal compiler example
Had to add these items -
es2020
=>lib.es2020.d.ts
)Also note that is a good to have
rootDir
specified in a tsconfig. Otherwise it might not create the directory levels correctly.