UNPKG

@storm-software/esbuild

Version:

A package containing `esbuild` utilities for building Storm Software libraries and applications

123 lines (119 loc) 3.88 kB
import { DEFAULT_BUILD_OPTIONS } from "./chunk-W4OIIV5S.js"; import { getEnv, getWorkspaceConfig } from "./chunk-NVYTS7UE.js"; import { joinPaths } from "./chunk-B4VYSTGG.js"; import { formatLogMessage, getStopwatch, writeDebug } from "./chunk-CKFE6AY5.js"; import { init_esm_shims } from "./chunk-5OCVL4NC.js"; // src/context.ts init_esm_shims(); import { createProjectGraphAsync, readProjectsConfigurationFromProjectGraph } from "@nx/devkit"; import defu from "defu"; import { existsSync } from "node:fs"; import hf from "node:fs/promises"; import { findWorkspaceRoot } from "nx/src/utils/find-workspace-root"; async function resolveContext(userOptions) { const projectRoot = userOptions.projectRoot; const workspaceRoot = findWorkspaceRoot(projectRoot); if (!workspaceRoot) { throw new Error("Cannot find Nx workspace root"); } const workspaceConfig = await getWorkspaceConfig(true, { workspaceRoot: workspaceRoot.dir }); writeDebug(" \u2699\uFE0F Resolving build options", workspaceConfig); const stopwatch = getStopwatch("Build options resolution"); const projectGraph = await createProjectGraphAsync({ exitOnError: true }); const projectJsonPath = joinPaths( workspaceRoot.dir, projectRoot, "project.json" ); if (!existsSync(projectJsonPath)) { throw new Error("Cannot find project.json configuration"); } const projectJsonFile = await hf.readFile(projectJsonPath, "utf8"); const projectJson = JSON.parse(projectJsonFile); const projectName = projectJson.name || userOptions.name; const projectConfigurations = readProjectsConfigurationFromProjectGraph(projectGraph); if (!projectConfigurations?.projects?.[projectName]) { throw new Error( "The Build process failed because the project does not have a valid configuration in the project.json file. Check if the file exists in the root of the project." ); } const options = defu(userOptions, DEFAULT_BUILD_OPTIONS); options.name ??= projectName; const packageJsonPath = joinPaths( workspaceRoot.dir, options.projectRoot, "package.json" ); if (!existsSync(packageJsonPath)) { throw new Error("Cannot find package.json configuration"); } const env = getEnv("esbuild", options); const define = defu(options.define ?? {}, env ?? {}); const resolvedOptions = { ...options, tsconfig: userOptions.tsconfig === null ? void 0 : userOptions.tsconfig ? userOptions.tsconfig : joinPaths(workspaceRoot.dir, projectRoot, "tsconfig.json"), metafile: userOptions.mode === "development", clean: false, env, define: { STORM_FORMAT: JSON.stringify(options.format), ...Object.keys(define).filter((key) => define[key] !== void 0).reduce((res, key) => { const value = JSON.stringify(define[key]); const safeKey = key.replaceAll("(", "").replaceAll(")", ""); return { ...res, [`process.env.${safeKey}`]: value, [`import.meta.env.${safeKey}`]: value }; }, {}) } }; stopwatch(); const context = { options: resolvedOptions, clean: userOptions.clean !== false, workspaceConfig, projectConfigurations, projectName, projectGraph, sourceRoot: resolvedOptions.sourceRoot || projectJson.sourceRoot || joinPaths(resolvedOptions.projectRoot, "src"), outputPath: resolvedOptions.outputPath || joinPaths( workspaceConfig.workspaceRoot, "dist", resolvedOptions.projectRoot ), minify: resolvedOptions.minify || resolvedOptions.mode === "production" }; context.options.esbuildPlugins = [...context.options.esbuildPlugins ?? []]; if (context.options.verbose) { writeDebug( ` \u2699\uFE0F Build options resolved: ${formatLogMessage(context.options)}`, workspaceConfig ); } return context; } export { resolveContext };