UNPKG

@storm-stack/core

Version:

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

1 lines 7.07 kB
{"version":3,"sources":["../../src/lib/deepkit/reflect-type.ts"],"names":["resolveType","context","entry","options","path","resolvePath","file","vfs","existsSync","Error","result","bundle","defu","override","write","sourcemap","splitting","treeShaking","compiler","reflectionLevel","errors","length","map","error","text","join","warnings","log","warning","outputFiles","resolvedTypes","Promise","all","outputFile","resolved","resolver","evalModule","filename","forceTranspile","exportName","name","filter","Boolean","reflectType","reflect"],"mappings":";;;;;;;;;;;AAiCA,eAAsBA,WAAAA,CACpBC,OAAAA,EACAC,KAAAA,EACAC,OAAAA,GAAyB,EAAC,EAAC;AAE3B,EAAA,MAAMC,IAAAA,GAAO,MAAMC,6BAAAA,CAAYJ,OAAAA,EAASC,MAAMI,IAAI,CAAA;AAClD,EAAA,IAAI,CAACF,IAAAA,IAAQ,CAACH,QAAQM,GAAAA,CAAIC,UAAAA,CAAWJ,IAAAA,CAAAA,EAAO;AAC1C,IAAA,MAAM,IAAIK,KAAAA,CACR,CAAA,mBAAA,EAAsBP,KAAAA,CAAMI,IAAI,CAAA,uCAAA,CAAyC,CAAA;AAE7E,EAAA;AAEA,EAAA,MAAMI,MAAAA,GAAS,MAAMC,wBAAAA,CACnBV,OAAAA,EACAW,qBAAAA,CACE;IACEV,KAAAA,EAAO;AAACE,MAAAA;;IACRS,QAAAA,EAAU;MACRC,KAAAA,EAAO,KAAA;MACPC,SAAAA,EAAW,KAAA;MACXC,SAAAA,EAAW,KAAA;MACXC,WAAAA,EAAa;AACf,KAAA;IACAC,QAAAA,EAAU;MACRC,eAAAA,EAAiB;AACnB;AACF,GAAA,EACAhB,OAAAA,CAAAA,CAAAA;AAGJ,EAAA,IAAIO,MAAAA,CAAOU,MAAAA,CAAOC,MAAAA,GAAS,CAAA,EAAG;AAC5B,IAAA,MAAM,IAAIZ,KAAAA,CACR,CAAA,oBAAA,EAAuBP,KAAAA,CAAMI,IAAI,KAAKI,MAAAA,CAA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EAAA,CAAA,CAAA,CAAA;AAGF,EAAA,MAAML,QAAAA,GAAWJ,aAAAA,CAAcW,MAAAA,CAAOC,OAAAA,EAAS,CAAA,CAAA;AAC/C,EAAA,IAAI,CAACR,QAAAA,EAAU;AACb,IAAA,MAAM,IAAIzB,KAAAA,CACR,CAAA,mCAAA,EAAsCP,KAAAA,CAAMI,IAAI,CAAA,EAC9CJ,KAAAA,CAAMsC,IAAAA,GAAO,CAAA,CAAA,EAAItC,KAAAA,CAAMsC,IAAI,CAAA,CAAA,GAAK,EAAA,CAAA,sCAAA,CACM,CAAA;AAE5C,EAAA;AAEA,EAAA,OAAON,QAAAA;AACT;AA7EsBlC,wBAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA;AAuFtB,eAAsB2C,WAAAA,CACpB1C,OAAAA,EACAC,KAAAA,EACAC,OAAAA,GAAyB,EAAC,EAAC;AAE3B,EAAA,OAAOyC,SAAQ,MAAM5C,WAAAA,CAAYC,OAAAA,EAASC,KAAAA,EAAOC,OAAAA,CAAAA,CAAAA;AACnD;AANsBwC,wBAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA","file":"chunk-LZ6FYLJ2.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 type { Type } from \"@deepkit/type\";\nimport { reflect } from \"@deepkit/type\";\nimport type { TypeDefinition } from \"@stryke/types/configuration\";\nimport defu from \"defu\";\nimport type { Context } from \"../../types/context\";\nimport { bundle, BundleOptions } from \"../esbuild/bundle\";\nimport { resolvePath } from \"../utilities/resolve-path\";\n\n/**\n * Compiles a type definition to a module.\n *\n * @param context - The context object containing the environment paths.\n * @param entry - The type definition to compile.\n * @returns A promise that resolves to the compiled module.\n */\nexport async function resolveType<TResult = any>(\n context: Context,\n entry: TypeDefinition,\n options: BundleOptions = {}\n): Promise<TResult> {\n const path = await resolvePath(context, entry.file);\n if (!path || !context.vfs.existsSync(path)) {\n throw new Error(\n `Module not found: \"${entry.file}\". Please check the path and try again.`\n );\n }\n\n const result = await bundle(\n context,\n defu(\n {\n entry: [path],\n override: {\n write: false,\n sourcemap: false,\n splitting: false,\n treeShaking: false\n },\n compiler: {\n reflectionLevel: \"verbose\"\n }\n },\n options\n ) as BundleOptions\n );\n if (result.errors.length > 0) {\n throw new Error(\n `Failed to transpile ${entry.file}: ${result.errors\n .map(error => error.text)\n .join(\", \")}`\n );\n }\n if (result.warnings.length > 0) {\n context.log(\n \"warn\",\n `Warnings while transpiling ${entry.file}: ${result.warnings\n .map(warning => warning.text)\n .join(\", \")}`\n );\n }\n if (!result.outputFiles || result.outputFiles.length === 0) {\n throw new Error(\n `No output files generated for ${entry.file}. Please check the configuration and try again.`\n );\n }\n\n const resolvedTypes = await Promise.all(\n result.outputFiles.map(async outputFile => {\n const resolved = (await context.resolver.evalModule(outputFile.text, {\n filename: outputFile.path,\n forceTranspile: true\n })) as Record<string, any>;\n\n let exportName = entry.name;\n if (!exportName) {\n exportName = \"default\";\n }\n\n return resolved[exportName] ?? resolved[`__Ω${exportName}`];\n })\n );\n\n const resolved = resolvedTypes.filter(Boolean)[0] as TResult;\n if (!resolved) {\n throw new Error(\n `No valid reflection types found in ${entry.file}${\n entry.name ? `#${entry.name}` : \"\"\n }. Please check the file and try again.`\n );\n }\n\n return resolved;\n}\n\n/**\n * Compiles a type definition to a module.\n *\n * @param context - The context object containing the environment paths.\n * @param entry - The type definition to compile.\n * @param options - The options for resolving the type.\n * @returns A promise that resolves to the compiled module.\n */\nexport async function reflectType(\n context: Context,\n entry: TypeDefinition,\n options: BundleOptions = {}\n): Promise<Type> {\n return reflect(await resolveType(context, entry, options));\n}\n"]}