@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
160 lines (158 loc) • 6.29 kB
JavaScript
import { __name } from './chunk-43IZMM3W.js';
import { listFiles } from '@stryke/fs/list-files';
import { readFile } from '@stryke/fs/read-file';
import { findFileName } from '@stryke/path/file-path-fns';
import { joinPaths } from '@stryke/path/join-paths';
import { resolvePackage } from '@stryke/path/resolve';
import defu from 'defu';
import { minimatch } from 'minimatch';
import ts from 'typescript';
var SourcesMap = Map;
async function loadLibFiles() {
const libLocation = await resolvePackage("typescript");
if (!libLocation) {
throw new Error("Could not resolve TypeScript package location.");
}
const libFiles = await listFiles(joinPaths(libLocation, "lib", "**", "lib.*.d.ts"));
if (libFiles.length === 0) {
throw new Error("No TypeScript library files found.");
}
const lib = /* @__PURE__ */ new Map();
for (const file of libFiles) {
lib.set(`/node_modules/typescript/lib/${findFileName(file)}`, await readFile(file));
}
return lib;
}
__name(loadLibFiles, "loadLibFiles");
function getDefaultCompilerOptions() {
return {
...ts.getDefaultCompilerOptions(),
jsx: ts.JsxEmit.React,
strict: true,
esModuleInterop: true,
module: ts.ModuleKind.ESNext,
suppressOutputPathCheck: true,
skipLibCheck: true,
skipDefaultLibCheck: true,
moduleResolution: ts.ModuleResolutionKind.Node10
};
}
__name(getDefaultCompilerOptions, "getDefaultCompilerOptions");
async function createVirtualProgram(rootNames, context, compilerOptions = {}) {
const options = defu(compilerOptions, getDefaultCompilerOptions());
const host = {
name: "storm-vfs",
root: context.options.workspaceRoot,
...ts.sys,
realpath: /* @__PURE__ */ __name((path) => {
if (context.vfs.existsSync(path)) {
return context.vfs.resolvePath(path);
}
return ts.sys.realpath?.(path) ?? path;
}, "realpath"),
getCurrentDirectory() {
return context.options.workspaceRoot;
},
getCanonicalFileName(fileName) {
return fileName;
},
getDefaultLibFileName(_options) {
return ts.getDefaultLibFileName(options);
},
getDefaultLibLocation() {
return "/";
},
getNewLine() {
return "\n";
},
useCaseSensitiveFileNames() {
return true;
},
fileExists(fileName) {
return context.vfs.existsSync(fileName);
},
readFile(fileName) {
if (context.vfs.existsSync(fileName)) {
return context.vfs.readFileSync(fileName);
}
return void 0;
},
readDirectory: /* @__PURE__ */ __name((path, extensions = [], exclude = [], include = []) => {
let results = [];
if (context.vfs.existsSync(path)) {
results = context.vfs.readdirSync(path, {
encoding: "utf8",
recursive: true
});
if (extensions.length > 0) {
results = results.filter((file) => extensions.some((ext) => file.endsWith(ext.startsWith(".") ? ext : `.${ext}`)));
}
if (exclude.length > 0) {
results = results.filter((file) => !exclude.some((pattern) => minimatch(file, pattern)));
}
if (include.length > 0) {
results = results.filter((file) => include.some((pattern) => minimatch(file, pattern)));
}
}
return results;
}, "readDirectory"),
writeFile(fileName, data) {
context.vfs.writeFileSync(fileName, data);
},
resolvePath: /* @__PURE__ */ __name((fileName) => {
if (context.vfs.existsSync(fileName)) {
return context.vfs.resolvePath(fileName);
}
return ts.sys.resolvePath(fileName);
}, "resolvePath"),
getSourceFile(fileName, languageVersionOrOptions, _, shouldCreateNewSourceFile) {
if (context.vfs.existsSync(fileName)) {
return ts.createSourceFile(fileName, context.vfs.readFileSync(fileName), languageVersionOrOptions ?? compilerOptions.target ?? getDefaultCompilerOptions().target, false);
} else if (shouldCreateNewSourceFile) {
const sourceFile = ts.createSourceFile(fileName, "", languageVersionOrOptions ?? compilerOptions.target ?? getDefaultCompilerOptions().target, false);
context.vfs.writeFileSync(fileName, sourceFile.text);
return sourceFile;
}
return void 0;
}
};
return ts.createProgram(rootNames, options, host);
}
__name(createVirtualProgram, "createVirtualProgram");
function createVirtualCompilerHost(sys, compilerOptions) {
const sourceFiles = /* @__PURE__ */ new Map();
const save = /* @__PURE__ */ __name((sourceFile) => {
sourceFiles.set(sourceFile.fileName, sourceFile);
return sourceFile;
}, "save");
const vHost = {
compilerHost: {
...sys,
getCanonicalFileName: /* @__PURE__ */ __name((fileName) => fileName, "getCanonicalFileName"),
getDefaultLibFileName: /* @__PURE__ */ __name(() => `/${ts.getDefaultLibFileName(compilerOptions)}`, "getDefaultLibFileName"),
// getDefaultLibLocation: () => '/',
getNewLine: /* @__PURE__ */ __name(() => sys.newLine, "getNewLine"),
getSourceFile: /* @__PURE__ */ __name((fileName, languageVersionOrOptions) => {
return sourceFiles.get(fileName) ?? save(ts.createSourceFile(fileName, sys.readFile(fileName), languageVersionOrOptions ?? compilerOptions.target, false));
}, "getSourceFile"),
useCaseSensitiveFileNames: /* @__PURE__ */ __name(() => sys.useCaseSensitiveFileNames, "useCaseSensitiveFileNames")
},
updateFile: /* @__PURE__ */ __name((sourceFile) => {
const alreadyExists = sourceFiles.has(sourceFile.fileName);
sys.writeFile(sourceFile.fileName, sourceFile.text);
sourceFiles.set(sourceFile.fileName, sourceFile);
return alreadyExists;
}, "updateFile"),
deleteFile: /* @__PURE__ */ __name((sourceFile) => {
const alreadyExists = sourceFiles.has(sourceFile.fileName);
sourceFiles.delete(sourceFile.fileName);
sys.deleteFile(sourceFile.fileName);
return alreadyExists;
}, "deleteFile")
};
return vHost;
}
__name(createVirtualCompilerHost, "createVirtualCompilerHost");
export { SourcesMap, createVirtualCompilerHost, createVirtualProgram, getDefaultCompilerOptions, loadLibFiles };
//# sourceMappingURL=chunk-E756KYZN.js.map
//# sourceMappingURL=chunk-E756KYZN.js.map