UNPKG

@storm-stack/core

Version:

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

177 lines (175 loc) 6.33 kB
import { getTsconfigFilePath } from './chunk-ONS37BLK.js'; import { __name } from './chunk-43IZMM3W.js'; import { getWorkspaceConfig } from '@storm-software/config-tools/get-config'; import { getColors } from '@storm-software/config-tools/utilities/colors'; import { existsSync } from '@stryke/path/exists'; import { getProjectRoot, getWorkspaceRoot } from '@stryke/path/get-workspace-root'; import { joinPaths } from '@stryke/path/join-paths'; import { isFunction } from '@stryke/type-checks/is-function'; import { isSetObject } from '@stryke/type-checks/is-set-object'; import { loadConfig } from 'c12'; import defu from 'defu'; async function loadUserConfigFile(projectRoot, jiti, command, mode) { let resolvedUserConfig = {}; const resolvedUserConfigFile = existsSync(joinPaths(projectRoot, "storm.config.ts")) ? joinPaths(projectRoot, "storm.config.ts") : existsSync(joinPaths(projectRoot, "storm.config.js")) ? joinPaths(projectRoot, "storm.config.js") : existsSync(joinPaths(projectRoot, "storm.config.mts")) ? joinPaths(projectRoot, "storm.config.mts") : existsSync(joinPaths(projectRoot, "storm.config.mjs")) ? joinPaths(projectRoot, "storm.config.mjs") : void 0; if (resolvedUserConfigFile) { const resolved = await jiti.import(jiti.esmResolve(resolvedUserConfigFile)); if (resolved) { let config = {}; if (isFunction(resolved)) { config = await Promise.resolve(resolved({ command, mode: mode || "production", isSsrBuild: false, isPreview: false })); } if (isSetObject(config)) { resolvedUserConfig = { ...config, config, configFile: resolvedUserConfigFile }; } } } const result = await Promise.all([ loadConfig({ cwd: projectRoot, name: "storm", envName: mode, globalRc: true, packageJson: true, dotenv: true, jiti }), loadConfig({ cwd: projectRoot, name: "storm.config", rcFile: false, globalRc: false, packageJson: false, dotenv: false, jiti }), loadConfig({ cwd: projectRoot, name: "storm-stack", envName: mode, globalRc: true, packageJson: "stormStack", jiti }) ]); return defu(resolvedUserConfig, isSetObject(result[0]?.config) ? { ...result[0].config, ...result[0] } : {}, isSetObject(result[1]?.config) ? { ...result[1].config, ...result[1] } : {}, isSetObject(result[2]?.config) ? { ...result[2].config, ...result[2] } : {}); } __name(loadUserConfigFile, "loadUserConfigFile"); async function resolveConfig(context, inlineConfig, userConfig, projectRoot) { const resolvedProjectRoot = projectRoot || inlineConfig.root || userConfig?.root || getProjectRoot() || process.cwd(); const workspaceRoot = context.options.workspaceConfig?.workspaceRoot ?? getWorkspaceRoot(); if (!context.options.workspaceConfig) { const workspaceConfig2 = await getWorkspaceConfig(); context.options.workspaceConfig = defu(workspaceConfig2, { workspaceRoot }); } if (!context.options.workspaceConfig?.workspaceRoot) { throw new Error("The workspace root could not be determined. Please ensure you are in a Storm Stack project."); } const mode = inlineConfig.mode || userConfig?.mode || context.options.workspaceConfig?.mode || "production"; const resolvedUserConfig = await loadUserConfigFile(resolvedProjectRoot, context.resolver, context.options.command, mode); const workspaceConfig = { ...context.options.workspaceConfig }; delete workspaceConfig.name; const mergedUserConfig = defu(userConfig ?? {}, resolvedUserConfig); const resolvedOptions = defu({ inlineConfig, userConfig: mergedUserConfig, projectRoot: resolvedProjectRoot, workspaceConfig: context.options.workspaceConfig }, inlineConfig, mergedUserConfig.config ?? {}, { ...context.options, mode, tsconfig: getTsconfigFilePath(resolvedProjectRoot, context.options.tsconfig || "tsconfig.json") }, workspaceConfig, { platform: "neutral", mode: "production", projectType: "application", logLevel: "info", templates: joinPaths(resolvedProjectRoot, "templates"), isSsrBuild: false, isPreview: false, babel: { plugins: [], presets: [] }, esbuild: { override: {} }, unbuild: { override: {}, loaders: [] } }); resolvedOptions.output = defu(resolvedOptions.output ?? {}, { outputPath: resolvedOptions.tsconfigRaw?.compilerOptions?.outDir, outputMode: resolvedOptions.output?.outputMode }, { outputPath: resolvedProjectRoot === workspaceRoot ? "dist" : joinPaths("dist", resolvedProjectRoot), outputMode: "memory", assets: [ { input: resolvedProjectRoot, glob: "README.md", output: "/" }, { input: resolvedProjectRoot, glob: "CHANGELOG.md", output: "/" }, { input: "", glob: "LICENSE", output: "/" } ] }); resolvedOptions.environment ??= defaultEnvironmentName(resolvedOptions); resolvedOptions.sourceRoot ??= joinPaths(resolvedOptions.projectRoot, "src"); resolvedOptions.tsconfig ??= getTsconfigFilePath(resolvedOptions.projectRoot, resolvedOptions.tsconfig); context.options = resolvedOptions; context.options.colors = getColors(resolvedOptions); context.options.logLevel = context.options.logLevel === "silent" ? null : context.options.logLevel === "success" ? "info" : context.options.logLevel === "trace" || context.options.logLevel === "all" ? "debug" : context.options.logLevel; context.options.userConfig.plugins = mergedUserConfig.plugins ?? []; context.options.plugins = { config: { additionalFiles: [] } }; return resolvedOptions; } __name(resolveConfig, "resolveConfig"); function defaultEnvironmentName(options) { if (options.platform === "node" || options.isSsrBuild) { return "server"; } if (options.platform === "browser") { return "client"; } return "shared"; } __name(defaultEnvironmentName, "defaultEnvironmentName"); export { defaultEnvironmentName, loadUserConfigFile, resolveConfig }; //# sourceMappingURL=chunk-EAAFBEI6.js.map //# sourceMappingURL=chunk-EAAFBEI6.js.map