From 1a936bedbed41dd459b0ab55775327344695b0fa Mon Sep 17 00:00:00 2001 From: "David R. Myers" Date: Fri, 22 Dec 2023 12:59:27 -0500 Subject: [PATCH] Improve global shims (fixes #47) --- package.json | 32 +++++++++++++++++----- pnpm-lock.yaml | 28 +++++++------------- shims/banner/build.ts | 6 ++++- shims/banner/package.json | 5 ++++ shims/buffer/index.ts | 5 ++++ shims/buffer/package.json | 12 +++++++++ shims/global/index.ts | 5 ++++ shims/global/package.json | 12 +++++++++ shims/package.json | 12 +++++++++ shims/{ => process}/index.ts | 10 +++---- shims/process/package.json | 12 +++++++++ shims/vite.config.ts | 11 +++----- src/index.ts | 51 ++++++++++++++++++++++++------------ 13 files changed, 145 insertions(+), 56 deletions(-) create mode 100644 shims/banner/package.json create mode 100644 shims/buffer/index.ts create mode 100644 shims/buffer/package.json create mode 100644 shims/global/index.ts create mode 100644 shims/global/package.json rename shims/{ => process}/index.ts (59%) create mode 100644 shims/process/package.json diff --git a/package.json b/package.json index d5c0b27..ffd90f8 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,19 @@ }, "./shims/banner": { "require": "./shims/banner/dist/index.cjs", - "import": "./shims/banner/dist/index.cjs" + "import": "./shims/banner/dist/index.js" + }, + "./shims/buffer": { + "require": "./shims/buffer/dist/index.cjs", + "import": "./shims/buffer/dist/index.js" + }, + "./shims/global": { + "require": "./shims/global/dist/index.cjs", + "import": "./shims/global/dist/index.js" + }, + "./shims/process": { + "require": "./shims/process/dist/index.cjs", + "import": "./shims/process/dist/index.js" } }, "main": "./dist/index.cjs", @@ -44,10 +56,13 @@ "shims" ], "scripts": { - "build": "run-s build:core build:shims build:shims:banner build:types", + "build": "run-s build:core build:shims build:banner build:types", + "build:banner": "vite-node ./shims/banner/build.ts", "build:core": "vite build", - "build:shims": "vite build ./shims", - "build:shims:banner": "vite-node ./shims/banner/build.ts", + "build:shims": "run-p build:shims:*", + "build:shims:buffer": "vite build -c ./shims/vite.config.ts ./shims/buffer", + "build:shims:global": "vite build -c ./shims/vite.config.ts ./shims/global", + "build:shims:process": "vite build -c ./shims/vite.config.ts ./shims/process", "build:types": "run-s typecheck", "lint": "eslint .", "lint:fix": "eslint --fix .", @@ -70,16 +85,16 @@ }, "dependencies": { "@rollup/plugin-inject": "^5.0.5", - "buffer-polyfill": "npm:buffer@^6.0.3", - "node-stdlib-browser": "^1.2.0", - "process": "^0.11.10" + "node-stdlib-browser": "^1.2.0" }, "devDependencies": { "@types/node": "^18.18.8", + "buffer": "6.0.3", "esbuild": "^0.19.8", "eslint": "^8.54.0", "eslint-config-artisan": "^0.2.1", "npm-run-all": "^4.1.5", + "process": "^0.11.10", "rollup": "^4.6.0", "typescript": "4.8.3", "vite": "^5.0.2", @@ -92,6 +107,9 @@ "access": "public" }, "pnpm": { + "overrides": { + "buffer": "6.0.3" + }, "patchedDependencies": { "buffer@6.0.3": "patches/buffer@6.0.3.patch" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 468cf72..3811ae7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,9 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false +overrides: + buffer: 6.0.3 + patchedDependencies: buffer@6.0.3: hash: zkkuxompt5d553skpnegwi5wuy @@ -16,19 +19,16 @@ importers: '@rollup/plugin-inject': specifier: ^5.0.5 version: 5.0.5(rollup@4.6.0) - buffer-polyfill: - specifier: npm:buffer@^6.0.3 - version: /buffer@6.0.3(patch_hash=zkkuxompt5d553skpnegwi5wuy) node-stdlib-browser: specifier: ^1.2.0 version: 1.2.0 - process: - specifier: ^0.11.10 - version: 0.11.10 devDependencies: '@types/node': specifier: ^18.18.8 version: 18.18.8 + buffer: + specifier: 6.0.3 + version: 6.0.3(patch_hash=zkkuxompt5d553skpnegwi5wuy) esbuild: specifier: ^0.19.8 version: 0.19.8 @@ -41,6 +41,9 @@ importers: npm-run-all: specifier: ^4.1.5 version: 4.1.5 + process: + specifier: ^0.11.10 + version: 0.11.10 rollup: specifier: ^4.6.0 version: 4.6.0 @@ -1620,7 +1623,6 @@ packages: /base64-js@1.5.1: resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==} - dev: false /big-integer@1.6.52: resolution: {integrity: sha512-QxD8cf2eVqJOOz63z6JIN9BzvVs/dlySa5HGSBH5xtR8dPteIRQnBxxKqkNTiT6jbDTF6jAfrd4oMcND9RGbQg==} @@ -1741,19 +1743,11 @@ packages: resolution: {integrity: sha512-571s0T7nZWK6vB67HI5dyUF7wXiNcfaPPPTl6zYCNApANjIvYJTg7hlud/+cJpdAhS7dVzqMLmfhfHR3rAcOjQ==} dev: false - /buffer@5.7.1: - resolution: {integrity: sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==} - dependencies: - base64-js: 1.5.1 - ieee754: 1.2.1 - dev: false - /buffer@6.0.3(patch_hash=zkkuxompt5d553skpnegwi5wuy): resolution: {integrity: sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==} dependencies: base64-js: 1.5.1 ieee754: 1.2.1 - dev: false patched: true /builtin-modules@3.3.0: @@ -3066,7 +3060,6 @@ packages: /ieee754@1.2.1: resolution: {integrity: sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==} - dev: false /ignore@5.3.0: resolution: {integrity: sha512-g7dmpshy+gD7mh88OC9NwSGTKoc3kyLAZQRU1mt53Aw/vnvfXnbC+F/7F7QoYVKbV+KNvJx8wArewKy1vXMtlg==} @@ -3682,7 +3675,7 @@ packages: assert: 2.1.0 browser-resolve: 2.0.0 browserify-zlib: 0.2.0 - buffer: 5.7.1 + buffer: 6.0.3(patch_hash=zkkuxompt5d553skpnegwi5wuy) console-browserify: 1.2.0 constants-browserify: 1.0.0 create-require: 1.1.1 @@ -4063,7 +4056,6 @@ packages: /process@0.11.10: resolution: {integrity: sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==} engines: {node: '>= 0.6.0'} - dev: false /public-encrypt@4.0.3: resolution: {integrity: sha512-zVpa8oKZSz5bTMTFClc1fQOnyyEzpl5ozpi1B5YcvBrdohMjH2rfsBtyXcuNuwjsDIXmBYlF2N5FlJYhR29t8Q==} diff --git a/shims/banner/build.ts b/shims/banner/build.ts index f313d1b..98f4c04 100644 --- a/shims/banner/build.ts +++ b/shims/banner/build.ts @@ -8,7 +8,11 @@ await build({ bundle: true, entryPoints: [join(thisDir, './index.cjs')], format: 'iife', - inject: [join(thisDir, '..')], + inject: [ + join(thisDir, '../buffer'), + join(thisDir, '../global'), + join(thisDir, '../process'), + ], outdir: join(thisDir, './dist'), outExtension: { '.js': '.cjs' }, }) diff --git a/shims/banner/package.json b/shims/banner/package.json new file mode 100644 index 0000000..684aa9a --- /dev/null +++ b/shims/banner/package.json @@ -0,0 +1,5 @@ +{ + "type": "module", + "sideEffects": false, + "main": "./dist/index.cjs" +} diff --git a/shims/buffer/index.ts b/shims/buffer/index.ts new file mode 100644 index 0000000..838f4f5 --- /dev/null +++ b/shims/buffer/index.ts @@ -0,0 +1,5 @@ +// eslint-disable-next-line unicorn/prefer-node-protocol +import { Buffer } from 'buffer' + +export { Buffer } +export default Buffer diff --git a/shims/buffer/package.json b/shims/buffer/package.json new file mode 100644 index 0000000..5827a71 --- /dev/null +++ b/shims/buffer/package.json @@ -0,0 +1,12 @@ +{ + "type": "module", + "sideEffects": false, + "exports": { + ".": { + "require": "./dist/index.cjs", + "import": "./dist/index.js" + } + }, + "main": "./dist/index.cjs", + "module": "./dist/index.js" +} diff --git a/shims/global/index.ts b/shims/global/index.ts new file mode 100644 index 0000000..538c605 --- /dev/null +++ b/shims/global/index.ts @@ -0,0 +1,5 @@ +// eslint-disable-next-line @typescript-eslint/no-invalid-this +const global = globalThis || this || self + +export { global } +export default global diff --git a/shims/global/package.json b/shims/global/package.json new file mode 100644 index 0000000..5827a71 --- /dev/null +++ b/shims/global/package.json @@ -0,0 +1,12 @@ +{ + "type": "module", + "sideEffects": false, + "exports": { + ".": { + "require": "./dist/index.cjs", + "import": "./dist/index.js" + } + }, + "main": "./dist/index.cjs", + "module": "./dist/index.js" +} diff --git a/shims/package.json b/shims/package.json index 5827a71..d6e2fb6 100644 --- a/shims/package.json +++ b/shims/package.json @@ -5,6 +5,18 @@ ".": { "require": "./dist/index.cjs", "import": "./dist/index.js" + }, + "./buffer": { + "require": "./buffer/dist/index.cjs", + "import": "./buffer/dist/index.js" + }, + "./global": { + "require": "./global/dist/index.cjs", + "import": "./global/dist/index.js" + }, + "./process": { + "require": "./process/dist/index.cjs", + "import": "./process/dist/index.js" } }, "main": "./dist/index.cjs", diff --git a/shims/index.ts b/shims/process/index.ts similarity index 59% rename from shims/index.ts rename to shims/process/index.ts index 8806b60..89616f4 100644 --- a/shims/index.ts +++ b/shims/process/index.ts @@ -1,10 +1,8 @@ -// eslint-disable-next-line @typescript-eslint/no-invalid-this -const global = globalThis || this || self - -export { Buffer } from 'buffer-polyfill' // We cannot use `process-polyfill` as the package name due to a bug in Yarn v1. The errors results in a dependency // conflict with `node-stdlib-browser` which fails to import `process/browser.js`. // https://github.com/yarnpkg/yarn/issues/6907 // eslint-disable-next-line unicorn/prefer-node-protocol -export { default as process } from 'process' -export { global } +import process from 'process' + +export { process } +export default process diff --git a/shims/process/package.json b/shims/process/package.json new file mode 100644 index 0000000..5827a71 --- /dev/null +++ b/shims/process/package.json @@ -0,0 +1,12 @@ +{ + "type": "module", + "sideEffects": false, + "exports": { + ".": { + "require": "./dist/index.cjs", + "import": "./dist/index.js" + } + }, + "main": "./dist/index.cjs", + "module": "./dist/index.js" +} diff --git a/shims/vite.config.ts b/shims/vite.config.ts index a1d6066..c03db22 100644 --- a/shims/vite.config.ts +++ b/shims/vite.config.ts @@ -1,13 +1,14 @@ import { defineConfig } from 'vite' -import { externalizeDeps } from 'vite-plugin-externalize-deps' // https://vitejs.dev/config/ export default defineConfig({ build: { lib: { - entry: './index.ts', - fileName: 'index', + entry: { + index: './index.ts', + }, }, + minify: false, rollupOptions: { external: [/^node:.*$/], output: [ @@ -19,7 +20,6 @@ export default defineConfig({ { exports: 'named', format: 'cjs', - inlineDynamicImports: true, interop: 'auto', }, ], @@ -27,7 +27,4 @@ export default defineConfig({ sourcemap: true, target: 'esnext', }, - plugins: [ - externalizeDeps(), - ], }) diff --git a/src/index.ts b/src/index.ts index 51b9c2b..87cf202 100644 --- a/src/index.ts +++ b/src/index.ts @@ -139,7 +139,11 @@ const stripNodePrefix = (name: ModuleName): ModuleNameWithoutNodePrefix => { */ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { const require = createRequire(import.meta.url) - const globalShimsPath = require.resolve('vite-plugin-node-polyfills/shims') + const globalShimPaths = [ + require.resolve('vite-plugin-node-polyfills/shims/buffer'), + require.resolve('vite-plugin-node-polyfills/shims/global'), + require.resolve('vite-plugin-node-polyfills/shims/process'), + ] const globalShimsBannerPath = require.resolve('vite-plugin-node-polyfills/shims/banner') const globalShimsBanner = readFileSync(globalShimsBannerPath, 'utf-8') const optionsResolved: PolyfillOptionsResolved = { @@ -169,7 +173,15 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { const toOverride = (name: ModuleNameWithoutNodePrefix): string | void => { if (isDevEnabled(optionsResolved.globals.Buffer) && /^buffer$/.test(name)) { - return require.resolve('buffer-polyfill') + return 'vite-plugin-node-polyfills/shims/buffer' + } + + if (isDevEnabled(optionsResolved.globals.global) && /^global$/.test(name)) { + return 'vite-plugin-node-polyfills/shims/global' + } + + if (isDevEnabled(optionsResolved.globals.process) && /^process$/.test(name)) { + return 'vite-plugin-node-polyfills/shims/process' } if (name in optionsResolved.overrides) { @@ -211,9 +223,9 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { { ...inject({ // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md#vite - ...(isBuildEnabled(optionsResolved.globals.Buffer) ? { Buffer: [globalShimsPath, 'Buffer'] } : {}), - ...(isBuildEnabled(optionsResolved.globals.global) ? { global: [globalShimsPath, 'global'] } : {}), - ...(isBuildEnabled(optionsResolved.globals.process) ? { process: [globalShimsPath, 'process'] } : {}), + ...(isBuildEnabled(optionsResolved.globals.Buffer) ? { Buffer: 'vite-plugin-node-polyfills/shims/buffer' } : {}), + ...(isBuildEnabled(optionsResolved.globals.global) ? { global: 'vite-plugin-node-polyfills/shims/global' } : {}), + ...(isBuildEnabled(optionsResolved.globals.process) ? { process: 'vite-plugin-node-polyfills/shims/process' } : {}), }), }, ], @@ -224,6 +236,9 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { banner: isDev ? globalShimsBanner : undefined, }, optimizeDeps: { + exclude: [ + ...globalShimPaths, + ], esbuildOptions: { banner: isDev ? { js: globalShimsBanner } : undefined, // https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209 @@ -233,7 +248,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { ...((isDev && isDevEnabled(optionsResolved.globals.process)) ? { process: 'process' } : {}), }, inject: [ - globalShimsPath, + ...globalShimPaths, ], plugins: [ esbuildPlugin(polyfills), @@ -243,18 +258,20 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { name: 'vite-plugin-node-polyfills-shims-resolver', setup(build) { - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping - const escapedGlobalShimsPath = globalShimsPath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') - const globalShimsFilter = new RegExp(`^${escapedGlobalShimsPath}$`) + for (const globalShimPath of globalShimPaths) { + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping + const escapedGlobalShimPath = globalShimPath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + const globalShimsFilter = new RegExp(`^${escapedGlobalShimPath}$`) - // https://esbuild.github.io/plugins/#on-resolve - build.onResolve({ filter: globalShimsFilter }, () => { - return { - // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468 - external: false, - path: globalShimsPath, - } - }) + // https://esbuild.github.io/plugins/#on-resolve + build.onResolve({ filter: globalShimsFilter }, () => { + return { + // https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468 + external: false, + path: globalShimPath, + } + }) + } }, }, ],