@ibyar/cli
Version:
The Ibyar/Aurora CLI tool
47 lines • 1.46 kB
JavaScript
import { normalize } from 'path';
import ts from 'typescript/lib/tsserverlibrary.js';
import { getConfigPath, getTransformers, scanDirectives } from '../compiler/compiler.js';
let host;
let program;
let orgWriteFile;
let transformers;
/**
* The entry point for ts-loader
*/
export function initTypeScript() {
const configPath = getConfigPath();
const cmd = ts.getParsedCommandLineOfConfigFile(configPath, {}, ts.sys);
if (!cmd) {
console.log(`can't parse command line `);
throw new Error('');
}
host = ts.createCompilerHost(cmd.options);
orgWriteFile = host.writeFile;
program = ts.createProgram(cmd.fileNames, cmd.options, host);
transformers = getTransformers(program);
scanDirectives(program);
}
export function loader(contents, inputSourceMap) {
if (!program) {
try {
initTypeScript();
}
catch (e) {
this.callback(e);
}
}
let content, sourceMap;
host.writeFile = (fileName, contents) => {
if (fileName.endsWith('.map')) {
sourceMap = contents;
}
else if (fileName.endsWith('.js')) {
content = contents;
}
};
const filePath = normalize(this.resourcePath);
program.emit(program.getSourceFile(filePath), undefined, undefined, undefined, transformers);
this.callback(null, content, sourceMap);
host.writeFile = orgWriteFile;
}
//# sourceMappingURL=loader.js.map