UNPKG

esbuild-shim-plugin

Version:

A plugin for esbuild to shim imports for commonjs and esm modules.

69 lines (67 loc) 2.59 kB
"use strict"; 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); // index.ts var index_exports = {}; __export(index_exports, { default: () => index_default, shimPlugin: () => shimPlugin }); module.exports = __toCommonJS(index_exports); var shimPlugin = () => ({ name: "shimPlugin", setup(build) { const options = build.initialOptions; const format = (options?.define ?? {})["TSUP_FORMAT"]?.toLowerCase().replaceAll('"', "") ?? options.format; if (!format) { throw new Error(`options.format needs to be defined in order to use plugin`); } if (format !== "esm" && format !== "cjs") { throw new TypeError(`options.format must be either "esm" or "cjs". Got "${format}"`); } if (format === "esm") { options.banner = { js: ` const Module = await import('node:module'); const require = /* @__PURE__ */ (Module?.default?.createRequire ?? Module?.createRequire)(import.meta.url); const __filename = /* @__PURE__ */ (await import("node:url")).fileURLToPath(import.meta.url); const __dirname = /* @__PURE__ */ (await import("node:path")).dirname(__filename);` }; } if (format === "cjs") { options.banner = { js: ` const importMetaUrl = /* @__PURE__ */ require("node:url").pathToFileURL(__filename).toString(); const importMetaFilename = /* @__PURE__ */ __filename; const importMetaDirname = /* @__PURE__ */ __dirname; const importMetaResolve = /* @__PURE__ */ require.resolve;` }; options.define = { "import.meta.url": "importMetaUrl", "import.meta.filename": "importMetaFilename", "import.meta.dirname": "importMetaDirname", "import.meta.resolve": "importMetaResolve" }; } } }); var index_default = shimPlugin; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { shimPlugin });