@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
1 lines • 21.9 kB
Source Map (JSON)
{"version":3,"sources":["../../src/lib/typescript/program.ts"],"names":["SourcesMap","Map","loadLibFiles","libLocation","resolvePackage","Error","libFiles","listFiles","joinPaths","length","lib","file","set","findFileName","readFile","getDefaultCompilerOptions","ts","jsx","JsxEmit","React","strict","esModuleInterop","module","ModuleKind","ESNext","suppressOutputPathCheck","skipLibCheck","skipDefaultLibCheck","moduleResolution","ModuleResolutionKind","Node10","createVirtualProgram","rootNames","context","compilerOptions","options","defu","host","name","root","workspaceRoot","sys","realpath","path","vfs","existsSync","resolvePath","getCurrentDirectory","getCanonicalFileName","fileName","getDefaultLibFileName","_options","getDefaultLibLocation","getNewLine","useCaseSensitiveFileNames","fileExists","readFileSync","undefined","readDirectory","__name","extensions","exclude","include","results","readdirSync","encoding","recursive","filter","some","ext","endsWith","startsWith","pattern","minimatch","writeFile","data","writeFileSync","getSourceFile","languageVersionOrOptions","_","shouldCreateNewSourceFile","createSourceFile","target","sourceFile","text","createProgram","createVirtualCompilerHost","sourceFiles","save","vHost","compilerHost","newLine","get","updateFile","alreadyExists","has","deleteFile","delete"],"mappings":";;;;;;;;;;;;;;;;;AA4BO,IAAMA,UAAAA,GAAaC;AAI1B,eAAsBC,YAAAA,GAAAA;AACpB,EAAA,MAAMC,WAAAA,GAAc,MAAMC,sBAAAA,CAAe,YAAA,CAAA;AACzC,EAAA,IAAI,CAACD,WAAAA,EAAa;AAChB,IAAA,MAAM,IAAIE,MAAM,gDAAA,CAAA;AAClB,EAAA;AAEA,EAAA,MAAMC,QAAAA,GAAW,MAAMC,mBAAAA,CACrBC,mBAAAA,CAAUL,aAAa,KAAA,EAAO,IAAA,EAAM,YAAA,CAAA,CAAA;AAEtC,EAAA,IAAIG,QAAAA,CAASG,WAAW,CAAA,EAAG;AACzB,IAAA,MAAM,IAAIJ,MAAM,oCAAA,CAAA;AAClB,EAAA;AAEA,EAAA,MAAMK,GA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wBAAA,CAAA,MAAMlB,IAAIa,yBAAAA,EAAV,2BAAA;AAC7B,KAAA;AACAuC,IAAAA,UAAAA,4CAAYV,UAAAA,KAAAA;AACV,MAAA,MAAMW,aAAAA,GAAgBP,WAAAA,CAAYQ,GAAAA,CAAIZ,UAAAA,CAAWlC,QAAQ,CAAA;AACzDR,MAAAA,GAAAA,CAAIiC,SAAAA,CAAUS,UAAAA,CAAWlC,QAAAA,EAAUkC,UAAAA,CAAWC,IAAI,CAAA;AAClDG,MAAAA,WAAAA,CAAY3E,GAAAA,CAAIuE,UAAAA,CAAWlC,QAAAA,EAAUkC,UAAAA,CAAAA;AACrC,MAAA,OAAOW,aAAAA;IACT,CAAA,EALYX,YAAAA,CAAAA;AAMZa,IAAAA,UAAAA,4CAAYb,UAAAA,KAAAA;AACV,MAAA,MAAMW,aAAAA,GAAgBP,WAAAA,CAAYQ,GAAAA,CAAIZ,UAAAA,CAAWlC,QAAQ,CAAA;AACzDsC,MAAAA,WAAAA,CAAYU,MAAAA,CAAOd,WAAWlC,QAAQ,CAAA;AACtCR,MAAAA,GAAAA,CAAIuD,UAAAA,CAAYb,WAAWlC,QAAQ,CAAA;AACnC,MAAA,OAAO6C,aAAAA;IACT,CAAA,EALYX,YAAAA;AAMd,GAAA;AAEA,EAAA,OAAOM,KAAAA;AACT;AAhDgBH,wBAAAA,CAAAA,yBAAAA,EAAAA,2BAAAA,CAAAA","file":"chunk-W44YRSYF.cjs","sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Storm Stack\n\n This code was released as part of the Storm Stack project. Storm Stack\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/storm-stack.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/storm-stack\n Documentation: https://docs.stormsoftware.com/projects/storm-stack\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { listFiles } from \"@stryke/fs/list-files\";\nimport { readFile } from \"@stryke/fs/read-file\";\nimport { findFileName } from \"@stryke/path/file-path-fns\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport { resolvePackage } from \"@stryke/path/resolve\";\nimport defu from \"defu\";\nimport { minimatch } from \"minimatch\";\nimport ts, { CompilerOptions } from \"typescript\";\nimport { Context } from \"../../types/context\";\n\nexport const SourcesMap = Map<string, string>;\n// eslint-disable-next-line ts/no-redeclare\nexport type SourcesMap = InstanceType<typeof SourcesMap>;\n\nexport async function loadLibFiles(): Promise<SourcesMap> {\n const libLocation = await resolvePackage(\"typescript\");\n if (!libLocation) {\n throw new Error(\"Could not resolve TypeScript package location.\");\n }\n\n const libFiles = await listFiles(\n joinPaths(libLocation, \"lib\", \"**\", \"lib.*.d.ts\")\n );\n if (libFiles.length === 0) {\n throw new Error(\"No TypeScript library files found.\");\n }\n\n const lib: SourcesMap = new Map();\n for (const file of libFiles) {\n lib.set(\n `/node_modules/typescript/lib/${findFileName(file)}`,\n await readFile(file)\n );\n }\n\n return lib;\n}\n\n/** The default compiler options if TypeScript could ever change the compiler options */\nexport function getDefaultCompilerOptions(): CompilerOptions {\n return {\n ...ts.getDefaultCompilerOptions(),\n jsx: ts.JsxEmit.React,\n strict: true,\n esModuleInterop: true,\n module: ts.ModuleKind.ESNext,\n suppressOutputPathCheck: true,\n skipLibCheck: true,\n skipDefaultLibCheck: true,\n moduleResolution: ts.ModuleResolutionKind.Node10\n };\n}\n\n/**\n * Creates a TypeScript program from in-memory, virtual source files.\n *\n * @param rootNames - An array of root file names to include in the program.\n * @param context - The context containing options and environment paths.\n * @param compilerOptions - Optional TypeScript compiler options. Defaults to the standard TypeScript options\n * @returns A TypeScript program instance that can be used for type checking, emitting, etc.\n * @throws If the provided library files are not in the expected format or if the TypeScript package cannot be resolved.\n */\nexport async function createVirtualProgram(\n rootNames: readonly string[],\n context: Context,\n compilerOptions: ts.CompilerOptions = {}\n): Promise<ts.Program> {\n const options = defu(compilerOptions, getDefaultCompilerOptions());\n\n const host = {\n name: \"storm-vfs\",\n root: context.options.workspaceRoot,\n ...ts.sys,\n realpath: (path: string) => {\n if (context.vfs.existsSync(path)) {\n return context.vfs.resolvePath(path);\n }\n\n return ts.sys.realpath?.(path) ?? path;\n },\n getCurrentDirectory(): string {\n return context.options.workspaceRoot;\n },\n getCanonicalFileName(fileName: string): string {\n return fileName;\n },\n getDefaultLibFileName(_options: ts.CompilerOptions): string {\n return ts.getDefaultLibFileName(options);\n },\n getDefaultLibLocation(): string {\n return \"/\";\n },\n getNewLine(): string {\n return \"\\n\";\n },\n useCaseSensitiveFileNames(): boolean {\n return true;\n },\n fileExists(fileName: string): boolean {\n return context.vfs.existsSync(fileName);\n },\n readFile(fileName: string): string | undefined {\n if (context.vfs.existsSync(fileName)) {\n return context.vfs.readFileSync(fileName);\n }\n\n return undefined;\n },\n readDirectory: (\n path: string,\n extensions: readonly string[] = [],\n exclude: readonly string[] = [],\n include: readonly string[] = []\n ) => {\n let results = [] as string[];\n if (context.vfs.existsSync(path)) {\n results = context.vfs.readdirSync(path, {\n encoding: \"utf8\",\n recursive: true\n });\n\n if (extensions.length > 0) {\n results = results.filter(file =>\n extensions.some(ext =>\n file.endsWith(ext.startsWith(\".\") ? ext : `.${ext}`)\n )\n );\n }\n\n if (exclude.length > 0) {\n results = results.filter(\n file => !exclude.some(pattern => minimatch(file, pattern))\n );\n }\n\n if (include.length > 0) {\n results = results.filter(file =>\n include.some(pattern => minimatch(file, pattern))\n );\n }\n }\n\n return results;\n },\n writeFile(fileName: string, data: string): void {\n context.vfs.writeFileSync(fileName, data);\n },\n resolvePath: fileName => {\n if (context.vfs.existsSync(fileName)) {\n return context.vfs.resolvePath(fileName);\n }\n\n return ts.sys.resolvePath(fileName);\n },\n getSourceFile(\n fileName: string,\n languageVersionOrOptions: ts.ScriptTarget | ts.CreateSourceFileOptions,\n _?: (message: string) => void,\n shouldCreateNewSourceFile?: boolean\n ): ts.SourceFile | undefined {\n if (context.vfs.existsSync(fileName)) {\n return ts.createSourceFile(\n fileName,\n context.vfs.readFileSync(fileName)!,\n languageVersionOrOptions ??\n compilerOptions.target ??\n getDefaultCompilerOptions().target!,\n false\n );\n } else if (shouldCreateNewSourceFile) {\n const sourceFile = ts.createSourceFile(\n fileName,\n \"\",\n languageVersionOrOptions ??\n compilerOptions.target ??\n getDefaultCompilerOptions().target!,\n false\n );\n context.vfs.writeFileSync(fileName, sourceFile.text);\n return sourceFile;\n }\n\n return undefined;\n }\n } as ts.CompilerHost;\n\n // const host = {\n // ...ts.sys,\n // getCanonicalFileName: fileName => fileName,\n // getDefaultLibFileName: () =>\n // `/${ts.getDefaultLibFileName(compilerOptions)}`,\n // // getDefaultLibLocation: () => '/',\n // getNewLine: () => ts.sys.newLine,\n // getSourceFile: (fileName, languageVersionOrOptions) => {\n // if (context.vfs.existsSync(fileName)) {\n // return ts.createSourceFile(\n // fileName,\n // context.vfs.readFileSync(fileName)!,\n // languageVersionOrOptions ??\n // compilerOptions.target ??\n // getDefaultCompilerOptions().target!,\n // false\n // );\n // }\n\n // const sourceFile = ts.createSourceFile(\n // fileName,\n // ts.sys.readFile(fileName)!,\n // languageVersionOrOptions ??\n // compilerOptions.target ??\n // getDefaultCompilerOptions().target!,\n // false\n // );\n // context.vfs.writeFileSync(fileName, sourceFile.text);\n\n // return sourceFile;\n // },\n // useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames\n // };\n\n return ts.createProgram(rootNames, options, host);\n}\n\n/**\n * Creates a file-system backed System object which can be used in a TypeScript program, you provide\n * a set of virtual files which are prioritized over the FS versions, then a path to the root of your\n * project (basically the folder your node_modules lives)\n */\n// export async function createVirtualSystem(\n// context: Context\n// ): Promise<ts.System> {\n// const projectRoot = joinPaths(\n// context.options.workspaceRoot,\n// context.options.projectRoot\n// );\n\n// // We need to make an isolated folder for the tsconfig, but also need to be able to resolve the\n// // existing node_modules structures going back through the history\n// const root = joinPaths(projectRoot, \"vfs\");\n\n// // The default System in TypeScript\n// const tsLib = await resolvePackage(\"typescript\", {\n// paths: [context.options.workspaceRoot, projectRoot, root]\n// });\n// if (!tsLib) {\n// throw new Error(\"Could not resolve TypeScript package location.\");\n// }\n\n// return {\n// // @ts-ignore\n// name: \"storm-vfs\",\n// root,\n// args: [],\n// createDirectory: () => {\n// throw new Error(\"createDirectory not implemented\");\n// },\n// directoryExists: (path: string) => {\n// return (\n// context.vfs.values().some(file => file.path.startsWith(path)) ||\n// ts.sys.directoryExists(path)\n// );\n// },\n// exit: () => ts.sys.exit(),\n// fileExists: (path: string) => {\n// if (context.vfs.has(path)) {\n// return true;\n// }\n\n// // Don't let other tsconfigs end up touching the vfs\n// if (path.includes(\"tsconfig.json\")) {\n// return false;\n// }\n\n// if (path.startsWith(\"/lib\")) {\n// const tsLibName = `${tsLib}/${path.replace(\"/\", \"\")}`;\n\n// return ts.sys.fileExists(tsLibName);\n// }\n\n// return ts.sys.fileExists(path);\n// },\n// getCurrentDirectory: () => root,\n// getDirectories: (path: string) => ts.sys.getDirectories(path),\n// getExecutingFilePath: () => {\n// throw new Error(\"getExecutingFilePath not implemented.\");\n// },\n// readDirectory: (\n// path: string,\n// extensions?: readonly string[],\n// exclude?: readonly string[],\n// include?: readonly string[],\n// depth?: number\n// ) => {\n// return ts.sys\n// .readDirectory(path, extensions, exclude, include, depth)\n// .reduce((ret, file) => {\n// if (!ret.includes(file)) {\n// ret.push(file);\n// }\n// return ret;\n// }, context.vfs.readDirectory(path));\n// },\n// readFile: (path: string) => {\n// const virtualFile = context.vfs.readFile(path);\n// if (virtualFile) {\n// return virtualFile.contents?.toString();\n// }\n\n// if (path.startsWith(\"/lib\")) {\n// const tsLibName = `${tsLib}/${path.replace(\"/\", \"\")}`;\n// const result = ts.sys.readFile(tsLibName);\n// if (!result) {\n// const libs = ts.sys.readDirectory(tsLib);\n// throw new Error(\n// `A request was made for ${tsLibName} but there wasn't a file found in the file map. You likely have a mismatch in the compiler options for the CDN download vs the compiler program. Existing Libs: ${libs.join(\", \")}.`\n// );\n// }\n\n// return result;\n// }\n\n// return ts.sys.readFile(path);\n// },\n// resolvePath: (path: string) => {\n// if (context.vfs.has(path)) {\n// return path;\n// }\n\n// return ts.sys.resolvePath(path);\n// },\n// newLine: \"\\n\",\n// useCaseSensitiveFileNames: true,\n// write: () => {\n// throw new Error(\"write not implemented\");\n// },\n// writeFile: (fileName, contents) => {\n// void context.vfs.writeFile(fileName, contents);\n// },\n// deleteFile: fileName => {\n// context.vfs.deleteFile(fileName);\n// },\n// realpath: (path: string) => String(ts.sys.realpath?.(path))\n// };\n// }\n\ninterface VirtualCompilerHostReturn {\n compilerHost: ts.CompilerHost;\n updateFile: (sourceFile: ts.SourceFile) => boolean;\n deleteFile: (sourceFile: ts.SourceFile) => boolean;\n}\n\n/**\n * Creates an in-memory CompilerHost -which is essentially an extra wrapper to System\n * which works with TypeScript objects - returns both a compiler host, and a way to add new SourceFile\n * instances to the in-memory file system.\n */\nexport function createVirtualCompilerHost(\n sys: ts.System,\n compilerOptions: ts.CompilerOptions\n) {\n const sourceFiles = new Map<string, ts.SourceFile>();\n const save = (sourceFile: ts.SourceFile) => {\n sourceFiles.set(sourceFile.fileName, sourceFile);\n return sourceFile;\n };\n\n const vHost: VirtualCompilerHostReturn = {\n compilerHost: {\n ...sys,\n getCanonicalFileName: fileName => fileName,\n getDefaultLibFileName: () =>\n `/${ts.getDefaultLibFileName(compilerOptions)}`, // '/lib.d.ts',\n // getDefaultLibLocation: () => '/',\n getNewLine: () => sys.newLine,\n getSourceFile: (fileName, languageVersionOrOptions) => {\n return (\n sourceFiles.get(fileName) ??\n save(\n ts.createSourceFile(\n fileName,\n sys.readFile(fileName)!,\n languageVersionOrOptions ?? compilerOptions.target,\n false\n )\n )\n );\n },\n useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames\n },\n updateFile: sourceFile => {\n const alreadyExists = sourceFiles.has(sourceFile.fileName);\n sys.writeFile(sourceFile.fileName, sourceFile.text);\n sourceFiles.set(sourceFile.fileName, sourceFile);\n return alreadyExists;\n },\n deleteFile: sourceFile => {\n const alreadyExists = sourceFiles.has(sourceFile.fileName);\n sourceFiles.delete(sourceFile.fileName);\n sys.deleteFile!(sourceFile.fileName);\n return alreadyExists;\n }\n };\n\n return vHost;\n}\n"]}