@esbuild-plugins/node-modules-polyfill
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -40,3 +40,11 @@ "use strict"; | ||
name, | ||
setup: function setup({ onLoad, onResolve }) { | ||
setup: function setup({ onLoad, onResolve, initialOptions }) { | ||
var _a; | ||
// polyfills contain global keyword, it must be defined | ||
if ((initialOptions === null || initialOptions === void 0 ? void 0 : initialOptions.define) && !((_a = initialOptions.define) === null || _a === void 0 ? void 0 : _a.global)) { | ||
initialOptions.define['global'] = '{}'; | ||
} | ||
else if (!(initialOptions === null || initialOptions === void 0 ? void 0 : initialOptions.define)) { | ||
initialOptions.define = { global: '{}' }; | ||
} | ||
// TODO these polyfill module cannot import anything, is that ok? | ||
@@ -43,0 +51,0 @@ function loader(args) { |
@@ -142,2 +142,23 @@ "use strict"; | ||
})); | ||
test('does not include global keyword', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const { unlink, paths: [ENTRY], } = yield test_support_1.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 esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [_1.default()], | ||
}); | ||
const text = res.outputFiles[0].text; | ||
eval(text); | ||
console.log(text); | ||
expect(text).not.toContain(/\bglobal\b/); | ||
// console.log(res.outputFiles[0].text) | ||
unlink(); | ||
})); | ||
//# sourceMappingURL=index.test.js.map |
@@ -34,3 +34,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
name, | ||
setup: function setup({ onLoad, onResolve }) { | ||
setup: function setup({ onLoad, onResolve, initialOptions }) { | ||
var _a; | ||
// polyfills contain global keyword, it must be defined | ||
if ((initialOptions === null || initialOptions === void 0 ? void 0 : initialOptions.define) && !((_a = initialOptions.define) === null || _a === void 0 ? void 0 : _a.global)) { | ||
initialOptions.define['global'] = '{}'; | ||
} | ||
else if (!(initialOptions === null || initialOptions === void 0 ? void 0 : initialOptions.define)) { | ||
initialOptions.define = { global: '{}' }; | ||
} | ||
// TODO these polyfill module cannot import anything, is that ok? | ||
@@ -95,2 +103,5 @@ function loader(args) { | ||
module.exports = polyfill.default | ||
for (let k in polyfill) { | ||
module.exports[k] = polyfill[k] | ||
} | ||
} else if (polyfill) { | ||
@@ -100,7 +111,3 @@ module.exports = polyfill | ||
if (polyfill) { | ||
for (let k in polyfill) { | ||
module.exports[k] = polyfill[k] | ||
} | ||
} | ||
`; | ||
@@ -107,0 +114,0 @@ } |
@@ -12,2 +12,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { writeFiles } from 'test-support'; | ||
import fs from 'fs'; | ||
import NodeModulesPolyfillsPlugin from '.'; | ||
@@ -33,2 +34,27 @@ require('debug').enable(require('../package.json').name); | ||
})); | ||
test('works with SafeBuffer and other package consumers', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const { unlink, paths: [ENTRY], } = yield writeFiles({ | ||
'entry.ts': `import {Buffer as SafeBuffer} from './safe-buffer'; console.log(SafeBuffer);`, | ||
'safe-buffer.ts': fs | ||
.readFileSync(require.resolve('safe-buffer')) | ||
.toString(), | ||
}); | ||
// const outfile = randomOutputFile() | ||
const res = yield build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [NodeModulesPolyfillsPlugin()], | ||
}); | ||
// console.log( | ||
// res.outputFiles[0].text | ||
// .split('\n') | ||
// .map((x, i) => i + ' ' + x) | ||
// .join('\n'), | ||
// ) | ||
eval(res.outputFiles[0].text); | ||
unlink(); | ||
})); | ||
test('events works', () => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -113,2 +139,23 @@ const { unlink, paths: [ENTRY], } = yield writeFiles({ | ||
})); | ||
test('does not include global keyword', () => __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()], | ||
}); | ||
const text = res.outputFiles[0].text; | ||
eval(text); | ||
console.log(text); | ||
expect(text).not.toContain(/\bglobal\b/); | ||
// console.log(res.outputFiles[0].text) | ||
unlink(); | ||
})); | ||
//# sourceMappingURL=index.test.js.map |
{ | ||
"name": "@esbuild-plugins/node-modules-polyfill", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -152,1 +152,26 @@ import { build } from 'esbuild' | ||
}) | ||
test('does not include global keyword', async () => { | ||
const { | ||
unlink, | ||
paths: [ENTRY], | ||
} = await 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 = await build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [NodeModulesPolyfillsPlugin()], | ||
}) | ||
const text = res.outputFiles[0].text | ||
eval(text) | ||
console.log(text) | ||
expect(text).not.toContain(/\bglobal\b/) | ||
// console.log(res.outputFiles[0].text) | ||
unlink() | ||
}) |
@@ -38,3 +38,10 @@ import { OnResolveArgs, Plugin } from 'esbuild' | ||
name, | ||
setup: function setup({ onLoad, onResolve }) { | ||
setup: function setup({ onLoad, onResolve, initialOptions }) { | ||
// polyfills contain global keyword, it must be defined | ||
if (initialOptions?.define && !initialOptions.define?.global) { | ||
initialOptions.define['global'] = '{}' | ||
} else if (!initialOptions?.define) { | ||
initialOptions.define = { global: '{}' } | ||
} | ||
// TODO these polyfill module cannot import anything, is that ok? | ||
@@ -41,0 +48,0 @@ async function loader( |
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
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
66242
1097
28
5
14