UNPKG

@storm-software/esbuild

Version:

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

60 lines (58 loc) 2.12 kB
import { __require } from "../chunk-3RG5ZIWI.js"; // src/plugins/resolve-paths.ts import { joinPaths } from "@storm-software/config-tools/utilities/correct-paths"; import { findFilePath } from "@storm-software/config-tools/utilities/file-path-utils"; import path from "node:path"; function resolvePathsConfig(options, cwd, projectRoot) { if (options?.compilerOptions?.paths) { const paths = Object.entries(options.compilerOptions.paths); const resolvedPaths = paths.map(([key, paths2]) => { return [key, paths2.map((v) => path.resolve(cwd, v))]; }); return Object.fromEntries(resolvedPaths); } if (options.extends) { const extendsPath = path.resolve( projectRoot ? joinPaths(cwd, projectRoot, options.extends) : joinPaths(cwd, options.extends) ); const extendsDir = path.dirname(extendsPath); const extendsConfig = __require(extendsPath); return resolvePathsConfig(extendsConfig, extendsDir); } return []; } var resolvePathsPlugin = (context) => ({ name: "storm:resolve-paths", setup(build) { const parentTsConfig = build.initialOptions.tsconfig ? __require(joinPaths( context.workspaceConfig.workspaceRoot, build.initialOptions.tsconfig.replace( context.workspaceConfig.workspaceRoot, "" ) )) : __require(joinPaths(context.workspaceConfig.workspaceRoot, "tsconfig.json")); const resolvedTsPaths = resolvePathsConfig( parentTsConfig, context.workspaceConfig.workspaceRoot, context.options.projectRoot ); const packagesRegex = new RegExp( `^(${Object.keys(resolvedTsPaths).join("|")})$` ); build.onResolve({ filter: packagesRegex }, (args) => { if (build.initialOptions.external?.includes(args.path)) { return { path: args.path, external: true }; } let resolvedPath = resolvedTsPaths[args.path][0]; if (resolvedPath.endsWith(".ts") || resolvedPath.endsWith(".tsx")) { resolvedPath = findFilePath(resolvedPath); } return { path: joinPaths(resolvedPath, "index.ts") }; }); } }); export { resolvePathsPlugin };