From 6db096337178dd82cfd80336dc4440cbb4450399 Mon Sep 17 00:00:00 2001 From: parbez Date: Sun, 24 Jul 2022 17:10:37 +0530 Subject: [PATCH] fix: change polyfill package --- package.json | 2 +- src/lib/plugin.ts | 27 ++++++++++++++------------- yarn.lock | 45 +++++++++++++++++++++++++++++++-------------- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index c259bfe..09bff38 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,6 @@ "packageManager": "yarn@3.2.2", "prettier": "@sapphire/prettier-config", "dependencies": { - "rollup-plugin-polyfill-node": "^0.10.2" + "modern-node-polyfills": "^0.0.7" } } diff --git a/src/lib/plugin.ts b/src/lib/plugin.ts index fd86eeb..b583d44 100644 --- a/src/lib/plugin.ts +++ b/src/lib/plugin.ts @@ -1,8 +1,9 @@ -import fs from 'node:fs'; import path from 'node:path'; import type { OnResolveArgs, Plugin } from 'esbuild'; import type esbuild from 'esbuild'; -import { getModules as builtinsPolyfills } from 'rollup-plugin-polyfill-node/dist/modules.js'; +import { polyfillContent, polyfillPath } from 'modern-node-polyfills'; +import { builtinModules } from 'module'; + import { escapeRegex, commonJsTemplate, removeEndingSlash } from './utils/util'; const NAME = 'node-modules-polyfills'; @@ -19,8 +20,8 @@ export const nodeModulesPolyfillPlugin = (options: NodePolyfillsOptions = {}): P } const commonjsNamespace = `${namespace}-commonjs`; - const polyfilledBuiltins = builtinsPolyfills(); - const polyfilledBuiltinsNames = [...polyfilledBuiltins.keys()]; + // const polyfilledBuiltins = builtinsPolyfills(); + // const polyfilledBuiltinsNames = [...polyfilledBuiltins.keys()]; return { name, @@ -34,18 +35,17 @@ export const nodeModulesPolyfillPlugin = (options: NodePolyfillsOptions = {}): P const loader = async (args: esbuild.OnLoadArgs): Promise => { try { - const argsPath = args.path.replace(/^node:/, ''); const isCommonjs = args.namespace.endsWith('commonjs'); - const resolved = polyfilledBuiltins.get(removeEndingSlash(argsPath)) as string; - const contents = (await fs.promises.readFile(resolved)).toString(); + const resolved = await polyfillPath(removeEndingSlash(args.path)); + const contents = await polyfillContent(removeEndingSlash(args.path)); const resolveDir = path.dirname(resolved); if (isCommonjs) { return { loader: 'js', contents: commonJsTemplate({ - importPath: argsPath + importPath: args.path }), resolveDir }; @@ -65,12 +65,13 @@ export const nodeModulesPolyfillPlugin = (options: NodePolyfillsOptions = {}): P }; onLoad({ filter: /.*/, namespace }, loader); onLoad({ filter: /.*/, namespace: commonjsNamespace }, loader); - const filter = new RegExp([...polyfilledBuiltinsNames, ...polyfilledBuiltinsNames.map((n) => `node:${n}`)].map(escapeRegex).join('|')); - const resolver = (args: OnResolveArgs) => { - const argsPath = args.path.replace(/^node:/, ''); + const filter = new RegExp(`(?:node:)?${builtinModules.map(escapeRegex).join('|')}`); + const resolver = async (args: OnResolveArgs) => { const ignoreRequire = args.namespace === commonjsNamespace; - if (!polyfilledBuiltins.has(argsPath)) { + const pollyfill = await polyfillPath(args.path).catch(() => null); + + if (!pollyfill) { return; } @@ -78,7 +79,7 @@ export const nodeModulesPolyfillPlugin = (options: NodePolyfillsOptions = {}): P return { namespace: isCommonjs ? commonjsNamespace : namespace, - path: argsPath + path: args.path }; }; onResolve({ filter }, resolver); diff --git a/yarn.lock b/yarn.lock index 2f712bc..d4ba6ac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -379,6 +379,13 @@ __metadata: languageName: node linkType: hard +"@jspm/core@npm:2.0.0-beta.24": + version: 2.0.0-beta.24 + resolution: "@jspm/core@npm:2.0.0-beta.24" + checksum: c3a986d523a61522794c26b809d1fb40b1404a45a5661ba476b5d529e9c15c4c0dc51983c95168532bcc7455a387f0eceeed06f4b386aa219173535fa53a0a62 + languageName: node + linkType: hard + "@nodelib/fs.scandir@npm:2.1.5": version: 2.1.5 resolution: "@nodelib/fs.scandir@npm:2.1.5" @@ -426,7 +433,7 @@ __metadata: languageName: node linkType: hard -"@rollup/plugin-inject@npm:^4.0.0": +"@rollup/plugin-inject@npm:^4.0.4": version: 4.0.4 resolution: "@rollup/plugin-inject@npm:4.0.4" dependencies: @@ -2089,10 +2096,10 @@ __metadata: eslint-plugin-prettier: ^4.2.1 husky: ^8.0.1 lint-staged: ^13.0.3 + modern-node-polyfills: ^0.0.7 pinst: ^3.0.0 prettier: ^2.7.1 pretty-quick: ^3.1.3 - rollup-plugin-polyfill-node: ^0.10.2 tsup: ^6.1.3 typescript: ^4.7.4 languageName: unknown @@ -2225,7 +2232,7 @@ __metadata: languageName: node linkType: hard -"esbuild@npm:^0.14.49": +"esbuild@npm:^0.14.47, esbuild@npm:^0.14.49": version: 0.14.49 resolution: "esbuild@npm:0.14.49" dependencies: @@ -3587,6 +3594,13 @@ __metadata: languageName: node linkType: hard +"local-pkg@npm:^0.4.1": + version: 0.4.2 + resolution: "local-pkg@npm:0.4.2" + checksum: 22be451353c25c4411b552bf01880ebc9e995b93574b2facc7757968d888356df59199cacada14162ab53bbc9da055bb692c907b4171f008dbce45a2afc777c1 + languageName: node + linkType: hard + "locate-path@npm:^5.0.0": version: 5.0.0 resolution: "locate-path@npm:5.0.0" @@ -3981,6 +3995,20 @@ __metadata: languageName: node linkType: hard +"modern-node-polyfills@npm:^0.0.7": + version: 0.0.7 + resolution: "modern-node-polyfills@npm:0.0.7" + dependencies: + "@jspm/core": 2.0.0-beta.24 + "@rollup/plugin-inject": ^4.0.4 + acorn: ^8.7.1 + esbuild: ^0.14.47 + local-pkg: ^0.4.1 + rollup: ^2.75.7 + checksum: 27e6e37d283f5ef0b7c6bd8807c663a15187e0e1ae761348bc4ebdec00465978ecf513a56aeb35385c9808453a5441f6efb7f21520d4f6665145f04dafb8d73c + languageName: node + linkType: hard + "modify-values@npm:^1.0.0": version: 1.0.1 resolution: "modify-values@npm:1.0.1" @@ -4733,17 +4761,6 @@ __metadata: languageName: node linkType: hard -"rollup-plugin-polyfill-node@npm:^0.10.2": - version: 0.10.2 - resolution: "rollup-plugin-polyfill-node@npm:0.10.2" - dependencies: - "@rollup/plugin-inject": ^4.0.0 - peerDependencies: - rollup: ^1.20.0 || ^2.0.0 - checksum: 10806925ddf17d9a49bea885fa30296dec20ac59c43986c6f1eca892dd260cc75cc2f77b098dbcb8bbf503c10c7aa4d3078f441c1e2384ba475c105273132c99 - languageName: node - linkType: hard - "rollup@npm:^2.74.1": version: 2.75.1 resolution: "rollup@npm:2.75.1"