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
119 lines (117 loc) • 5.53 kB
JavaScript
;
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 = 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 = (Reflect.get(globalThis, Symbol.for("import-meta-ponyfill-commonjs")) ??
(() => {
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 = (Reflect.get(globalThis, Symbol.for("import-meta-ponyfill-esmodule")) ??
((importMeta) => {
const resolveFunStr = String(importMeta.resolve);
const importMetaWM = new WeakMap();
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 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");
// enable by --experimental-import-meta-resolve flag
exports.import_meta_ponyfill_esmodule = (im) => {
let importMetaCache = importMetaWM.get(im);
if (importMetaCache == null) {
const filename = im.filename ?? (0, node_url_1.fileURLToPath)(im.url);
const dirname = im.dirname ?? (0, node_path_1.dirname)(filename);
const importMeta = {
url: im.url,
main: im.main ?? isMain(filename),
filename,
dirname,
nodeResolve: isSupportResolve
? im.resolve
: (() => {
const importMetaUrlRequire = (0, node_module_1.createRequire)(im.url);
return (specifier, parentURL = im.url) => {
return (0, node_url_1.pathToFileURL)((importMeta.url === parentURL
? importMetaUrlRequire
: (0, node_module_1.createRequire)(parentURL)).resolve(specifier)).href;
};
})(),
resolve: function resolve(specifier, parentURL = im.url) {
if (/^[./]*\/.*/.test(specifier)) {
return pathResolve(specifier, parentURL);
}
try {
return importMeta.nodeResolve(specifier, parentURL);
}
catch {
return pathResolve(specifier, parentURL);
}
},
};
importMetaCache = importMeta;
importMetaWM.set(im, importMeta);
}
return importMetaCache;
};
return (0, exports.import_meta_ponyfill_esmodule)(importMeta);
}));
exports.default = exports.import_meta_ponyfill_commonjs;
module.exports = Object.assign(exports.import_meta_ponyfill_commonjs, exports);