UNPKG

@storm-software/unbuild

Version:

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

107 lines (102 loc) 2.93 kB
import { writeTrace } from "./chunk-DY4PAY36.js"; // src/plugins/tsc.ts import { createProjectGraphAsync, readCachedProjectGraph } from "@nx/devkit"; import { calculateProjectBuildableDependencies } from "@nx/js/src/utils/buildable-libs-utils"; import { getHelperDependency, HelperDependency } from "@nx/js/src/utils/compiler-helper-dependency"; import ts2Plugin from "rollup-plugin-typescript2"; // src/utilities/helpers.ts import { joinPathFragments } from "@nx/devkit"; import { computeCompilerOptionsPaths } from "@nx/js/src/utils/buildable-libs-utils"; 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 ); let dependencies = result.dependencies; const tsLibDependency = getHelperDependency( HelperDependency.tsc, options.tsconfig, dependencies, projectGraph, true ); if (tsLibDependency) { dependencies = dependencies.filter( (deps) => deps.name !== tsLibDependency.name ); dependencies.push(tsLibDependency); } const compilerOptions = await createTsCompilerOptions( options.config, options.tsconfig, options.projectRoot, dependencies ); return ts2Plugin({ check: options.declaration !== false, tsconfig: options.tsconfig, tsconfigOverride: compilerOptions }); }; export { loadConfig, createTsCompilerOptions, tscPlugin };