UNPKG

@toruslabs/torus-scripts

Version:
72 lines (61 loc) 2.19 kB
// gets the tsconfig for build // merges the user provided config with the default config // and returns the merged config import fs from "fs"; import ts from "typescript"; import merge from "lodash.mergewith"; import { createRequire } from "node:module"; import paths from "./paths.js"; const require = createRequire(import.meta.url); const userPathExists = fs.existsSync(paths.appTsBuildConfig); const userConfig = userPathExists ? ts.readConfigFile(paths.appTsBuildConfig, ts.sys.readFile).config : {}; /* TODO: change this when fork-ts-checker-webpack-plugin is updated to support tsconfig.json extends as an array // objValue is the first object (our default config) function customizer(objValue, srcValue, key) { if (key === "extends") { const finalArray = []; if (Array.isArray(objValue)) { finalArray.push(...objValue); } else finalArray.push(objValue); if (Array.isArray(srcValue)) { finalArray.push(...srcValue); } else finalArray.push(srcValue); return [...new Set(finalArray)]; } if (Array.isArray(objValue)) { return srcValue; } } */ // objValue is the first object (our default config) function customizer(objValue, srcValue) { if (Array.isArray(objValue)) { return srcValue; } } function getFullTsConfigFromFile(filePath) { const configPath = require.resolve(filePath); const config = ts.readConfigFile(configPath, ts.sys.readFile).config; if (typeof config.extends === "string" && config.extends) { const extend = config.extends; delete config.extends; return merge(config, getFullTsConfigFromFile(extend), customizer); } else if (Array.isArray(config.extends)) { const extend = config.extends; delete config.extends; return merge(config, ...extend.map((x) => getFullTsConfigFromFile(x)), customizer); } return config; } const defaultConfig = getFullTsConfigFromFile("@toruslabs/config/tsconfig.build.json"); const mergedConfig = merge( defaultConfig, { compilerOptions: { outDir: paths.appBuildPath, declarationDir: paths.appBuildPath + "/types", rootDir: paths.appSrc }, include: ["src"], }, userConfig, customizer, ); export default mergedConfig;