diff options
| author | akiyamn | 2023-09-24 23:22:21 +1000 |
|---|---|---|
| committer | akiyamn | 2023-09-24 23:22:21 +1000 |
| commit | 4e87195739f2a5d9a05451b48773c8afdc680765 (patch) | |
| tree | 9cba501844a4a11dcbdffc4050ed8189561c55ed /node_modules/@esbuild-plugins/node-globals-polyfill/src | |
| download | price-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.tar.gz price-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.zip | |
Initial commit (by create-cloudflare CLI)
Diffstat (limited to 'node_modules/@esbuild-plugins/node-globals-polyfill/src')
| -rw-r--r-- | node_modules/@esbuild-plugins/node-globals-polyfill/src/index.test.ts | 155 | ||||
| -rw-r--r-- | node_modules/@esbuild-plugins/node-globals-polyfill/src/index.ts | 84 |
2 files changed, 239 insertions, 0 deletions
diff --git a/node_modules/@esbuild-plugins/node-globals-polyfill/src/index.test.ts b/node_modules/@esbuild-plugins/node-globals-polyfill/src/index.test.ts new file mode 100644 index 0000000..62e530e --- /dev/null +++ b/node_modules/@esbuild-plugins/node-globals-polyfill/src/index.test.ts @@ -0,0 +1,155 @@ +import { build } from 'esbuild' +import { writeFiles } from 'test-support' +import { NodeGlobalsPolyfillPlugin } from '.' + +require('debug').enable(require('../package.json').name) + +test('process works', async () => { + const { + unlink, + paths: [ENTRY], + } = await writeFiles({ + 'entry.ts': `process.version`, + }) + const res = await build({ + entryPoints: [ENTRY], + write: false, + format: 'esm', + target: 'es2017', + bundle: true, + inject: [require.resolve('../process')], + }) + const output = res.outputFiles[0].text + // console.log(output) + eval(output) + 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() +}) + +// TODO esbuild cannot use virtual modules for inject: https://github.com/evanw/esbuild/issues/2762 +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, + define: { + 'process.env.VAR': '"hello"', + }, + plugins: [NodeGlobalsPolyfillPlugin({})], + }) + 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() +}) diff --git a/node_modules/@esbuild-plugins/node-globals-polyfill/src/index.ts b/node_modules/@esbuild-plugins/node-globals-polyfill/src/index.ts new file mode 100644 index 0000000..4b06c37 --- /dev/null +++ b/node_modules/@esbuild-plugins/node-globals-polyfill/src/index.ts @@ -0,0 +1,84 @@ +import path from 'path' +import fs from 'fs' +import * as esbuild from 'esbuild' + +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'), + } + }) + + // TODO esbuild cannot use virtual modules for inject: https://github.com/evanw/esbuild/issues/2762 + // 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}', + // ), + // } + // }) + onResolve({ filter: /_virtual-process-polyfill_\.js/ }, () => { + return { + path: path.resolve(__dirname, '../process.js'), + } + }) + onResolve({ filter: /_virtual-buffer-polyfill_\.js/ }, () => { + return { + path: path.resolve(__dirname, '../_buffer.js'), + } + }) + + const polyfills: string[] = [] + if (process) { + polyfills.push( + path.resolve(__dirname, '../_virtual-process-polyfill_.js'), + ) + } + if (buffer) { + polyfills.push( + path.resolve(__dirname, '../_virtual-buffer-polyfill_.js'), + ) + } + if (initialOptions.inject) { + initialOptions.inject.push(...polyfills) + // handle duplicate plugin + initialOptions.inject = [...new Set(initialOptions.inject)] + } else { + initialOptions.inject = [...polyfills] + } + }, + } +} + +export default NodeGlobalsPolyfillPlugin |
