@excute/cli
Version:
Execute JSX on anywhere
90 lines (89 loc) • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.compile = exports.run = void 0;
const path = require("path");
const ts = require("typescript");
const file_1 = require("./file");
const tempDir = path.join(process.cwd(), ".cache/.excute");
function run(transpiledResult) {
require(transpiledResult.transpiledFileNames[0]);
}
exports.run = run;
function getProgram(fileNames, options) {
const program = ts.createProgram(fileNames, options);
const emitResult = program.emit();
const allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
allDiagnostics.forEach((diagnostic) => {
if (diagnostic.file) {
let { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
}
else {
console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
}
});
if (emitResult.emitSkipped) {
throw new Error("Compile failed");
}
return program;
}
function getHost(program) {
return {
...program,
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory,
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
onUnRecoverableConfigFileDiagnostic: () => {
// TODO: error handling
},
};
}
function getOutputFileNames(fileNames, options, host) {
var _a;
const configPath = ts.findConfigFile(host.getCurrentDirectory(), ts.sys.fileExists, "tsconfig.json");
if (!configPath) {
throw new Error("Could not find a valid 'tsconfig.json'.");
}
if (((_a = process.env) === null || _a === void 0 ? void 0 : _a.EXCUTE_VERBOSE) === "true") {
console.log("Read tsconfig:", configPath);
}
const cmdLine = ts.getParsedCommandLineOfConfigFile(configPath, options, host);
return ts.getOutputFileNames(cmdLine, fileNames[0], false);
}
// TODO: apply adapter pattern
function compile(fileNames, options = {
get outDir() {
return path.join(tempDir, (0, file_1.getHashByFilenames)(fileNames));
},
jsx: ts.JsxEmit.ReactJSX,
jsxImportSource: "@excute/core",
declaration: false,
declarationMap: false,
esModuleInterop: true,
forceConsistentCasingInFileNames: true,
inlineSourceMap: true,
isolatedModules: false,
noUnusedLocals: false,
noUnusedParameters: false,
preserveWatchOutput: true,
skipLibCheck: true,
strict: true,
noEmitOnError: true,
get rootDir() {
return path.dirname(fileNames[0]);
},
}) {
var _a;
const program = getProgram(fileNames, options);
const host = getHost(program);
const transpiledFileNames = getOutputFileNames(fileNames, options, host);
if (((_a = process.env) === null || _a === void 0 ? void 0 : _a.EXCUTE_VERBOSE) === "true") {
console.log("Out dir:", options.outDir);
}
return { transpiledFileNames };
}
exports.compile = compile;