UNPKG

@storm-stack/core

Version:

A build toolkit and runtime used by Storm Software in TypeScript applications

183 lines (181 loc) 6.56 kB
import { getCache, setCache } from './chunk-NNKV7APZ.js'; import { generateSourceMap } from './chunk-E3IP23TH.js'; import { transpile } from './chunk-6MI3B55M.js'; import { transform } from './chunk-DNI4FJXL.js'; import { getString, getSourceFile } from './chunk-K4ZS2URJ.js'; import { extendLog } from './chunk-RDCOIWVB.js'; import { __name } from './chunk-43IZMM3W.js'; import { LogLevelLabel } from '@storm-software/config-tools/types'; import { createDirectorySync } from '@stryke/fs/helpers'; import { existsSync } from '@stryke/path/exists'; import { isParentPath } from '@stryke/path/is-parent-path'; import { joinPaths } from '@stryke/path/join-paths'; import { resolvePackage } from '@stryke/path/resolve'; var Compiler = class { static { __name(this, "Compiler"); } #cache = /* @__PURE__ */ new WeakMap(); #options; #corePath; /** * The logger function to use */ log; /** * The cache directory */ cacheDir; /** * Create a new compiler instance. * * @param context - The compiler context. * @param options - The compiler options. */ constructor(context, options = {}) { this.log = extendLog(context.log, "compiler"); this.#options = options; this.cacheDir = joinPaths(context.cachePath, "compiler"); if (!existsSync(this.cacheDir)) { createDirectorySync(this.cacheDir); } } /** * Transform the module. * * @param context - The compiler context. * @param fileName - The name of the file to compile. * @param code - The source code to compile. * @param options - The transpile options. * @returns The transpiled module. */ async transform(context, fileName, code, options = {}) { if (await this.shouldSkip(context, fileName, code)) { this.log(LogLevelLabel.TRACE, `Skipping transform for ${fileName}`); return getString(code); } this.log(LogLevelLabel.TRACE, `Transforming ${fileName}`); let source = getSourceFile(fileName, code); if (options.onPreTransform) { this.log(LogLevelLabel.TRACE, `Running onPreTransform hook for ${source.id}`); source = await Promise.resolve(options.onPreTransform(context, source)); } if (this.#options.onPreTransform) { this.log(LogLevelLabel.TRACE, `Running onPreTransform hook for ${source.id}`); source = await Promise.resolve(this.#options.onPreTransform(context, source)); } if (!options.skipAllTransforms) { if (context.unimport && !options.skipTransformUnimport && !context.vfs.isRuntimeFile(fileName)) { source = await context.unimport.injectImports(source); } this.log(LogLevelLabel.TRACE, `Running transforms for ${source.id} with options: ${JSON.stringify(options)}`); source = await transform(this.log, context, source, options); this.log(LogLevelLabel.TRACE, `Transformed: ${source.id}`); } if (this.#options.onPostTransform) { this.log(LogLevelLabel.TRACE, `Running onPostTransform hook for ${source.id}`); source = await Promise.resolve(this.#options.onPostTransform(context, source)); } if (options.onPostTransform) { this.log(LogLevelLabel.TRACE, `Running onPostTransform hook for ${source.id}`); source = await Promise.resolve(options.onPostTransform(context, source)); } return getString(source.code); } /** * Transpile the module. * * @param context - The compiler context. * @param id - The name of the file to compile. * @param code - The source code to compile. * @returns The transpiled module. */ async transpile(context, id, code, options = {}) { this.log(LogLevelLabel.TRACE, `Transpiling ${id} module with TypeScript compiler`); const transpiled = transpile(context, id, getString(code), options); if (transpiled === null) { this.log(LogLevelLabel.ERROR, `Transform is null: ${id}`); throw new Error(`Transform is null: ${id}`); } else { this.log(LogLevelLabel.TRACE, `Transformed: ${id}`); } return transpiled.outputText; } /** * Compile the source code. * * @param context - The compiler context. * @param id - The name of the file to compile. * @param code - The source code to compile. * @returns The compiled source code and source map. */ async compile(context, id, code, options = {}) { this.log(LogLevelLabel.TRACE, `Compiling ${id}`); const source = getSourceFile(id, code); let compiled; if (!options.skipCache) { compiled = await this.getCache(context, source); if (compiled) { this.log(LogLevelLabel.TRACE, `Cache hit: ${source.id}`); } else { this.log(LogLevelLabel.TRACE, `Cache miss: ${source.id}`); } } if (!compiled) { const transformed = await this.transform(context, source.id, source.code, options); compiled = await this.transpile(context, id, transformed, options); await this.setCache(context, source, compiled); } return compiled; } /** * Get the result of the compiler. * * @param sourceFile - The source file. * @param transpiled - The transpiled source code. * @returns The result of the compiler. */ getResult(sourceFile, transpiled) { return generateSourceMap(sourceFile.id, sourceFile.code, transpiled); } async getCache(context, sourceFile) { let cache = this.#cache.get(sourceFile); if (cache) { return cache; } if (context.options.skipCache) { return; } cache = await getCache(sourceFile, this.cacheDir); if (cache) { this.#cache.set(sourceFile, cache); } return cache; } async setCache(context, sourceFile, transpiled) { if (transpiled) { this.#cache.set(sourceFile, transpiled); } else { this.#cache.delete(sourceFile); } if (context.options.skipCache) { return; } return setCache(sourceFile, this.cacheDir, transpiled); } async shouldSkip(context, id, code) { if (!this.#corePath) { this.#corePath = process.env.STORM_STACK_LOCAL ? joinPaths(context.options.workspaceRoot, "packages/core") : await resolvePackage("@storm-stack/core"); if (!this.#corePath) { throw new Error("Could not resolve @storm-stack/core package location."); } } if (process.env.STORM_STACK_LOCAL && isParentPath(id, this.#corePath) || getString(code).includes("/* @storm-ignore */") || getString(code).includes("/* @storm-disable */")) { return true; } return false; } }; export { Compiler }; //# sourceMappingURL=chunk-V2KW5JFJ.js.map //# sourceMappingURL=chunk-V2KW5JFJ.js.map