Monday, 9 July 2018

start typescript tsc compiler from node

From the command line I can start the tsc compiler like this:

../../node_modules/.bin/tsc

I want to incorporate this into a node build script.

There is a typescript compiler for node but it seems much more work to set up rather than just shelling out. You have to pull in all the right files etc.

I have this code:

  fs.emptyDirSync(paths.appBuild);

  const json = ts.parseConfigFileTextToJson(tsconfig, ts.sys.readFile(tsconfig), true);

  const { options } = ts.parseJsonConfigFileContent(json.config, ts.sys, path.dirname(tsconfig));

  options.configFilePath = paths.tsConfig;

  options.outDir = outDir;
  options.src = src;
  options.noEmitOnError = true;
  options.pretty = true;
  options.sourceMap = process.argv.includes('--source-map');

  let rootFile = path.join(process.cwd(), 'src/index.tsx');

  if (!fs.existsSync(rootFile)) {
    rootFile = path.join(process.cwd(), 'src/index.ts');
  }

  const host = ts.createCompilerHost(options, true);
  const prog = ts.createProgram([rootFile], options, host);
  const result = prog.emit();

But This will miss files that are not imported in the RootFile.

HOw can I simply shell out to the tsc exe from node?



from start typescript tsc compiler from node

No comments:

Post a Comment