@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
260 lines (253 loc) • 10.2 kB
JavaScript
var chunkA43FUIC2_cjs = require('./chunk-A43FUIC2.cjs');
var exists = require('@stryke/fs/exists');
var json = require('@stryke/fs/json');
var joinPaths = require('@stryke/path/join-paths');
var replace = require('@stryke/path/replace');
var defu2 = require('defu');
var ts = require('typescript');
var getConfig = require('@storm-software/config-tools/get-config');
var getWorkspaceRoot = require('@stryke/fs/get-workspace-root');
var isFunction = require('@stryke/type-checks/is-function');
var isSetObject = require('@stryke/type-checks/is-set-object');
var c12 = require('c12');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var defu2__default = /*#__PURE__*/_interopDefault(defu2);
var ts__default = /*#__PURE__*/_interopDefault(ts);
// src/lib/typescript/tsconfig.ts
chunkA43FUIC2_cjs.init_cjs_shims();
function getTsconfigFilePath(projectRoot, tsconfig = "tsconfig.json") {
let tsconfigFilePath = tsconfig;
if (!exists.existsSync(tsconfigFilePath)) {
tsconfigFilePath = joinPaths.joinPaths(projectRoot, replace.replacePath(tsconfig, projectRoot));
if (!exists.existsSync(tsconfigFilePath)) {
throw new Error(`Cannot find the \`tsconfig.json\` configuration file at ${joinPaths.joinPaths(projectRoot, replace.replacePath(tsconfig, projectRoot))} or ${tsconfigFilePath}`);
}
}
return tsconfigFilePath;
}
chunkA43FUIC2_cjs.__name(getTsconfigFilePath, "getTsconfigFilePath");
function findMatch(tsconfigType, types, extensions = [
".ts",
".tsx",
".d.ts"
]) {
return types.find((type) => tsconfigType?.toString().toLowerCase() === type?.toString().toLowerCase() || tsconfigType?.toString().toLowerCase() === `./${type?.toString().toLowerCase()}` || `./${tsconfigType?.toString().toLowerCase()}` === type?.toString().toLowerCase() || extensions.some((ext) => `${tsconfigType?.toString().toLowerCase()}${ext}` === type?.toString().toLowerCase() || `${tsconfigType?.toString().toLowerCase()}${ext}` === `./${type?.toString().toLowerCase()}` || `${type?.toString().toLowerCase()}${ext}` === `./${tsconfigType?.toString().toLowerCase()}` || tsconfigType?.toString().toLowerCase() === `${type?.toString().toLowerCase()}${ext}` || tsconfigType?.toString().toLowerCase() === `./${type?.toString().toLowerCase()}${ext}` || type?.toString().toLowerCase() === `./${tsconfigType?.toString().toLowerCase()}${ext}`));
}
chunkA43FUIC2_cjs.__name(findMatch, "findMatch");
function findIncludeMatch(tsconfigType, types) {
return findMatch(tsconfigType, types, [
".ts",
".tsx",
".d.ts",
".js",
".jsx",
".mjs",
".cjs",
".mts",
".cts",
"/*.ts",
"/*.tsx",
"/*.d.ts",
"/*.js",
"/*.jsx",
"/*.mjs",
"/*.cjs",
"/*.mts",
"/*.cts",
"/**/*.ts",
"/**/*.tsx",
"/**/*.d.ts",
"/**/*.js",
"/**/*.jsx",
"/**/*.mjs",
"/**/*.cjs",
"/**/*.mts",
"/**/*.cts"
]);
}
chunkA43FUIC2_cjs.__name(findIncludeMatch, "findIncludeMatch");
function isIncludeMatchFound(tsconfigType, types) {
return findIncludeMatch(tsconfigType, types) !== void 0;
}
chunkA43FUIC2_cjs.__name(isIncludeMatchFound, "isIncludeMatchFound");
function getParsedTypeScriptConfig(workspaceRoot, projectRoot, tsconfig, tsconfigRaw = {}, host = ts__default.default.sys) {
const tsconfigFilePath = getTsconfigFilePath(projectRoot, tsconfig);
const tsconfigJson = json.readJsonFileSync(tsconfigFilePath);
if (!tsconfigJson) {
throw new Error(`Cannot find the \`tsconfig.json\` configuration file at ${joinPaths.joinPaths(projectRoot, tsconfig ?? "tsconfig.json")}`);
}
const parsedCommandLine = ts__default.default.parseJsonConfigFileContent(defu2__default.default(tsconfigRaw ?? {}, tsconfigJson), host, joinPaths.joinPaths(workspaceRoot, projectRoot));
if (parsedCommandLine.errors.length > 0) {
const errorMessage = `Cannot parse the TypeScript compiler options. Please investigate the following issues:
${parsedCommandLine.errors.map((error) => `- ${(error.category !== void 0 && error.code ? `[${error.category}-${error.code}]: ` : "") + error.messageText.toString()}`).join("\n")}
`;
throw new Error(errorMessage);
}
return {
...parsedCommandLine,
tsconfigJson,
tsconfigFilePath
};
}
chunkA43FUIC2_cjs.__name(getParsedTypeScriptConfig, "getParsedTypeScriptConfig");
// src/lib/config.ts
chunkA43FUIC2_cjs.init_cjs_shims();
async function loadUserConfigFile(projectRoot, jiti, command, mode) {
let resolvedUserConfig = {};
const resolvedUserConfigFile = exists.existsSync(joinPaths.joinPaths(projectRoot, "storm.config.ts")) ? joinPaths.joinPaths(projectRoot, "storm.config.ts") : exists.existsSync(joinPaths.joinPaths(projectRoot, "storm.config.js")) ? joinPaths.joinPaths(projectRoot, "storm.config.js") : exists.existsSync(joinPaths.joinPaths(projectRoot, "storm.config.mts")) ? joinPaths.joinPaths(projectRoot, "storm.config.mts") : exists.existsSync(joinPaths.joinPaths(projectRoot, "storm.config.mjs")) ? joinPaths.joinPaths(projectRoot, "storm.config.mjs") : void 0;
if (resolvedUserConfigFile) {
const resolved = await jiti.import(jiti.esmResolve(resolvedUserConfigFile));
if (resolved) {
let config = {};
if (isFunction.isFunction(resolved)) {
config = await Promise.resolve(resolved({
command,
mode: mode || "production",
isSsrBuild: false,
isPreview: false
}));
}
if (isSetObject.isSetObject(config)) {
resolvedUserConfig = {
...config,
config,
configFile: resolvedUserConfigFile
};
}
}
}
const result = await Promise.all([
c12.loadConfig({
cwd: projectRoot,
name: "storm",
envName: mode,
globalRc: true,
packageJson: true,
dotenv: true,
jiti
}),
c12.loadConfig({
cwd: projectRoot,
name: "storm.config",
rcFile: false,
globalRc: false,
packageJson: false,
dotenv: false,
jiti
}),
c12.loadConfig({
cwd: projectRoot,
name: "storm-stack",
envName: mode,
globalRc: true,
packageJson: "stormStack",
jiti
})
]);
return defu2__default.default(resolvedUserConfig, isSetObject.isSetObject(result[0]?.config) ? {
...result[0].config,
...result[0]
} : {}, isSetObject.isSetObject(result[1]?.config) ? {
...result[1].config,
...result[1]
} : {}, isSetObject.isSetObject(result[2]?.config) ? {
...result[2].config,
...result[2]
} : {});
}
chunkA43FUIC2_cjs.__name(loadUserConfigFile, "loadUserConfigFile");
async function resolveConfig(context, inlineConfig, userConfig, projectRoot) {
const resolvedProjectRoot = projectRoot || inlineConfig.root || userConfig?.root || getWorkspaceRoot.getProjectRoot() || process.cwd();
const workspaceConfig = defu2__default.default(await getConfig.getWorkspaceConfig(), {
workspaceRoot: context.options.workspaceConfig?.workspaceRoot ?? getWorkspaceRoot.getWorkspaceRoot()
});
if (!workspaceConfig) {
throw new Error("The workspace root could not be determined. Please ensure you are in a Storm Stack project.");
}
const mergedUserConfig = defu2__default.default({
config: userConfig ?? {}
}, await loadUserConfigFile(resolvedProjectRoot, context.resolver, context.options.command, inlineConfig.mode || userConfig?.mode || context.options.workspaceConfig?.mode || "production"));
const resolvedOptions = defu2__default.default({
inlineConfig,
userConfig: mergedUserConfig,
workspaceConfig,
projectRoot: resolvedProjectRoot,
workspaceRoot: workspaceConfig.workspaceRoot
}, inlineConfig, mergedUserConfig.config ?? {}, {
...context.options,
tsconfig: getTsconfigFilePath(resolvedProjectRoot, context.options.tsconfig ?? "tsconfig.json")
}, {
platform: "neutral",
mode: "production",
projectType: "application",
logLevel: "info",
isSsrBuild: false,
isPreview: false,
babel: {
plugins: [],
presets: []
},
build: {},
override: {}
});
resolvedOptions.output = defu2__default.default(resolvedOptions.output ?? {}, {
outputPath: resolvedOptions.tsconfigRaw?.compilerOptions?.outDir,
outputMode: resolvedOptions.output?.outputMode
}, {
outputPath: resolvedProjectRoot === workspaceConfig.workspaceRoot ? "dist" : joinPaths.joinPaths("dist", resolvedProjectRoot),
outputMode: "virtual",
assets: [
{
input: resolvedProjectRoot,
glob: "README.md",
output: "/"
},
{
input: resolvedProjectRoot,
glob: "CHANGELOG.md",
output: "/"
},
{
input: "",
glob: "LICENSE",
output: "/"
}
]
});
resolvedOptions.environment ??= defaultEnvironmentName(resolvedOptions);
resolvedOptions.sourceRoot ??= joinPaths.joinPaths(resolvedOptions.projectRoot, "src");
resolvedOptions.tsconfig ??= getTsconfigFilePath(resolvedOptions.projectRoot, resolvedOptions.tsconfig);
context.options = 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 ??= {};
context.options.userConfig.plugins = mergedUserConfig.plugins ?? [];
context.options.plugins = {
config: {
additionalFiles: []
}
};
return resolvedOptions;
}
chunkA43FUIC2_cjs.__name(resolveConfig, "resolveConfig");
function defaultEnvironmentName(options) {
if (options.isSsrBuild) {
return "ssr";
}
if (options.isPreview) {
return "preview";
}
if (options.platform === "node" || options.isSsrBuild) {
return "server";
}
if (options.platform === "browser") {
return "client";
}
return "shared";
}
chunkA43FUIC2_cjs.__name(defaultEnvironmentName, "defaultEnvironmentName");
exports.defaultEnvironmentName = defaultEnvironmentName;
exports.getParsedTypeScriptConfig = getParsedTypeScriptConfig;
exports.getTsconfigFilePath = getTsconfigFilePath;
exports.isIncludeMatchFound = isIncludeMatchFound;
exports.resolveConfig = resolveConfig;
;