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
135 lines (134 loc) • 5.67 kB
JavaScript
var _a, _b, _c, _d;
import { createRequire } from "node:module";
import { fileURLToPath, pathToFileURL, URL } from "node:url";
import path, { dirname as path_dirname } from "node:path";
import { realpathSync } from "node:fs";
const pathResolve = (specifier, parentURL) => {
let baseUrl;
if (parentURL instanceof URL) {
baseUrl = parentURL;
}
else {
const parentHref = String(parentURL);
if (parentHref.startsWith("file:")) {
baseUrl = parentHref;
}
else {
baseUrl = pathToFileURL(parentHref).href;
}
}
return new URL(specifier, baseUrl).href;
};
export const import_meta_ponyfill_commonjs = /*@__PURE__*/ ((_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: pathToFileURL(module.filename).href,
main: require.main === module,
nodeResolve(specifier, parentURL = importMeta.url) {
return pathToFileURL((importMeta.url === parentURL
? require
: 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;
};
})()));
export const import_meta_ponyfill_esmodule = /*@__PURE__*/ ((_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 || realpathSync(filename) === 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 ?? fileURLToPath(im.url);
const dirname = im.dirname ?? path_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 = createRequire(im.url);
nodeResolve = (specifier, parentURL = im.url) => {
return pathToFileURL((importMeta.url === parentURL
? importMetaUrlRequire
: createRequire(parentURL)).resolve(specifier)).href;
};
return nodeResolve(specifier, parentURL);
};
let resolve = isSupportPathResolve
? im.resolve
: (specifier, parentURL) => {
if (/^[./]*\//.test(specifier) || path.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;
};
})()));
export { import_meta_ponyfill_esmodule as import_meta_ponyfill };
export default import_meta_ponyfill_esmodule;