Skip to content

Commit

Permalink
fix: change polyfill package
Browse files Browse the repository at this point in the history
  • Loading branch information
imranbarbhuiya committed Jul 24, 2022
1 parent d79a448 commit 6db0963
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
27 changes: 14 additions & 13 deletions src/lib/plugin.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -34,18 +35,17 @@ export const nodeModulesPolyfillPlugin = (options: NodePolyfillsOptions = {}): P

const loader = async (args: esbuild.OnLoadArgs): Promise<esbuild.OnLoadResult> => {
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
};
Expand All @@ -65,20 +65,21 @@ 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;
}

const isCommonjs = !ignoreRequire && args.kind === 'require-call';

return {
namespace: isCommonjs ? commonjsNamespace : namespace,
path: argsPath
path: args.path
};
};
onResolve({ filter }, resolver);
Expand Down
45 changes: 31 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 6db0963

Please sign in to comment.