// eslint-disable-next-line @typescript-eslint/ban-ts-comment import inject from "@rollup/plugin-inject"; import { dirname, relative, join, resolve } from "path"; import type { FilterPattern } from "@rollup/pluginutils"; // @ts-ignore import POLYFILLS from "rollup-plugin-polyfill-node/dist/es/polyfills"; import type { InputPluginOption } from "rollup"; // @ts-ignore import { v4 as uuid } from "uuid"; import { getModules, Polyfill, PolyfillFilter } from "./modules"; export type PolyFillOptions = { exclude?: FilterPattern; sourceMap?: boolean; baseDir?: string; polyfillFilter?: Partial; // When rollup used to bundle a package named after a node builtin, // it shouldn't be treated as a polyfill. // resolveEntryPoints can be used to determine what goes in the bundle of such packages. // If resolveEntryPoints == true, invoking rollup on a package named 'buffer' will resolve // to the polyfill and not the package on disk. resolveEntryPoints?: boolean; }; function polyfill(opts: PolyFillOptions = {}): InputPluginOption { const libs = getModules(opts.polyfillFilter); const PREFIX = `\0polyfill-node.`; const PREFIX_LENGTH = PREFIX.length; const injectPlugin = inject({ include: "**/*.js", exclude: opts.exclude, sourceMap: opts.sourceMap, modules: { process: PREFIX + "process", console: PREFIX + "console", Buffer: [PREFIX + "buffer", "Buffer"], global: PREFIX + "global", __filename: FILENAME_PATH, __dirname: DIRNAME_PATH, }, }); const basedir = opts.baseDir || "/"; const dirs = new Map(); return { name: "node-polyfills", resolveId( importee: string, importer: string | undefined, options?: { isEntry?: boolean }, ) { if (opts.resolveEntryPoints === false && options?.isEntry) return null; // Fixes commonjs compatability: https://github.com/FredKSchott/rollup-plugin-polyfill-node/pull/42 if (importee[0] == "\0" && /\?commonjs-\w+$/.test(importee)) { importee = importee.slice(1).replace(/\?commonjs-\w+$/, ""); } if (importee === DIRNAME_PATH && importer) { const id = getRandomId(); dirs.set(id, dirname("/" + relative(basedir, importer))); return { id, moduleSideEffects: false }; } if (importee === FILENAME_PATH && importer) { const id = getRandomId(); dirs.set(id, dirname("/" + relative(basedir, importer))); return { id, moduleSideEffects: false }; } if (importer && importer.startsWith(PREFIX) && importee.startsWith(".")) { importee = PREFIX + join( importer.substring(PREFIX_LENGTH).replace(".js", ""), "..", importee, ) + ".js"; } if (importee.startsWith(PREFIX)) { importee = importee.substring(PREFIX_LENGTH); } if (importee.startsWith("node:")) { importee = importee.substring("node:".length); } if ( libs.has(importee as Polyfill) || POLYFILLS[importee.replace(".js", "") + ".js"] ) { return { id: PREFIX + importee.replace(".js", "") + ".js", moduleSideEffects: false, }; } return null; }, load(id: string) { if (dirs.has(id)) { return `export default '${dirs.get(id)}'`; } if (id.startsWith(PREFIX)) { const importee = id.substring(PREFIX_LENGTH).replace(".js", ""); return libs.get(importee as Polyfill) || POLYFILLS[importee + ".js"]; } }, transform(code: string, id: string) { if (id === PREFIX + "global.js") return; // @ts-ignore return injectPlugin.transform.call( this, code, id.replace(PREFIX, resolve("node_modules", "polyfill-node")), ); }, }; } function getRandomId() { return uuid().replaceAll("-", ""); } const DIRNAME_PATH = "\0node-polyfills:dirname"; const FILENAME_PATH = "\0node-polyfills:filename"; export default polyfill;