summaryrefslogtreecommitdiff
path: root/node_modules/@esbuild-plugins/node-globals-polyfill/src/index.ts
blob: 4b06c378ac5059858e79a3718cffa5a3780258ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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