Skip to content

Instantly share code, notes, and snippets.

@craigphicks
Last active January 6, 2021 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigphicks/6904ca369de12390517215d1a064d226 to your computer and use it in GitHub Desktop.
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)
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;
}
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;
}
{
"compilerOptions": {
"outDir": "dev-out",
"rootDir": "./"
},
}
@craigphicks
Copy link
Author

Started off with the minimal compiler example

Had to add these items -

  • Reading in the config file(s). That was not included in the original example.
  • Merging the config files in order. (Not a problem if you use a single file).
  • Transforming the compilerOptions.lib entries (e.g., 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment