Skip to content

Instantly share code, notes, and snippets.

View developit's full-sized avatar
🦊
write, the codes

Jason Miller developit

🦊
write, the codes
View GitHub Profile
@developit
developit / *String.prototype.replaceAll() Ponyfill.md
Last active October 11, 2020 23:32
~80b ponyfill for String.prototype.replaceAll()

@developit/replaceall NPM

~80b ponyfill for String.prototype.replaceAll() with good performance.

Why ponyfill? Because this is a proposal for a spec, and polyfilling it in-place before it gets solidified could break code that relies on an incorrect implementation.

Details Alternate Version
@developit
developit / *webpack-size.md
Last active April 19, 2019 18:23
npx webpack-size foo.js → https://npm.im/webpack-size

webpack-size

Weigh current module:

npx webpack-size

Weigh the size of a specific module entry:

function serialize(node) {
if (node.nodeType === 3) {
return node.data;
}
else if (node.nodeType === 8) {
return '<!--' + node.data + '-->';
}
else if (node.nodeType === 1 || node.nodeType === 9) {
let str = '<' + node.nodeName;
const attrs = [];
@developit
developit / identity-component.js
Created March 25, 2019 00:23
The Identity Component is a component that does nothing but render its child/children.
/**
* The Identity Component is a component that does nothing but render its child/children.
*/
export default (props) => props.children;
module.exports = require('preact/compat');

respond

this isn't a library, don't use this.

it's a 1.5kb react-ey thing but using HTML instead of VDOM. diffing done via a hacked up version of set-dom.

Important Counter Demo

// this is a standard react component, except render() returns an HTML string
'oncopy oncut onpaste onabort onblur oncancel onchange onclick onclose oncontextmenu ondblclick ondrag ondragend ondragenter ondragleave ondragover ondragstart ondrop ondurationchange onemptied onended onerror onfocus oninput oninvalid onkeydown onkeypress onkeyup onload onloadeddata onloadedmetadata onloadstart onmousedown onmouseenter onmouseleave onmousemove onmouseout onmouseover onmouseup onmousewheel onpause onprogress onreset onresize onscroll onselect onstalled onsubmit onsuspend ontoggle onwaiting onwheel onpointerdown onpointermove onpointerup onpointercancel onpointerover onpointerout onpointerenter onpointerleave onselectstart onselectionchange onbeforecopy onbeforecut onbeforepaste onsearch onfullscreenchange onfullscreenerror'
.split(' ')
.map(e => {
Element.prototype[e] = null;
});
// import fetch from 'isomorphic-unfetch';
const RETRIES = 5;
/**
* Example:
* global.fetch = fetchWithRetry;
*/
function fetchWithRetry(url, options) {
import Link from 'next/link'
import { withRouter } from 'next/router'
import histore from 'histore'
const historyStore = histore();
const linkStyle = {
marginRight: 10
}
@developit
developit / babel-plugin-optimize-tagged-templates.js
Created May 29, 2019 20:04
Babel plugin to optimize tagged templates by inlining string/number/boolean values.
module.exports = (babel, options = {}) => {
const { types: t } = babel;
return {
name: "optimize-tagged-templates",
visitor: {
TemplateLiteral(path) {
for (let i=0; i<path.node.expressions.length; i++) {
const expr = path.node.expressions[i];
let isLiteral = t.isStringLiteral(expr);