avoid temporary .ts file due to lack of sandbox on Windows

Just a quick hack for now to store it in memory, as the temp file
conflicts on Windows due to the lack of a sandbox, and we don't really
have a need to write it to the filesystem anyway.
This commit is contained in:
Damien Elmes 2021-03-21 00:17:09 +10:00
parent aabcc8a7db
commit e0b85865f4
2 changed files with 32 additions and 17 deletions

View file

@ -6,18 +6,15 @@ def _svelte(ctx):
args.use_param_file("@%s", use_always = True)
args.set_param_file_format("multiline")
temp_ts_path = ctx.actions.declare_file(ctx.attr.name + ".svelte.ts")
args.add(ctx.file.entry_point.path)
args.add(ctx.outputs.mjs.path)
args.add(ctx.outputs.dts.path)
args.add(temp_ts_path)
args.add_all(ctx.files._shims)
ctx.actions.run(
execution_requirements = {"supports-workers": "1"},
executable = ctx.executable._svelte_bin,
outputs = [ctx.outputs.mjs, ctx.outputs.dts, temp_ts_path],
outputs = [ctx.outputs.mjs, ctx.outputs.dts],
inputs = [ctx.file.entry_point] + ctx.files._shims,
mnemonic = "Svelte",
arguments = [args],

View file

@ -16,24 +16,38 @@ let parsedCommandLine: ts.ParsedCommandLine = {
},
};
let tsText = "";
// largely taken from https://github.com/Asana/bazeltsc/blob/7dfa0ba2bd5eb9ee556e146df35cf793fad2d2c3/src/bazeltsc.ts (MIT)
const languageServiceHost: ts.LanguageServiceHost = {
getCompilationSettings: (): ts.CompilerOptions => parsedCommandLine.options,
getNewLine: () => ts.sys.newLine,
getScriptFileNames: (): string[] => parsedCommandLine.fileNames,
getScriptVersion: (fileName: string): string => {
if (fileName == parsedCommandLine.fileNames[0]) {
return require("crypto").createHash("md5").update(tsText).digest("hex");
} else {
// If the file's size or modified-timestamp changed, it's a different version.
return (
ts.sys.getFileSize!(fileName) +
":" +
ts.sys.getModifiedTime!(fileName)!.getTime()
);
}
},
getScriptSnapshot: (fileName: string): ts.IScriptSnapshot | undefined => {
let text;
if (fileName == parsedCommandLine.fileNames[0]) {
// serve out generated ts file from memory, so we can avoid writing a temporary
// file that causes conflicts on Windows
text = tsText;
} else {
if (!ts.sys.fileExists(fileName)) {
return undefined;
} else {
text = ts.sys.readFile(fileName)!;
}
}
let text = ts.sys.readFile(fileName)!;
return {
getText: (start: number, end: number) => {
if (start === 0 && end === text.length) {
@ -129,7 +143,7 @@ async function writeDts(tsPath, dtsPath, shims) {
await writeFile(dtsPath, dtsSource);
}
async function writeTs(svelteSource, sveltePath, tsPath) {
function writeTs(svelteSource, sveltePath) {
let tsSource = svelte2tsx(svelteSource, {
filename: sveltePath,
strictMode: true,
@ -139,7 +153,8 @@ async function writeTs(svelteSource, sveltePath, tsPath) {
// 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"));
// write to our global
tsText = codeLines.join("\n");
}
async function writeJs(source, inputFilename, outputPath) {
@ -167,10 +182,13 @@ async function writeJs(source, inputFilename, outputPath) {
}
async function compileSvelte(args) {
const [sveltePath, mjsPath, dtsPath, tempTsPath, ...shims] = args;
const [sveltePath, mjsPath, dtsPath, ...shims] = args;
const svelteSource = await readFile(sveltePath);
await writeTs(svelteSource, sveltePath, tempTsPath);
// mock filename
const tempTsPath = sveltePath + ".ts";
await writeTs(svelteSource, sveltePath);
await writeDts(tempTsPath, dtsPath, shims);
await writeJs(svelteSource, sveltePath, mjsPath);