Skip to content

Instantly share code, notes, and snippets.

View matthewhartman's full-sized avatar

Matthew Hartman matthewhartman

View GitHub Profile
@matthewhartman
matthewhartman / generate-bundle.sh
Created April 23, 2020 08:49
Generate A Unique JS Bundle Name
#!/bin/bash
rm -rf ./dist &&
parcel build src/index.html &&
VERSION=$(git rev-parse --short HEAD)
cp ./dist/src*.js ./dist/$VERSION.js
cp ./dist/src*.js ./dist/default.js
@matthewhartman
matthewhartman / generate-new-service-worker-version.js
Created March 9, 2020 06:36
Generate New Service Worker Version
// https://mydevhack.com/post/automatically-generate-a-new-service-worker-version
const fs = require('fs')
// Create new version
const version = new Date().getTime()
// Write new version to service worker
fs.readFile('sw.js', 'utf8', function (err,data) {
if (err) {
return console.log(err)
@matthewhartman
matthewhartman / sw.js
Created March 6, 2020 08:38
Service Worker Code
// Service Worker
var CACHE_NAME = 'v-1'
// Initial Assets to Cache
var urlsToCache = [
'/',
'/index.html',
'/android-chrome-144x144.png',
'/android-chrome-192x192.png',
@matthewhartman
matthewhartman / service-worker.js
Created March 6, 2020 06:43
Basic Service Worker (needs corresponding sw.js on root)
// Service Worker
(function(serviceWorker) {
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope)
}, function(err) {
console.log('ServiceWorker registration failed: ', err)
})
})
@matthewhartman
matthewhartman / tsconfig.json
Created March 2, 2020 09:11
Typescript Configuration
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"experimentalDecorators": true,
"emitDecoratorMetadata": false,
"moduleResolution": "node",
"stripInternal": true,
"declaration": false,
"allowSyntheticDefaultImports": true,
@matthewhartman
matthewhartman / package.json
Created March 2, 2020 08:57
Parcel Configuration for Typescript
{
"name": "parcel-example",
"version": "1.0.0",
"description": "A basic setup using Parcel - A blazing fast, zero configuration web application bundler",
"scripts": {
"start": "NODE_ENV=development parcel src/index.html --open",
"build": "NODE_ENV=production rm -rf dist && tsc --noEmit && parcel build src/index.html",
"clean": "rm -rf dist"
},
"private": true,
@matthewhartman
matthewhartman / my-gulp-4-file.js
Last active June 2, 2021 20:35
My Gulp 4 File / Setup
const { watch, src, dest, parallel } = require('gulp');
const sass = require('gulp-sass');
const cssnano = require('cssnano');
const sourcemaps = require('gulp-sourcemaps');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const concat = require('gulp-concat');
const browserSync = require('browser-sync');
const newer = require('gulp-newer');
const imagemin = require('gulp-imagemin');
@matthewhartman
matthewhartman / log-json.js
Last active May 4, 2019 02:57
Console Log Object and Output to JSON
// in dev tools, right click on object and `store as global variable`
JSON.stringify(temp1, null, 2) // 2 tab spacing
@matthewhartman
matthewhartman / useRouter.js
Created March 21, 2019 07:16 — forked from tim-field/useRouter.js
Hooks Router
import { useEffect, useState } from "react"
import { createBrowserHistory } from "history"
const history = createBrowserHistory()
const trim = url => url.replace(/^\/|\/$/g, "")
function useRouter(initial = "") {
const [route, setRoute] = useState(initial)
useEffect(() => {
const { pathname, search } = new URL(route, window.location.href)