2021-03-20 05:56:08 +01:00
|
|
|
const fs = require("fs");
|
2021-03-20 11:42:29 +01:00
|
|
|
const worker = require("@bazel/worker");
|
|
|
|
const svelte2tsx = require("svelte2tsx");
|
|
|
|
const preprocess = require("svelte-preprocess");
|
2021-03-20 14:13:27 +01:00
|
|
|
import * as ts from "typescript";
|
|
|
|
import * as svelte from "svelte/compiler";
|
2021-03-20 05:56:08 +01:00
|
|
|
|
2021-03-20 11:42:29 +01:00
|
|
|
const tsOptions = {
|
2021-03-20 14:13:27 +01:00
|
|
|
jsx: ts.JsxEmit.Preserve,
|
2021-03-20 11:42:29 +01:00
|
|
|
declaration: true,
|
|
|
|
emitDeclarationOnly: true,
|
|
|
|
skipLibCheck: true,
|
|
|
|
};
|
|
|
|
const tsHost = ts.createCompilerHost(tsOptions);
|
2021-03-20 05:56:08 +01:00
|
|
|
|
2021-03-20 11:42:29 +01:00
|
|
|
function writeFile(file, data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.writeFile(file, data, (err) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-03-20 05:56:08 +01:00
|
|
|
|
2021-03-20 11:42:29 +01:00
|
|
|
function readFile(file) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.readFile(file, "utf8", (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-03-20 05:56:08 +01:00
|
|
|
|
2021-03-20 11:42:29 +01:00
|
|
|
function buildDeclarations(svelteTsFile, shims) {
|
|
|
|
const createdFiles = {}; //test2
|
|
|
|
tsHost.writeFile = (fileName, contents) => (createdFiles[fileName] = contents);
|
|
|
|
const program = ts.createProgram([svelteTsFile, ...shims], tsOptions, tsHost);
|
|
|
|
program.emit();
|
|
|
|
const dtsSource = createdFiles[svelteTsFile.replace(".ts", ".d.ts")];
|
|
|
|
return dtsSource;
|
|
|
|
}
|
2021-03-20 05:56:08 +01:00
|
|
|
|
2021-03-20 11:42:29 +01:00
|
|
|
async function writeDts(tsPath, dtsPath, shims) {
|
|
|
|
const dtsSource = buildDeclarations(tsPath, shims);
|
|
|
|
await writeFile(dtsPath, dtsSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function writeTs(svelteSource, sveltePath, tsPath) {
|
|
|
|
let tsSource = svelte2tsx(svelteSource, {
|
|
|
|
filename: sveltePath,
|
|
|
|
strictMode: true,
|
|
|
|
isTsFile: true,
|
|
|
|
});
|
|
|
|
let codeLines = tsSource.code.split("\n");
|
|
|
|
// replace the "///<reference types="svelte" />" with a line
|
|
|
|
// turning off checking, as we'll use svelte-check for that
|
|
|
|
codeLines[0] = "// @ts-nocheck";
|
|
|
|
await writeFile(tsPath, codeLines.join("\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//test
|
|
|
|
async function writeJs(source, inputFilename, outputPath) {
|
|
|
|
const preprocessOptions = preprocess({});
|
|
|
|
preprocessOptions.filename = inputFilename;
|
2021-03-20 05:56:08 +01:00
|
|
|
|
|
|
|
try {
|
2021-03-20 11:42:29 +01:00
|
|
|
const processed = await svelte.preprocess(source, preprocessOptions);
|
2021-03-20 14:13:27 +01:00
|
|
|
const result = svelte.compile(processed.toString!(), {
|
2021-03-20 11:42:29 +01:00
|
|
|
format: "esm",
|
|
|
|
generate: "dom",
|
|
|
|
filename: outputPath,
|
|
|
|
});
|
|
|
|
// warnings are an error
|
|
|
|
if (result.warnings.length > 0) {
|
|
|
|
console.log(`warnings during compile: ${result.warnings}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const outputSource = result.js.code;
|
|
|
|
await writeFile(outputPath, outputSource);
|
2021-03-20 05:56:08 +01:00
|
|
|
} catch (err) {
|
2021-03-20 11:42:29 +01:00
|
|
|
console.log(`compile failed: ${err}`);
|
|
|
|
return;
|
2021-03-20 05:56:08 +01:00
|
|
|
}
|
2021-03-20 11:42:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function compileSvelte(args) {
|
|
|
|
const [sveltePath, mjsPath, dtsPath, tempTsPath, ...shims] = args;
|
|
|
|
const svelteSource = await readFile(sveltePath);
|
|
|
|
|
|
|
|
await writeTs(svelteSource, sveltePath, tempTsPath);
|
|
|
|
await writeDts(tempTsPath, dtsPath, shims);
|
|
|
|
await writeJs(svelteSource, sveltePath, mjsPath);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function main(args) {
|
|
|
|
if (worker.runAsWorker(process.argv)) {
|
|
|
|
worker.log("Svelte running as a Bazel worker");
|
|
|
|
worker.runWorkerLoop(compileSvelte);
|
|
|
|
} else {
|
|
|
|
const paramFile = process.argv[2].replace(/^@/, "");
|
|
|
|
const commandLineArgs = fs.readFileSync(paramFile, "utf-8").trim().split("\n");
|
|
|
|
console.log("Svelte running as a standalone process");
|
|
|
|
compileSvelte(commandLineArgs);
|
2021-03-20 05:56:08 +01:00
|
|
|
}
|
2021-03-20 11:42:29 +01:00
|
|
|
}
|
2021-03-20 05:56:08 +01:00
|
|
|
|
2021-03-20 11:42:29 +01:00
|
|
|
if (require.main === module) {
|
|
|
|
main(process.argv.slice(2));
|
|
|
|
process.exitCode = 0;
|
|
|
|
}
|