@storm-software/esbuild
Version:
A package containing `esbuild` utilities for building Storm Software libraries and applications
123 lines (102 loc) • 5.39 kB
JavaScript
;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 _chunk5R2QF7FGcjs = require('./chunk-5R2QF7FG.cjs');
var _chunk4KTFAYGLcjs = require('./chunk-4KTFAYGL.cjs');
var _chunkI7VU53I5cjs = require('./chunk-I7VU53I5.cjs');
var _chunk7DZ3NOLBcjs = require('./chunk-7DZ3NOLB.cjs');
var _chunkJG7MZJV2cjs = require('./chunk-JG7MZJV2.cjs');
// src/context.ts
_chunkJG7MZJV2cjs.init_cjs_shims.call(void 0, );
var _devkit = require('@nx/devkit');
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 _chunk4KTFAYGLcjs.getWorkspaceConfig.call(void 0, true, {
workspaceRoot: workspaceRoot.dir
});
_chunk7DZ3NOLBcjs.writeDebug.call(void 0, " \u2699\uFE0F Resolving build options", workspaceConfig);
const stopwatch = _chunk7DZ3NOLBcjs.getStopwatch.call(void 0, "Build options resolution");
const projectGraph = await _devkit.createProjectGraphAsync.call(void 0, {
exitOnError: true
});
const projectJsonPath = _chunkI7VU53I5cjs.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, _chunk5R2QF7FGcjs.DEFAULT_BUILD_OPTIONS);
options.name ??= projectName;
const packageJsonPath = _chunkI7VU53I5cjs.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 = _chunk4KTFAYGLcjs.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 : _chunkI7VU53I5cjs.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 || _chunkI7VU53I5cjs.joinPaths.call(void 0, resolvedOptions.projectRoot, "src"),
outputPath: resolvedOptions.outputPath || _chunkI7VU53I5cjs.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) {
_chunk7DZ3NOLBcjs.writeDebug.call(void 0,
` \u2699\uFE0F Build options resolved:
${_chunk7DZ3NOLBcjs.formatLogMessage.call(void 0, context.options)}`,
workspaceConfig
);
}
return context;
}
exports.resolveContext = resolveContext;