pollute-global
Make any value global in any JavaScript environment!
Use case
Test
import { pollute } from '@polioan/pollute-global'
function toTest() {
return 333
}
if (process.env.NODE_ENV === 'test') {
pollute('toTest', toTest)
}
Global cache (useful for hot reload)
import { pollute } from '@polioan/pollute-global'
import { PrismaClient } from '@prisma/client'
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined
}
export const prisma = globalForPrisma.prisma ?? new PrismaClient({})
if (process.env.NODE_ENV !== 'production') {
pollute('prisma', prisma)
}
Libs for browser
import { pollute } from '@polioan/pollute-global'
class Myjquery {}
pollute('$', new Myjquery())
Polyfills
import { pollute } from '@polioan/pollute-global'
if (typeof structuredClone === 'undefined') {
pollute('structuredClone', value => JSON.parse(JSON.stringify(value)))
}
Creating global libraries
import { pollute } from '@polioan/pollute-global'
declare global {
var calculate: (a: number, b: number) => number
}
pollute('calculate', (a: number, b: number) => a + b)
const test = calculate(2, 3) // will work
Install
npm
npm i @polioan/pollute-global
yarn
yarn add @polioan/pollute-global
CDN
<script src="https://unpkg.com/@polioan/pollute-global@1.0.0/dist/index.global.js"></script>
<script>
pollute('some', 'test')
</script>