esbuild-plugin-alias-path
Version:
ESBuild plugin for alias path.
119 lines (114 loc) • 4.28 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
aliasPath: () => aliasPath,
default: () => src_default
});
module.exports = __toCommonJS(src_exports);
// src/lib/normalize-options.ts
var import_fs_extra = __toESM(require("fs-extra"));
var import_path = __toESM(require("path"));
function recursiveResolve(alias, cwd) {
const result = {};
for (const [k, v] of Object.entries(alias)) {
if (import_fs_extra.default.statSync(v).isDirectory()) {
import_fs_extra.default.readdirSync(v).forEach((fileOrDir) => {
const isStillDir = import_fs_extra.default.statSync(import_path.default.join(v, fileOrDir)).isDirectory();
if (isStillDir) {
const nextExploreReplacementDir = import_path.default.join(v, fileOrDir);
const currentDir = import_path.default.dirname(k).replace("**", "");
const nextExploreMatchDir = import_path.default.join(currentDir, fileOrDir);
const resolved = recursiveResolve({
[nextExploreMatchDir]: nextExploreReplacementDir
}, cwd);
const [[_k, _v]] = Object.entries(resolved);
result[_k] = _v;
} else {
const replacedKey = k.endsWith("*") ? k.replace("*", fileOrDir.replace(import_path.default.extname(fileOrDir), "")) : import_path.default.join(k, fileOrDir.replace(import_path.default.extname(fileOrDir), ""));
const absoluteReplacementValue = import_path.default.resolve(v, fileOrDir);
result[replacedKey] = absoluteReplacementValue;
}
});
k.endsWith("*") && delete alias[k];
} else {
result[k] = v;
}
}
return result;
}
__name(recursiveResolve, "recursiveResolve");
function normalizeOption(options = {}) {
const alias = options.alias ?? {};
const cwd = options.cwd ?? process.cwd();
const resolvedAlias = recursiveResolve(alias, cwd);
const shouldSkipThisPlugin = options.skip ?? !Object.keys(resolvedAlias).length;
return {
alias: resolvedAlias,
skip: shouldSkipThisPlugin,
cwd
};
}
__name(normalizeOption, "normalizeOption");
// src/lib/esbuild-plugin-alias-path.ts
var pluginName = "plugin:alias-path";
function escapeNamespace(keys) {
return new RegExp(`^${keys.map((str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")}$`);
}
__name(escapeNamespace, "escapeNamespace");
var aliasPath = /* @__PURE__ */ __name((options = {}) => {
const { alias, skip } = normalizeOption(options);
if (skip) {
return {
name: pluginName,
setup() {
}
};
}
const escapedNamespace = escapeNamespace(Object.keys(alias));
return {
name: pluginName,
setup(build) {
build.onResolve({
filter: escapedNamespace
}, ({ path: fromPath }) => {
const replacedPath = alias[fromPath];
if (!replacedPath) {
return null;
}
return {
path: replacedPath
};
});
}
};
}, "aliasPath");
// src/index.ts
var src_default = aliasPath;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
aliasPath
});