From a8ded05b3528013603ca3b96244c0932af45996b 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 | 21 +++++++++++++++++--- shims/banner/build.ts | 6 +++++- shims/banner/package.json | 5 +++++ shims/buffer/index.ts | 4 ++++ 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 | 38 +++++++++++++++++++++--------------- 12 files changed, 115 insertions(+), 33 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..50a3357 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,18 @@ "./shims/banner": { "require": "./shims/banner/dist/index.cjs", "import": "./shims/banner/dist/index.cjs" + }, + "./shims/buffer": { + "require": "./shims/buffer/dist/index.cjs", + "import": "./shims/buffer/dist/index.cjs" + }, + "./shims/global": { + "require": "./shims/global/dist/index.cjs", + "import": "./shims/global/dist/index.cjs" + }, + "./shims/process": { + "require": "./shims/process/dist/index.cjs", + "import": "./shims/process/dist/index.cjs" } }, "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 .", 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..e288cb0 --- /dev/null +++ b/shims/buffer/index.ts @@ -0,0 +1,4 @@ +import { Buffer } from 'buffer-polyfill' + +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..46fe171 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 = { @@ -211,9 +215,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' } : {}), }), }, ], @@ -233,7 +237,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => { ...((isDev && isDevEnabled(optionsResolved.globals.process)) ? { process: 'process' } : {}), }, inject: [ - globalShimsPath, + ...globalShimPaths, ], plugins: [ esbuildPlugin(polyfills), @@ -243,18 +247,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, + } + }) + } }, }, ],