UNPKG

@storm-stack/core

Version:

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

98 lines (95 loc) 3.65 kB
'use strict'; var chunkSQHGN4CZ_cjs = require('./chunk-SQHGN4CZ.cjs'); var chunkQH6JOSVV_cjs = require('./chunk-QH6JOSVV.cjs'); var chunkEND3JFY3_cjs = require('./chunk-END3JFY3.cjs'); var chunk3ONWID2V_cjs = require('./chunk-3ONWID2V.cjs'); var types = require('@storm-software/config-tools/types'); var helpers = require('@stryke/fs/helpers'); var throttle = require('@stryke/helpers/throttle'); var stormJson = require('@stryke/json/storm-json'); var index = require('@stryke/path/index'); var isSetString = require('@stryke/type-checks/is-set-string'); var unimport = require('unimport'); var lastImportsDump; var DEFAULT_UNIMPORT_CONFIG = { commentsDisable: [ "@unimport-disable", "@imports-disable", "@storm-disable", "@storm-ignore" ], commentsDebug: [ "@unimport-debug", "@imports-debug", "@storm-debug" ], injectAtEnd: true }; function createUnimport(context) { context.log(types.LogLevelLabel.TRACE, "Creating Unimport context with Storm Stack presets"); let unimport$1 = unimport.createUnimport({ ...DEFAULT_UNIMPORT_CONFIG, presets: [] }); async function refreshRuntimeImports() { const presets = []; for (const id of context.vfs.runtimeIdMap.keys()) { const contents = await context.vfs.readFile(id); if (contents) { context.log(types.LogLevelLabel.TRACE, `Processing exports from runtime file: ${id}`); const importNames = chunkSQHGN4CZ_cjs.listExports(chunkQH6JOSVV_cjs.parseAst(contents)).filter((importName) => !presets.some((preset) => preset?.imports && !preset?.imports.some((presetImport) => isSetString.isSetString(presetImport) && presetImport === importName || Array.isArray(presetImport) && presetImport[0] === importName))); if (importNames.length > 0) { presets.push({ imports: importNames, from: id }); } } } unimport$1 = unimport.createUnimport({ ...DEFAULT_UNIMPORT_CONFIG, presets, virtualImports: Array.from(context.vfs.runtimeIdMap.keys()) }); await unimport$1.init(); } chunk3ONWID2V_cjs.__name(refreshRuntimeImports, "refreshRuntimeImports"); async function dumpImports() { context.log(types.LogLevelLabel.TRACE, "Dumping import file..."); const items = await unimport$1.getImports(); const importDumpFile = index.joinPaths(context.dataPath, "imports-dump.json"); if (!index.existsSync(index.findFilePath(importDumpFile))) { await helpers.createDirectory(index.findFilePath(importDumpFile)); } context.log(types.LogLevelLabel.TRACE, `Writing imports-dump JSON file: ${importDumpFile}`); const content = stormJson.StormJSON.stringify(items); if (content.trim() !== lastImportsDump?.trim()) { lastImportsDump = content; await chunkEND3JFY3_cjs.writeFile(context.log, importDumpFile, content); } } chunk3ONWID2V_cjs.__name(dumpImports, "dumpImports"); const dumpImportsThrottled = throttle.throttle(dumpImports, 500); async function injectImports(source) { const result = await unimport$1.injectImports(source.code, source.id); if (!source.code.hasChanged()) { return source; } await dumpImportsThrottled(); return { ...source, code: result.s }; } chunk3ONWID2V_cjs.__name(injectImports, "injectImports"); return { ...unimport$1, dumpImports: dumpImportsThrottled, injectImports, refreshRuntimeImports }; } chunk3ONWID2V_cjs.__name(createUnimport, "createUnimport"); exports.createUnimport = createUnimport; //# sourceMappingURL=chunk-CV2HBLLG.cjs.map //# sourceMappingURL=chunk-CV2HBLLG.cjs.map