@esbuild-plugins/node-modules-polyfill
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
66371
3
27
1028
16
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@rollup/plugin-inject@5.0.5(transitive)
+ Added@rollup/pluginutils@5.1.0(transitive)
+ Added@types/estree@1.0.5(transitive)
+ Addedestree-walker@2.0.2(transitive)
+ Addedfsevents@2.3.3(transitive)
+ Addedmagic-string@0.30.11(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedrollup@3.29.4(transitive)
+ Addedrollup-plugin-polyfill-node@0.12.0(transitive)
- Removedrollup-plugin-node-polyfills@^0.2.1
- Removedestree-walker@0.6.1(transitive)
- Removedmagic-string@0.25.9(transitive)
- Removedrollup-plugin-inject@3.0.2(transitive)
- Removedrollup-plugin-node-polyfills@0.2.1(transitive)
- Removedrollup-pluginutils@2.8.2(transitive)
- Removedsourcemap-codec@1.4.8(transitive)