Skip to content

Commit

Permalink
Improve global shims (fixes #47)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmyersdev committed Dec 22, 2023
1 parent f0aecbe commit a8ded05
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 33 deletions.
21 changes: 18 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 .",
Expand Down
6 changes: 5 additions & 1 deletion shims/banner/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
})
5 changes: 5 additions & 0 deletions shims/banner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "module",
"sideEffects": false,
"main": "./dist/index.cjs"
}
4 changes: 4 additions & 0 deletions shims/buffer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Buffer } from 'buffer-polyfill'

export { Buffer }
export default Buffer
12 changes: 12 additions & 0 deletions shims/buffer/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
5 changes: 5 additions & 0 deletions shims/global/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-invalid-this
const global = globalThis || this || self

export { global }
export default global
12 changes: 12 additions & 0 deletions shims/global/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
12 changes: 12 additions & 0 deletions shims/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 4 additions & 6 deletions shims/index.ts → shims/process/index.ts
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions shims/process/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
11 changes: 4 additions & 7 deletions shims/vite.config.ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand All @@ -19,15 +20,11 @@ export default defineConfig({
{
exports: 'named',
format: 'cjs',
inlineDynamicImports: true,
interop: 'auto',
},
],
},
sourcemap: true,
target: 'esnext',
},
plugins: [
externalizeDeps(),
],
})
38 changes: 22 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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' } : {}),
}),
},
],
Expand All @@ -233,7 +237,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
...((isDev && isDevEnabled(optionsResolved.globals.process)) ? { process: 'process' } : {}),
},
inject: [
globalShimsPath,
...globalShimPaths,
],
plugins: [
esbuildPlugin(polyfills),
Expand All @@ -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,
}
})
}
},
},
],
Expand Down

0 comments on commit a8ded05

Please sign in to comment.