UNPKG

@storm-stack/core

Version:

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

185 lines (182 loc) 6.97 kB
'use strict'; var chunkFWFMIVER_cjs = require('./chunk-FWFMIVER.cjs'); var chunkXX6K67MA_cjs = require('./chunk-XX6K67MA.cjs'); var chunk3LKBNTVT_cjs = require('./chunk-3LKBNTVT.cjs'); var chunkYJTZDESI_cjs = require('./chunk-YJTZDESI.cjs'); var chunkEG72EGDV_cjs = require('./chunk-EG72EGDV.cjs'); var chunkSFV4P2MX_cjs = require('./chunk-SFV4P2MX.cjs'); var chunk3ONWID2V_cjs = require('./chunk-3ONWID2V.cjs'); var types = require('@storm-software/config-tools/types'); var helpers = require('@stryke/fs/helpers'); var exists = require('@stryke/path/exists'); var isParentPath = require('@stryke/path/is-parent-path'); var joinPaths = require('@stryke/path/join-paths'); var resolve = require('@stryke/path/resolve'); var Compiler = class { static { chunk3ONWID2V_cjs.__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 = chunkSFV4P2MX_cjs.extendLog(context.log, "compiler"); this.#options = options; this.cacheDir = joinPaths.joinPaths(context.cachePath, "compiler"); if (!exists.existsSync(this.cacheDir)) { helpers.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(types.LogLevelLabel.TRACE, `Skipping transform for ${fileName}`); return chunkEG72EGDV_cjs.getString(code); } this.log(types.LogLevelLabel.TRACE, `Transforming ${fileName}`); let source = chunkEG72EGDV_cjs.getSourceFile(fileName, code); if (options.onPreTransform) { this.log(types.LogLevelLabel.TRACE, `Running onPreTransform hook for ${source.id}`); source = await Promise.resolve(options.onPreTransform(context, source)); } if (this.#options.onPreTransform) { this.log(types.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(types.LogLevelLabel.TRACE, `Running transforms for ${source.id} with options: ${JSON.stringify(options)}`); source = await chunkYJTZDESI_cjs.transform(this.log, context, source, options); this.log(types.LogLevelLabel.TRACE, `Transformed: ${source.id}`); } if (this.#options.onPostTransform) { this.log(types.LogLevelLabel.TRACE, `Running onPostTransform hook for ${source.id}`); source = await Promise.resolve(this.#options.onPostTransform(context, source)); } if (options.onPostTransform) { this.log(types.LogLevelLabel.TRACE, `Running onPostTransform hook for ${source.id}`); source = await Promise.resolve(options.onPostTransform(context, source)); } return chunkEG72EGDV_cjs.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(types.LogLevelLabel.TRACE, `Transpiling ${id} module with TypeScript compiler`); const transpiled = chunk3LKBNTVT_cjs.transpile(context, id, chunkEG72EGDV_cjs.getString(code), options); if (transpiled === null) { this.log(types.LogLevelLabel.ERROR, `Transform is null: ${id}`); throw new Error(`Transform is null: ${id}`); } else { this.log(types.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(types.LogLevelLabel.TRACE, `Compiling ${id}`); const source = chunkEG72EGDV_cjs.getSourceFile(id, code); let compiled; if (!options.skipCache) { compiled = await this.getCache(context, source); if (compiled) { this.log(types.LogLevelLabel.TRACE, `Cache hit: ${source.id}`); } else { this.log(types.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 chunkXX6K67MA_cjs.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 chunkFWFMIVER_cjs.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 chunkFWFMIVER_cjs.setCache(sourceFile, this.cacheDir, transpiled); } async shouldSkip(context, id, code) { if (!this.#corePath) { this.#corePath = process.env.STORM_STACK_LOCAL ? joinPaths.joinPaths(context.options.workspaceRoot, "packages/core") : await resolve.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.isParentPath(id, this.#corePath) || chunkEG72EGDV_cjs.getString(code).includes("/* @storm-ignore */") || chunkEG72EGDV_cjs.getString(code).includes("/* @storm-disable */")) { return true; } return false; } }; exports.Compiler = Compiler; //# sourceMappingURL=chunk-3IE55BNO.cjs.map //# sourceMappingURL=chunk-3IE55BNO.cjs.map