esbuild-fix-imports-plugin
Version:
An ESBuild plugin that fixes import paths by applying fixAliasPlugin, fixFolderImportsPlugin, and fixExtensionsPlugin. It ensures correct file extensions, resolves path aliases, and fixes directory imports in your build output when using 'tsup' with 'bund
97 lines • 3.57 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var fixFolderImportsPlugin_exports = {};
__export(fixFolderImportsPlugin_exports, {
fixFolderImportsPlugin: () => fixFolderImportsPlugin
});
module.exports = __toCommonJS(fixFolderImportsPlugin_exports);
var import_path = require("path");
const fixFolderImportsPlugin = () => ({
name: "fixFolderImportsPlugin",
setup(build) {
const outExtension = build.initialOptions.outExtension?.[".js"] ?? ".js";
build.onEnd((result) => {
if (result.errors.length > 0) {
return;
}
const allJsOutputs = new Set(
(result.outputFiles ?? []).filter((f) => f.path.endsWith(outExtension)).map((f) => f.path)
);
for (const outputFile of result.outputFiles ?? []) {
if (!outputFile.path.endsWith(outExtension)) {
continue;
}
const fileContents = outputFile.text;
const filePath = outputFile.path;
const nextFileContents = modifyFolderImports(
fileContents,
filePath,
outExtension,
allJsOutputs
);
if (nextFileContents !== fileContents) {
outputFile.contents = Buffer.from(nextFileContents, "utf-8");
}
}
});
}
});
const ESM_RELATIVE_IMPORT_EXP = /from\s*['"](\..+?)['"]/g;
const CJS_RELATIVE_IMPORT_EXP = /require\s*\(\s*['"](\..+?)['"]\s*\)/g;
const hasExtensionRegex = /\.[^./\\]+$/;
const modifyFolderImports = (contents, filePath, outExtension, allJsOutputs) => {
contents = contents.replace(ESM_RELATIVE_IMPORT_EXP, (match, importPath) => {
const newPath = replaceFolderImport(
importPath,
filePath,
outExtension,
allJsOutputs
);
return match.replace(importPath, newPath);
});
contents = contents.replace(CJS_RELATIVE_IMPORT_EXP, (match, importPath) => {
const newPath = replaceFolderImport(
importPath,
filePath,
outExtension,
allJsOutputs
);
return match.replace(importPath, newPath);
});
return contents;
};
const replaceFolderImport = (importPath, filePath, outExtension, allJsOutputs) => {
if (importPath.endsWith("/") || importPath.endsWith(".") || hasExtensionRegex.test(importPath)) {
return importPath;
}
const currentDir = (0, import_path.dirname)(filePath);
const candidateFile = (0, import_path.resolve)(currentDir, importPath) + outExtension;
const candidateIndex = (0, import_path.resolve)(currentDir, importPath, "index") + outExtension;
if (allJsOutputs.has(candidateFile)) {
return importPath;
} else if (allJsOutputs.has(candidateIndex)) {
return importPath + "/index";
}
return importPath;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
fixFolderImportsPlugin
});
//# sourceMappingURL=fixFolderImportsPlugin.cjs.map