Socket
Socket
Sign inDemoInstall

@esbuild-plugins/node-modules-polyfill

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@esbuild-plugins/node-modules-polyfill - npm Package Compare versions

Comparing version 0.1.4 to 0.2.0

2

dist/index.d.ts

@@ -1,2 +0,2 @@

import { Plugin } from 'esbuild';
import type { Plugin } from 'esbuild';
export interface NodePolyfillsOptions {

@@ -3,0 +3,0 @@ name?: string;

@@ -17,6 +17,4 @@ "use strict";

const escape_string_regexp_1 = __importDefault(require("escape-string-regexp"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const polyfills_1 = require("./polyfills");
// import { NodeResolvePlugin } from '@esbuild-plugins/node-resolve'
const modules_1 = require("rollup-plugin-polyfill-node/dist/modules");
const NAME = 'node-modules-polyfills';

@@ -37,3 +35,3 @@ const NAMESPACE = NAME;

const commonjsNamespace = namespace + '-commonjs';
const polyfilledBuiltins = polyfills_1.builtinsPolyfills();
const polyfilledBuiltins = modules_1.getModules();
const polyfilledBuiltinsNames = [...polyfilledBuiltins.keys()];

@@ -55,6 +53,7 @@ return {

try {
const argsPath = args.path.replace(/^node:/, '');
const isCommonjs = args.namespace.endsWith('commonjs');
const resolved = polyfilledBuiltins.get(removeEndingSlash(args.path));
const contents = yield (yield fs_1.default.promises.readFile(resolved)).toString();
let resolveDir = path_1.default.dirname(resolved);
const key = removeEndingSlash(argsPath);
const contents = polyfilledBuiltins.get(key);
const resolveDir = path_1.default.dirname(key);
if (isCommonjs) {

@@ -64,3 +63,3 @@ return {

contents: commonJsTemplate({
importPath: args.path,
importPath: argsPath,
}),

@@ -87,7 +86,13 @@ resolveDir,

onLoad({ filter: /.*/, namespace: commonjsNamespace }, loader);
const filter = new RegExp(polyfilledBuiltinsNames.map(escape_string_regexp_1.default).join('|'));
const filter = new RegExp([
...polyfilledBuiltinsNames,
...polyfilledBuiltinsNames.map((n) => `node:${n}`),
]
.map(escape_string_regexp_1.default)
.join('|'));
function resolver(args) {
return __awaiter(this, void 0, void 0, function* () {
const argsPath = args.path.replace(/^node:/, '');
const ignoreRequire = args.namespace === commonjsNamespace;
if (!polyfilledBuiltins.has(args.path)) {
if (!polyfilledBuiltins.has(argsPath)) {
return;

@@ -98,3 +103,3 @@ }

namespace: isCommonjs ? commonjsNamespace : namespace,
path: args.path,
path: argsPath,
};

@@ -101,0 +106,0 @@ });

@@ -1,2 +0,2 @@

import { Plugin } from 'esbuild';
import type { Plugin } from 'esbuild';
export interface NodePolyfillsOptions {

@@ -3,0 +3,0 @@ name?: string;

@@ -11,6 +11,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

import escapeStringRegexp from 'escape-string-regexp';
import fs from 'fs';
import path from 'path';
import { builtinsPolyfills } from './polyfills';
// import { NodeResolvePlugin } from '@esbuild-plugins/node-resolve'
import { getModules as builtinsPolyfills } from 'rollup-plugin-polyfill-node/dist/modules';
const NAME = 'node-modules-polyfills';

@@ -39,6 +37,6 @@ const NAMESPACE = NAME;

if ((initialOptions === null || initialOptions === void 0 ? void 0 : initialOptions.define) && !((_a = initialOptions.define) === null || _a === void 0 ? void 0 : _a.global)) {
initialOptions.define['global'] = '{}';
initialOptions.define['global'] = 'globalThis';
}
else if (!(initialOptions === null || initialOptions === void 0 ? void 0 : initialOptions.define)) {
initialOptions.define = { global: '{}' };
initialOptions.define = { global: 'globalThis' };
}

@@ -49,6 +47,7 @@ // TODO these polyfill module cannot import anything, is that ok?

try {
const argsPath = args.path.replace(/^node:/, '');
const isCommonjs = args.namespace.endsWith('commonjs');
const resolved = polyfilledBuiltins.get(removeEndingSlash(args.path));
const contents = yield (yield fs.promises.readFile(resolved)).toString();
let resolveDir = path.dirname(resolved);
const key = removeEndingSlash(argsPath);
const contents = polyfilledBuiltins.get(key);
const resolveDir = path.dirname(key);
if (isCommonjs) {

@@ -58,3 +57,3 @@ return {

contents: commonJsTemplate({
importPath: args.path,
importPath: argsPath,
}),

@@ -81,7 +80,13 @@ resolveDir,

onLoad({ filter: /.*/, namespace: commonjsNamespace }, loader);
const filter = new RegExp(polyfilledBuiltinsNames.map(escapeStringRegexp).join('|'));
const filter = new RegExp([
...polyfilledBuiltinsNames,
...polyfilledBuiltinsNames.map((n) => `node:${n}`),
]
.map(escapeStringRegexp)
.join('|'));
function resolver(args) {
return __awaiter(this, void 0, void 0, function* () {
const argsPath = args.path.replace(/^node:/, '');
const ignoreRequire = args.namespace === commonjsNamespace;
if (!polyfilledBuiltins.has(args.path)) {
if (!polyfilledBuiltins.has(argsPath)) {
return;

@@ -92,3 +97,3 @@ }

namespace: isCommonjs ? commonjsNamespace : namespace,
path: args.path,
path: argsPath,
};

@@ -95,0 +100,0 @@ });

@@ -14,2 +14,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

import NodeModulesPolyfillsPlugin from '.';
import NodeGlobalsPolyfillsPlugin from '@esbuild-plugins/node-globals-polyfill';
require('debug').enable(require('../package.json').name);

@@ -154,3 +155,2 @@ test('works', () => __awaiter(void 0, void 0, void 0, function* () {

eval(text);
console.log(text);
expect(text).not.toContain(/\bglobal\b/);

@@ -160,2 +160,22 @@ // console.log(res.outputFiles[0].text)

}));
test('works with globals polyfills', () => __awaiter(void 0, void 0, void 0, function* () {
const { unlink, paths: [ENTRY], } = yield writeFiles({
'entry.ts': `import {x} from './utils'; console.log(x);`,
'utils.ts': `import path from 'path'; import { Buffer } from 'buffer'; export const x = path.resolve(Buffer.from('x').toString());`,
});
// const outfile = randomOutputFile()
const res = yield build({
entryPoints: [ENTRY],
write: false,
format: 'esm',
target: 'es2017',
bundle: true,
plugins: [NodeModulesPolyfillsPlugin(), NodeGlobalsPolyfillsPlugin()],
});
const text = res.outputFiles[0].text;
eval(text);
console.log(text);
// console.log(res.outputFiles[0].text)
unlink();
}));
//# sourceMappingURL=index.test.js.map
{
"name": "@esbuild-plugins/node-modules-polyfill",
"version": "0.1.4",
"version": "0.2.0",
"description": "",

@@ -22,2 +22,3 @@ "main": "dist/index.js",

"devDependencies": {
"rollup": "^2.75.7",
"safe-buffer": "^5.2.1",

@@ -28,3 +29,3 @@ "test-support": "*"

"escape-string-regexp": "^4.0.0",
"rollup-plugin-node-polyfills": "^0.2.1"
"rollup-plugin-polyfill-node": "^0.12.0"
},

@@ -31,0 +32,0 @@ "peerDependencies": {

@@ -1,9 +0,7 @@

import { OnResolveArgs, Plugin } from 'esbuild'
import type { OnResolveArgs, Plugin } from 'esbuild'
import esbuild from 'esbuild'
import escapeStringRegexp from 'escape-string-regexp'
import fs from 'fs'
import path from 'path'
import esbuild from 'esbuild'
import { builtinsPolyfills } from './polyfills'
import { getModules as builtinsPolyfills } from 'rollup-plugin-polyfill-node/dist/modules'
// import { NodeResolvePlugin } from '@esbuild-plugins/node-resolve'
const NAME = 'node-modules-polyfills'

@@ -51,11 +49,8 @@ const NAMESPACE = NAME

try {
const argsPath = args.path.replace(/^node:/, '')
const isCommonjs = args.namespace.endsWith('commonjs')
const resolved = polyfilledBuiltins.get(
removeEndingSlash(args.path),
)
const contents = await (
await fs.promises.readFile(resolved)
).toString()
let resolveDir = path.dirname(resolved)
const key = removeEndingSlash(argsPath)
const contents = polyfilledBuiltins.get(key)
const resolveDir = path.dirname(key)

@@ -66,3 +61,3 @@ if (isCommonjs) {

contents: commonJsTemplate({
importPath: args.path,
importPath: argsPath,
}),

@@ -88,8 +83,14 @@ resolveDir,

const filter = new RegExp(
polyfilledBuiltinsNames.map(escapeStringRegexp).join('|'), // TODO builtins could end with slash, keep in mind in regex
[
...polyfilledBuiltinsNames,
...polyfilledBuiltinsNames.map((n) => `node:${n}`),
]
.map(escapeStringRegexp)
.join('|'), // TODO builtins could end with slash, keep in mind in regex
)
async function resolver(args: OnResolveArgs) {
const argsPath = args.path.replace(/^node:/, '')
const ignoreRequire = args.namespace === commonjsNamespace
if (!polyfilledBuiltins.has(args.path)) {
if (!polyfilledBuiltins.has(argsPath)) {
return

@@ -103,3 +104,3 @@ }

namespace: isCommonjs ? commonjsNamespace : namespace,
path: args.path,
path: argsPath,
}

@@ -106,0 +107,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc