UNPKG

@storm-stack/core

Version:

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

99 lines (95 loc) 3.69 kB
'use strict'; var chunkJ6QQT263_cjs = require('./chunk-J6QQT263.cjs'); var chunkPWXX6DQI_cjs = require('./chunk-PWXX6DQI.cjs'); var chunkK5FGUCZ3_cjs = require('./chunk-K5FGUCZ3.cjs'); var chunkGRNJVY7I_cjs = require('./chunk-GRNJVY7I.cjs'); var types = require('@storm-software/config-tools/types'); var exists = require('@stryke/fs/exists'); var helpers = require('@stryke/fs/helpers'); var throttle = require('@stryke/helpers/throttle'); var stormJson = require('@stryke/json/storm-json'); var filePathFns = require('@stryke/path/file-path-fns'); var joinPaths = require('@stryke/path/join-paths'); 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 = chunkPWXX6DQI_cjs.listExports(chunkK5FGUCZ3_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(); } chunkGRNJVY7I_cjs.__name(refreshRuntimeImports, "refreshRuntimeImports"); async function dumpImports() { context.log(types.LogLevelLabel.TRACE, "Dumping import file..."); const items = await unimport$1.getImports(); const importDumpFile = joinPaths.joinPaths(context.dataPath, "imports-dump.json"); if (!exists.existsSync(filePathFns.findFilePath(importDumpFile))) { await helpers.createDirectory(filePathFns.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 chunkJ6QQT263_cjs.writeFile(context.log, importDumpFile, content); } } chunkGRNJVY7I_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 }; } chunkGRNJVY7I_cjs.__name(injectImports, "injectImports"); return { ...unimport$1, dumpImports: dumpImportsThrottled, injectImports, refreshRuntimeImports }; } chunkGRNJVY7I_cjs.__name(createUnimport, "createUnimport"); exports.createUnimport = createUnimport;