@storm-software/unbuild
Version:
A package containing `unbuild` utilities for building Storm Software libraries and applications
89 lines (84 loc) • 2.44 kB
JavaScript
import {
writeTrace
} from "./chunk-ZO3LA4ZI.js";
// src/plugins/tsc.ts
import {
createProjectGraphAsync,
readCachedProjectGraph
} from "@nx/devkit";
import { calculateProjectBuildableDependencies } from "@nx/js/internal";
import ts2Plugin from "rollup-plugin-typescript2";
// src/utilities/helpers.ts
import { joinPathFragments } from "@nx/devkit";
import {
computeCompilerOptionsPaths
} from "@nx/js/internal";
import { dirname, extname } from "node:path";
import { pathToFileURL } from "node:url";
import ts from "typescript";
async function loadConfig(configPath) {
if (!/\.(js|mjs)$/.test(extname(configPath))) {
throw new Error("Unsupported config file format");
}
return import(pathToFileURL(configPath).toString()).then(
(config) => config.default
);
}
async function createTsCompilerOptions(config, tsConfigPath, projectRoot, dependencies) {
const tsConfigFile = ts.readConfigFile(
joinPathFragments(config.workspaceRoot, projectRoot, tsConfigPath),
ts.sys.readFile
);
const tsConfig = ts.parseJsonConfigFileContent(
tsConfigFile.config,
ts.sys,
dirname(joinPathFragments(config.workspaceRoot, projectRoot, tsConfigPath))
);
const compilerOptions = {
rootDir: projectRoot,
declaration: true,
paths: computeCompilerOptionsPaths(tsConfig, dependencies ?? [])
};
writeTrace(compilerOptions, config);
return compilerOptions;
}
// src/plugins/tsc.ts
var tscPlugin = async (options) => {
let projectGraph;
try {
projectGraph = readCachedProjectGraph();
} catch {
await createProjectGraphAsync();
projectGraph = readCachedProjectGraph();
}
if (!projectGraph) {
throw new Error(
"The build process failed because the project graph is not available. Please run the build command again."
);
}
const result = calculateProjectBuildableDependencies(
void 0,
projectGraph,
options.config.workspaceRoot,
options.projectName,
process.env.NX_TASK_TARGET_TARGET || "build",
process.env.NX_TASK_TARGET_CONFIGURATION || "production",
true
);
const compilerOptions = await createTsCompilerOptions(
options.config,
options.tsconfig,
options.projectRoot,
result.dependencies
);
return ts2Plugin({
check: options.declaration !== false,
tsconfig: options.tsconfig,
tsconfigOverride: compilerOptions
});
};
export {
loadConfig,
createTsCompilerOptions,
tscPlugin
};