@storm-software/esbuild
Version:
A package containing `esbuild` utilities for building Storm Software libraries and applications
86 lines (82 loc) • 2.76 kB
JavaScript
import {
joinPaths,
normalizeWindowsPath
} from "../chunk-B4VYSTGG.js";
import {
__require,
init_esm_shims
} from "../chunk-5OCVL4NC.js";
// src/plugins/resolve-paths.ts
init_esm_shims();
// ../config-tools/src/utilities/file-path-utils.ts
init_esm_shims();
function findFileName(filePath, { requireExtension, withExtension } = {}) {
const result = normalizeWindowsPath(filePath)?.split(filePath?.includes("\\") ? "\\" : "/")?.pop() ?? "";
if (requireExtension === true && !result.includes(".")) {
return "";
}
if (withExtension === false && result.includes(".")) {
return result.split(".").slice(-1).join(".") || "";
}
return result;
}
function findFilePath(filePath) {
const normalizedPath = normalizeWindowsPath(filePath);
return normalizedPath.replace(
findFileName(normalizedPath, { requireExtension: true }),
""
);
}
// src/plugins/resolve-paths.ts
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
};