Skip to content

Instantly share code, notes, and snippets.

@citrus
citrus / index.js
Created June 8, 2021 18:55
Netlify plugin to contextualize vite env vars.
module.exports = {
onPreBuild: () => {
const BRANCH = process.env.BRANCH.toUpperCase().replace(/-/g, '_')
console.log('Vite env plugin starting for branch:', BRANCH)
Object.keys(process.env).sort().forEach(key => {
if (!key.startsWith('VITE_') || key.endsWith(`_${BRANCH}`)) {
return
}
const contextualValue = process.env[`${key}_${BRANCH}`]
@citrus
citrus / populateWithDotSyntax.js
Created November 10, 2021 15:13
Transform an object into an object with its values as deeply nested keys to themselves
const isLiteralObject = (object => !!object && object.constructor === Object)
function populateWithDotSyntax (object, parent = null) {
return Object.entries(object).reduce((obj, [ key, value ]) => {
const string = parent !== null ? `${parent}.${ key }` : key
if (isLiteralObject(value)) {
obj[key] = populateWithDotSyntax(value, string)
} else if (Array.isArray(value)) {
obj[key] = value.map((i, index) => populateWithDotSyntax(i, `${key}.${index}`))
} else {
@citrus
citrus / pinia.ts
Created December 9, 2022 17:41
Add Pinia stores as global properties in Vue3 plugin with Typescript
import type { App } from 'vue'
import { useAppStore, useAuthStore } from '@/stores'
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$app: ReturnType<typeof useAppStore>
$auth: ReturnType<typeof useAuthStore>
}
}