UNPKG

import-meta-ponyfill

Version:

This library implements some standard interfaces of [import.meta](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta) with the aim of resolving the confusion caused by multiple standards in Node.js. It includes unified

173 lines (171 loc) 7.47 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var _a, _b, _c, _d; Object.defineProperty(exports, "__esModule", { value: true }); exports.import_meta_ponyfill = exports.import_meta_ponyfill_esmodule = exports.import_meta_ponyfill_commonjs = void 0; const node_module_1 = require("node:module"); const node_url_1 = require("node:url"); const node_path_1 = __importStar(require("node:path")); const node_fs_1 = require("node:fs"); const pathResolve = (specifier, parentURL) => { let baseUrl; if (parentURL instanceof node_url_1.URL) { baseUrl = parentURL; } else { const parentHref = String(parentURL); if (parentHref.startsWith("file:")) { baseUrl = parentHref; } else { baseUrl = (0, node_url_1.pathToFileURL)(parentHref).href; } } return new node_url_1.URL(specifier, baseUrl).href; }; exports.import_meta_ponyfill_commonjs = ((_a = globalThis)[_b = Symbol.for("import-meta-ponyfill-commonjs")] ?? (_a[_b] = (() => { const moduleImportMetaWM = new WeakMap(); return (require, module) => { let importMetaCache = moduleImportMetaWM.get(module); if (importMetaCache == null) { const importMeta = Object.assign(Object.create(null), { url: (0, node_url_1.pathToFileURL)(module.filename).href, main: require.main === module, nodeResolve(specifier, parentURL = importMeta.url) { return (0, node_url_1.pathToFileURL)((importMeta.url === parentURL ? require : (0, node_module_1.createRequire)(parentURL)).resolve(specifier)).href; }, resolve: function resolve(specifier, parentURL = importMeta.url) { if (/^[./]*\/.*/.test(specifier)) { return pathResolve(specifier, parentURL); } try { return importMeta.nodeResolve(specifier, parentURL); } catch { return pathResolve(specifier, parentURL); } }, filename: module.filename, dirname: module.path, }); moduleImportMetaWM.set(module, importMeta); importMetaCache = importMeta; } return importMetaCache; }; })())); exports.import_meta_ponyfill = exports.import_meta_ponyfill_commonjs; exports.import_meta_ponyfill_esmodule = ((_c = globalThis)[_d = Symbol.for("import-meta-ponyfill-esmodule")] ?? (_c[_d] = (() => { const importMetaWM = new WeakMap(); return (im) => { let importMetaCache = importMetaWM.get(im); if (importMetaCache == null) { const isMain = (filename) => { try { const entry = process.argv[1]; return (filename === entry || (0, node_fs_1.realpathSync)(filename) === (0, node_fs_1.realpathSync)(entry)); } catch { return false; } }; const resolveFunStr = String(im.resolve); /** * enable by --experimental-import-meta-resolve flag * v20.6.0, v18.19.0 * No longer behind --experimental-import-meta-resolve CLI flag, except for the non-standard parentURL parameter. */ const isSupportResolve = // v16.2.0+, v14.18.0+: Add support for WHATWG URL object to parentURL parameter. resolveFunStr !== "undefined" && /** * v20.0.0, v18.19.0 * This API now returns a string synchronously instead of a Promise. */ !resolveFunStr.startsWith("async"); const filename = im.filename ?? (0, node_url_1.fileURLToPath)(im.url); const dirname = im.dirname ?? (0, node_path_1.dirname)(filename); /** * v20.6.0, v18.19.0 * This API no longer throws when targeting file: URLs that do not map to an existing file on the local FS. */ let isSupportPathResolve = false; if (isSupportResolve) { try { im.resolve(`./${Date.now()}${Math.random()}`); isSupportPathResolve = true; } catch { } } let nodeResolve = isSupportResolve ? im.resolve : (specifier, parentURL) => { const importMetaUrlRequire = (0, node_module_1.createRequire)(im.url); nodeResolve = (specifier, parentURL = im.url) => { return (0, node_url_1.pathToFileURL)((importMeta.url === parentURL ? importMetaUrlRequire : (0, node_module_1.createRequire)(parentURL)).resolve(specifier)).href; }; return nodeResolve(specifier, parentURL); }; let resolve = isSupportPathResolve ? im.resolve : (specifier, parentURL) => { if (/^[./]*\//.test(specifier) || node_path_1.default.isAbsolute(specifier)) { return pathResolve(specifier, parentURL ?? im.url); } return nodeResolve(specifier, parentURL); }; const importMeta = { ...im, url: im.url, main: im.main ?? isMain(filename), filename, dirname, get nodeResolve() { return nodeResolve; }, resolve: resolve, }; Object.setPrototypeOf(importMeta, null); importMetaCache = importMeta; importMetaWM.set(im, importMeta); } return importMetaCache; }; })())); exports.default = exports.import_meta_ponyfill_commonjs; module.exports = Object.assign(exports.import_meta_ponyfill_commonjs, exports);