UNPKG

@plugjs/plug

Version:
90 lines (88 loc) 4.35 kB
"use strict"; 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 __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( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // plugs/esbuild/fix-extensions.ts var fix_extensions_exports = {}; __export(fix_extensions_exports, { fixExtensions: () => fixExtensions }); module.exports = __toCommonJS(fix_extensions_exports); var import_node_path = __toESM(require("node:path"), 1); var import_fs = require("../../fs.cjs"); var import_paths = require("../../paths.cjs"); function fixExtensions() { return { name: "fix-extensions", setup(build) { build.initialOptions.bundle = true; const cjs = build.initialOptions.outExtension?.[".cjs"] || ".cjs"; const mjs = build.initialOptions.outExtension?.[".mjs"] || ".mjs"; const js = build.initialOptions.outExtension?.[".js"] || ".js"; const exts = build.initialOptions.resolveExtensions || [".ts", ".js", ".tsx", ".jsx"]; build.onResolve({ filter: /.*/ }, async (args) => { if (!args.importer) return null; if (!args.path.match(/^\.\.?\//)) return { external: true }; const resolveDir = args.resolveDir; (0, import_paths.assertAbsolutePath)(resolveDir); const resolved = (0, import_paths.resolveAbsolutePath)(resolveDir, args.path); if ((0, import_paths.resolveFile)(resolved)) return { path: args.path, external: true }; const match = args.path.match(/(.*)(\.[mc]?js$)/); if (match) { const [, name, ext] = match; const tspath = name + ext.replace("js", "ts"); const tsfile = (0, import_paths.resolveAbsolutePath)(resolveDir, tspath); if ((0, import_paths.resolveFile)(tsfile)) { const newext = ext === ".mjs" ? mjs : ext === ".cjs" ? cjs : js; return { path: name + newext, external: true }; } } for (const ext of exts) { const fileName = `${args.path}${ext}`; const filePath = import_node_path.default.resolve(args.resolveDir, fileName); const isFile = await (0, import_fs.stat)(filePath).then((stat2) => stat2.isFile(), (error) => void error); if (isFile) return { path: `${args.path}${js}`, external: true }; } const dirPath = import_node_path.default.resolve(args.resolveDir, args.path); const isDir = await (0, import_fs.stat)(dirPath).then((stat2) => stat2.isDirectory(), (error) => void error); if (!isDir) return { external: true }; for (const ext of exts) { const fileName = import_node_path.default.join(args.path, `index${ext}`); const filePath = import_node_path.default.resolve(args.resolveDir, fileName); const isFile = await (0, import_fs.stat)(filePath).then((stat2) => stat2.isFile(), (error) => void error); if (isFile) return { path: `${args.path}/index${js}`, external: true }; } return { external: true }; }); } }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { fixExtensions }); //# sourceMappingURL=fix-extensions.cjs.map