@esbuild-plugins/node-globals-polyfill
Advanced tools
Comparing version 0.0.5 to 0.1.0
# @esbuild-plugins/node-globals-polyfill | ||
## 0.1.0 | ||
### Minor Changes | ||
- Added plugin interface that changes initialOptions.inject, dedupe polyfills injected | ||
## 0.0.5 | ||
@@ -4,0 +10,0 @@ |
@@ -1,2 +0,8 @@ | ||
export {}; | ||
import * as esbuild from 'esbuild'; | ||
export declare function NodeGlobalsPolyfillPlugin({ buffer, define, process, }?: { | ||
buffer?: boolean | undefined; | ||
define?: {} | undefined; | ||
process?: boolean | undefined; | ||
}): esbuild.Plugin; | ||
export default NodeGlobalsPolyfillPlugin; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spread = (this && this.__spread) || function () { | ||
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.NodeGlobalsPolyfillPlugin = void 0; | ||
var path_1 = __importDefault(require("path")); | ||
var fs_1 = __importDefault(require("fs")); | ||
function NodeGlobalsPolyfillPlugin(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.buffer, buffer = _c === void 0 ? false : _c, _d = _b.define, define = _d === void 0 ? {} : _d, _e = _b.process, process = _e === void 0 ? true : _e; | ||
return { | ||
name: 'node-globals-polyfill', | ||
setup: function (_a) { | ||
var _b; | ||
var initialOptions = _a.initialOptions, onResolve = _a.onResolve, onLoad = _a.onLoad; | ||
onResolve({ filter: /_node-buffer-polyfill_\.js/ }, function (arg) { | ||
return { | ||
path: path_1.default.resolve(__dirname, '../Buffer.js'), | ||
}; | ||
}); | ||
onResolve({ filter: /_node-process-polyfill_\.js/ }, function (arg) { | ||
return { | ||
path: path_1.default.resolve(__dirname, '../process.js'), | ||
}; | ||
}); | ||
onLoad({ filter: /_virtual-process-polyfill_\.js/ }, function (arg) { | ||
var data = fs_1.default | ||
.readFileSync(path_1.default.resolve(__dirname, '../process.js')) | ||
.toString(); | ||
var keys = Object.keys(define); | ||
return { | ||
loader: 'js', | ||
contents: data.replace("const defines = {}", 'const defines = {\n' + | ||
keys | ||
.filter(function (x) { return x.startsWith('process.'); }) | ||
.sort(function (a, b) { return a.length - b.length; }) | ||
.map(function (k) { | ||
return " " + JSON.stringify(k).replace('process.', '') + ": " + define[k] + ","; | ||
}) | ||
.join('\n') + | ||
'\n}'), | ||
}; | ||
}); | ||
var polyfills = []; | ||
if (process) { | ||
polyfills.push(path_1.default.resolve(__dirname, '../_virtual-process-polyfill_.js')); | ||
} | ||
if (buffer) { | ||
polyfills.push(path_1.default.resolve(__dirname, '../_buffer.js')); | ||
} | ||
if (initialOptions.inject) { | ||
(_b = initialOptions.inject).push.apply(_b, __spread(polyfills)); | ||
} | ||
else { | ||
initialOptions.inject = __spread(polyfills); | ||
} | ||
}, | ||
}; | ||
} | ||
exports.NodeGlobalsPolyfillPlugin = NodeGlobalsPolyfillPlugin; | ||
exports.default = NodeGlobalsPolyfillPlugin; | ||
//# sourceMappingURL=index.js.map |
@@ -57,4 +57,5 @@ "use strict"; | ||
var test_support_1 = require("test-support"); | ||
var _1 = require("."); | ||
require('debug').enable(require('../package.json').name); | ||
test('works', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
test('process works', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, _b, ENTRY, res, output; | ||
@@ -86,2 +87,167 @@ return __generator(this, function (_c) { | ||
}); }); | ||
test('process is tree shaken', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, _b, ENTRY, res, output; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'entry.ts': "console.log('hei')", | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
inject: [require.resolve('../process')], | ||
})]; | ||
case 2: | ||
res = _c.sent(); | ||
output = res.outputFiles[0].text; | ||
expect(output).not.toContain('process'); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
test('process env vars are replaced with ones from define', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, _b, ENTRY, res, output; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'entry.ts': "if (process.env.VAR !== 'hello') { throw new Error('process.env.VAR not right: ' + process.env.VAR) }", | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [ | ||
_1.NodeGlobalsPolyfillPlugin({ | ||
define: { | ||
'process.env.VAR': '"hello"', | ||
}, | ||
}), | ||
], | ||
})]; | ||
case 2: | ||
res = _c.sent(); | ||
output = res.outputFiles[0].text; | ||
eval(output); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
test('Buffer works', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, _b, ENTRY, res, output; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'entry.ts': "console.log(Buffer.from('xxx').toString())", | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
inject: [require.resolve('../Buffer')], | ||
})]; | ||
case 2: | ||
res = _c.sent(); | ||
output = res.outputFiles[0].text; | ||
// console.log(output) | ||
eval(output); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
test('Buffer is tree shaken', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, _b, ENTRY, res, output; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'entry.ts': "console.log('hei')", | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
inject: [require.resolve('../Buffer')], | ||
})]; | ||
case 2: | ||
res = _c.sent(); | ||
output = res.outputFiles[0].text; | ||
expect(output).not.toContain('Buffer'); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
test('Buffer works using plugin', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, _b, ENTRY, res, output; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'entry.ts': "\n let buf = new Buffer(256);\n let len = buf.write(\"Simply Easy Learning\");\n console.log(\"Octets written : \"+ len);", | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [_1.NodeGlobalsPolyfillPlugin({ buffer: true })], | ||
})]; | ||
case 2: | ||
res = _c.sent(); | ||
output = res.outputFiles[0].text; | ||
// console.log(output) | ||
eval(output); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
test('process works using plugin', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, _b, ENTRY, res, output; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'entry.ts': "console.log(process.cwd())", | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [_1.NodeGlobalsPolyfillPlugin({ process: true })], | ||
})]; | ||
case 2: | ||
res = _c.sent(); | ||
output = res.outputFiles[0].text; | ||
// console.log(output) | ||
eval(output); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
//# sourceMappingURL=index.test.js.map |
{ | ||
"name": "@esbuild-plugins/node-globals-polyfill", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "", | ||
@@ -19,3 +19,6 @@ "preferUnplugged": true, | ||
"esm", | ||
"process.js" | ||
"Buffer.js", | ||
"process.js", | ||
"_buffer.js", | ||
"_process.js" | ||
], | ||
@@ -28,6 +31,3 @@ "keywords": [], | ||
}, | ||
"dependencies": { | ||
"debug": "^4.3.1", | ||
"rollup-plugin-node-polyfills": "^0.2.1" | ||
}, | ||
"dependencies": {}, | ||
"peerDependencies": { | ||
@@ -34,0 +34,0 @@ "esbuild": "*" |
@@ -205,3 +205,3 @@ // shim for using process in browser | ||
export const process = { | ||
export var process = { | ||
nextTick: nextTick, | ||
@@ -231,1 +231,17 @@ title: title, | ||
} | ||
// replace process.env.VAR with define | ||
const defines = {} | ||
Object.keys(defines).forEach((key) => { | ||
const segs = key.split('.') | ||
let target = process | ||
for (let i = 0; i < segs.length; i++) { | ||
const seg = segs[i] | ||
if (i === segs.length - 1) { | ||
target[seg] = defines[key] | ||
} else { | ||
target = target[seg] || (target[seg] = {}) | ||
} | ||
} | ||
}) |
import { build } from 'esbuild' | ||
import { writeFiles } from 'test-support' | ||
import { NodeGlobalsPolyfillPlugin } from '.' | ||
require('debug').enable(require('../package.json').name) | ||
test('works', async () => { | ||
test('process works', async () => { | ||
const { | ||
@@ -26,1 +27,131 @@ unlink, | ||
}) | ||
test('process is tree shaken', async () => { | ||
const { | ||
unlink, | ||
paths: [ENTRY], | ||
} = await writeFiles({ | ||
'entry.ts': `console.log('hei')`, | ||
}) | ||
const res = await build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
inject: [require.resolve('../process')], | ||
}) | ||
const output = res.outputFiles[0].text | ||
expect(output).not.toContain('process') | ||
unlink() | ||
}) | ||
test('process env vars are replaced with ones from define', async () => { | ||
const { | ||
unlink, | ||
paths: [ENTRY], | ||
} = await writeFiles({ | ||
'entry.ts': `if (process.env.VAR !== 'hello') { throw new Error('process.env.VAR not right: ' + process.env.VAR) }`, | ||
}) | ||
const res = await build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [ | ||
NodeGlobalsPolyfillPlugin({ | ||
define: { | ||
'process.env.VAR': '"hello"', | ||
}, | ||
}), | ||
], | ||
}) | ||
const output = res.outputFiles[0].text | ||
eval(output) | ||
unlink() | ||
}) | ||
test('Buffer works', async () => { | ||
const { | ||
unlink, | ||
paths: [ENTRY], | ||
} = await writeFiles({ | ||
'entry.ts': `console.log(Buffer.from('xxx').toString())`, | ||
}) | ||
const res = await build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
inject: [require.resolve('../Buffer')], | ||
}) | ||
const output = res.outputFiles[0].text | ||
// console.log(output) | ||
eval(output) | ||
unlink() | ||
}) | ||
test('Buffer is tree shaken', async () => { | ||
const { | ||
unlink, | ||
paths: [ENTRY], | ||
} = await writeFiles({ | ||
'entry.ts': `console.log('hei')`, | ||
}) | ||
const res = await build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
inject: [require.resolve('../Buffer')], | ||
}) | ||
const output = res.outputFiles[0].text | ||
expect(output).not.toContain('Buffer') | ||
unlink() | ||
}) | ||
test('Buffer works using plugin', async () => { | ||
const { | ||
unlink, | ||
paths: [ENTRY], | ||
} = await writeFiles({ | ||
'entry.ts': ` | ||
let buf = new Buffer(256); | ||
let len = buf.write("Simply Easy Learning"); | ||
console.log("Octets written : "+ len);`, | ||
}) | ||
const res = await build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [NodeGlobalsPolyfillPlugin({ buffer: true })], | ||
}) | ||
const output = res.outputFiles[0].text | ||
// console.log(output) | ||
eval(output) | ||
unlink() | ||
}) | ||
test('process works using plugin', async () => { | ||
const { | ||
unlink, | ||
paths: [ENTRY], | ||
} = await writeFiles({ | ||
'entry.ts': `console.log(process.cwd())`, | ||
}) | ||
const res = await build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [NodeGlobalsPolyfillPlugin({ process: true })], | ||
}) | ||
const output = res.outputFiles[0].text | ||
// console.log(output) | ||
eval(output) | ||
unlink() | ||
}) |
@@ -0,3 +1,69 @@ | ||
import path from 'path' | ||
import fs from 'fs' | ||
import * as esbuild from 'esbuild' | ||
// TODO make plugin once esbuild config modifiable from plugin | ||
export {} | ||
export function NodeGlobalsPolyfillPlugin({ | ||
buffer = false, | ||
define = {}, | ||
process = true, | ||
} = {}): esbuild.Plugin { | ||
return { | ||
name: 'node-globals-polyfill', | ||
setup({ initialOptions, onResolve, onLoad }) { | ||
onResolve({ filter: /_node-buffer-polyfill_\.js/ }, (arg) => { | ||
return { | ||
path: path.resolve(__dirname, '../Buffer.js'), | ||
} | ||
}) | ||
onResolve({ filter: /_node-process-polyfill_\.js/ }, (arg) => { | ||
return { | ||
path: path.resolve(__dirname, '../process.js'), | ||
} | ||
}) | ||
onLoad({ filter: /_virtual-process-polyfill_\.js/ }, (arg) => { | ||
const data = fs | ||
.readFileSync(path.resolve(__dirname, '../process.js')) | ||
.toString() | ||
const keys = Object.keys(define) | ||
return { | ||
loader: 'js', | ||
contents: data.replace( | ||
`const defines = {}`, | ||
'const defines = {\n' + | ||
keys | ||
.filter((x) => x.startsWith('process.')) | ||
.sort((a, b) => a.length - b.length) | ||
.map( | ||
(k) => | ||
` ${JSON.stringify(k).replace( | ||
'process.', | ||
'', | ||
)}: ${define[k]},`, | ||
) | ||
.join('\n') + | ||
'\n}', | ||
), | ||
} | ||
}) | ||
const polyfills: string[] = [] | ||
if (process) { | ||
polyfills.push( | ||
path.resolve(__dirname, '../_virtual-process-polyfill_.js'), | ||
) | ||
} | ||
if (buffer) { | ||
polyfills.push(path.resolve(__dirname, '../_buffer.js')) | ||
} | ||
if (initialOptions.inject) { | ||
initialOptions.inject.push(...polyfills) | ||
} else { | ||
initialOptions.inject = [...polyfills] | ||
} | ||
}, | ||
} | ||
} | ||
export default NodeGlobalsPolyfillPlugin |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
102008
1
24
2753
2
7
- Removeddebug@^4.3.1
- Removedrollup-plugin-node-polyfills@^0.2.1
- Removeddebug@4.3.7(transitive)
- Removedestree-walker@0.6.1(transitive)
- Removedmagic-string@0.25.9(transitive)
- Removedms@2.1.3(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)