UNPKG

@storm-software/esbuild

Version:

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

115 lines (101 loc) 5.32 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkTCXATKEDcjs = require('./chunk-TCXATKED.cjs'); // src/context.ts var _devkit = require('@nx/devkit'); var _buildtools = require('@storm-software/build-tools'); var _getconfig = require('@storm-software/config-tools/get-config'); var _console = require('@storm-software/config-tools/logger/console'); var _correctpaths = require('@storm-software/config-tools/utilities/correct-paths'); var _defu = require('defu'); var _defu2 = _interopRequireDefault(_defu); var _fs = require('fs'); var _promises = require('fs/promises'); var _promises2 = _interopRequireDefault(_promises); var _findworkspaceroot = require('nx/src/utils/find-workspace-root'); async function resolveContext(userOptions) { const projectRoot = userOptions.projectRoot; const workspaceRoot = _findworkspaceroot.findWorkspaceRoot.call(void 0, projectRoot); if (!workspaceRoot) { throw new Error("Cannot find Nx workspace root"); } const workspaceConfig = await _getconfig.getWorkspaceConfig.call(void 0, true, { workspaceRoot: workspaceRoot.dir }); _console.writeDebug.call(void 0, " \u2699\uFE0F Resolving build options", workspaceConfig); const stopwatch = _console.getStopwatch.call(void 0, "Build options resolution"); const projectGraph = await _devkit.createProjectGraphAsync.call(void 0, { exitOnError: true }); const projectJsonPath = _correctpaths.joinPaths.call(void 0, workspaceRoot.dir, projectRoot, "project.json" ); if (!_fs.existsSync.call(void 0, projectJsonPath)) { throw new Error("Cannot find project.json configuration"); } const projectJsonFile = await _promises2.default.readFile(projectJsonPath, "utf8"); const projectJson = JSON.parse(projectJsonFile); const projectName = projectJson.name || userOptions.name; const projectConfigurations = _devkit.readProjectsConfigurationFromProjectGraph.call(void 0, projectGraph); if (!_optionalChain([projectConfigurations, 'optionalAccess', _ => _.projects, 'optionalAccess', _2 => _2[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 = _defu2.default.call(void 0, userOptions, _chunkTCXATKEDcjs.DEFAULT_BUILD_OPTIONS); options.name ??= projectName; const packageJsonPath = _correctpaths.joinPaths.call(void 0, workspaceRoot.dir, options.projectRoot, "package.json" ); if (!_fs.existsSync.call(void 0, packageJsonPath)) { throw new Error("Cannot find package.json configuration"); } const env = _buildtools.getEnv.call(void 0, "esbuild", options); const define = _defu2.default.call(void 0, _nullishCoalesce(options.define, () => ( {})), _nullishCoalesce(env, () => ( {}))); const resolvedOptions = { ...options, tsconfig: userOptions.tsconfig === null ? void 0 : userOptions.tsconfig ? userOptions.tsconfig : _correctpaths.joinPaths.call(void 0, 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 || _correctpaths.joinPaths.call(void 0, resolvedOptions.projectRoot, "src"), outputPath: resolvedOptions.outputPath || _correctpaths.joinPaths.call(void 0, workspaceConfig.workspaceRoot, "dist", resolvedOptions.projectRoot ), minify: resolvedOptions.minify || resolvedOptions.mode === "production" }; context.options.esbuildPlugins = [..._nullishCoalesce(context.options.esbuildPlugins, () => ( []))]; if (context.options.verbose) { _console.writeDebug.call(void 0, ` \u2699\uFE0F Build options resolved: ${_console.formatLogMessage.call(void 0, context.options)}`, workspaceConfig ); } return context; } exports.resolveContext = resolveContext;