Skip to content

Instantly share code, notes, and snippets.

View bluwy's full-sized avatar
♥️
【=◈︿◈=】

Bjorn Lu bluwy

♥️
【=◈︿◈=】
View GitHub Profile
@bluwy
bluwy / vite.config.js
Created May 4, 2021 15:52
Import SVG as Svelte component
import fs from 'fs/promises'
import { defineConfig } from 'vite'
import svelte from '@sveltejs/vite-plugin-svelte'
export default defineConfig({
plugins: [svelteSvgPlugin(), svelte({ extensions: ['.svelte', '.svg'] })],
})
function svelteSvgPlugin() {
return {
@bluwy
bluwy / clean-snaps.sh
Created May 4, 2021 06:18
Remove unused snaps
#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu
LANG=C snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
snap remove "$snapname" --revision="$revision"
done
@bluwy
bluwy / ghaction-publish.md
Created January 24, 2021 05:25
GitHub Actions publish flow

Assuming

  1. Latest version is v1
  2. Real underlying version is v1.0.0
  3. A new release is to be published at v1.0.1 and v1

Steps

  1. Push commits
  2. Create new release for v1.0.1
@bluwy
bluwy / rollup-plugin-postcss.js
Created December 13, 2020 05:48
Rollup plugin to transform postcss files to virtual css files. Can be used with rollup-plugin-css-only to build a single css bundle
import { createFilter } from '@rollup/pluginutils'
import postcss from 'postcss'
import loadConfig from 'postcss-load-config'
/**
* @typedef {{
* include: import('@rollup/pluginutils').FilterPattern,
* exclude: import('@rollup/pluginutils').FilterPattern
* }} Options
*/
@bluwy
bluwy / remove-fluff.ts
Created December 10, 2020 16:19
Postgraphile remove Relay 1 fields
import type { Plugin } from 'postgraphile'
export const removeFluffPlugin: Plugin = (builder) => {
builder.hook('GraphQLInputObjectType:fields', (fields, _, { scope }) => {
if (scope.isMutationInput) {
delete fields.clientMutationId
}
return fields
})
@bluwy
bluwy / auto-external.js
Created October 24, 2020 14:22
Rollup automatically set dependencies to external
import pkg from './package.json'
export function autoExternal() {
return {
name: 'auto-external',
options(opts) {
const deps = [
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies)
]
@bluwy
bluwy / context.js
Last active September 22, 2021 18:01
Utility function to create Svelte contexts
import { getContext, setContext } from 'svelte'
/**
* @template T
* @param {any} [k]
* @returns {[() => T, (v: T) => void]}
*/
export function createContext(k = {}) {
return [() => getContext(k), (v) => setContext(k, v)]
}
@bluwy
bluwy / usage.ts
Last active December 6, 2020 05:14
Svelte derived store with extended writable functionaility. A quick alternative to https://github.com/PixievoltNo1/svelte-writable-derived.
import { get, writable } from 'svelte/store'
import { writableDerived } from './writable-derived'
const foo = writable('bar')
const hello = writable('world')
const store = writableDerived(
[foo, hello],
([$foo, $hello]) => ({
fooKey: $foo,
@bluwy
bluwy / get.ts
Created August 7, 2020 07:23
Get the current value of subscribable stores, e.g. Svelte stores
/** Callback to inform of a value updates. */
type Subscriber<T> = (value: T) => void
/** Unsubscribes from value updates. */
type Unsubscriber = () => void
/** A store that can be subscribed */
interface Subscribable<T> {
subscribe: (run: Subscriber<T>) => Unsubscriber
}
@bluwy
bluwy / debounce.js
Created June 18, 2020 11:22
Simple debounce and throttle with proper context binding
function debounce(fn, wait) {
let t
return function () {
clearTimeout(t)
t = setTimeout(() => fn.apply(this, arguments), wait)
}
}