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
@andrewiggins
andrewiggins / preact-checker.js
Created July 16, 2020 17:04
Find any preact roots on a webpage
// Run `"javascript:$(terser .\preact-checker.js)" > .\preact-checker.min.js` to make it a bookmarklet
(function () {
var expando =
typeof Symbol != "undefined" && Symbol.for && Symbol.for("preactattr");
function isMatch(node) {
if ("__k" in node && "props" in node.__k && "type" in node.__k) {
return true;
}
return (

Unistore Hooks for Preact

Experimental hooks-based bindings for Unistore. Available on npm:

npm i unistore-hooks

Note: this should also work with React, just alias "preact" and "preact/hooks" to "react" in your bundler.

Usage

resolve-export-map

import { resolveExportMap } from 'resolve-export-map';

const demoMeta = {
  exports: {
    '.': './index.mjs',
    './foo': {
 import: './dist/foo.mjs',

Module Workers Polyfill npm version

This is a 1.1kb polyfill for Module Workers.

It adds support for new Worker('..',{type:'module'}) to all modern browsers (those that support fetch).

Usage

Copy module-workers-polyfill.js to your web directory, then load it using an import or a script tag. It just needs to be loaded before instantiating your Worker.

@developit
developit / *cjyes.md
Last active July 25, 2023 12:54
more-or-less instant command-line ESM to CJS transform. Copies from src to dist. `cjyes src/*.js`

cjyes npm version

🔍 see jay, yes! 🎉 / 👨🏻‍💻 see, JS! 👾 / ⚓️ sea JS ⛴

If you're publishing ES Modules, you need to also publish CommonJS versions of those modules.

This isn't to support old browsers or Node versions: even in Node 14, using require() to load a module won't work if it's only available as ESM.

cjyes is the bare minimum fix for this problem. You write ES Modules and fill out a valid package.json, and it'll generate the corresponding CommonJS files pretty much instantly. cjyes takes up 500kb of disk space including its two dependencies.

@jviide
jviide / README.md
Last active November 7, 2020 22:39

A Small HTM VM FTW OMG BBQ

This is a half-baked sketch of compiling HTM expressions into a list of instructions that can be interpreted in runtime with a compact function. The instructions hopefully compress well and the evaluation function should be not-slow. Maybe.

  • evaluate.js contains the evaluation function (evaluate(...)) as well as an example of a compiled HTM call.

  • babel.js contains a Babel plugin that transforms HTM calls into the stack language. The build.mjs file it imports is HTM's src/build.mjs.

This tweet demonstrates that a minified evaluate() fits in one tweet: https://twitter.com/jviide/status/1257755526722662405 🙂

title layout permalink oneline
Creating .d.ts Files from .js files
docs
/docs/handbook/declaration-files/dts-from-js.html
How to add d.ts generation to JavaScript projects

With TypeScript 3.7, TypeScript added support for generating .d.ts files from JavaScript using JSDoc syntax.

Standalone forwardRef

import { forwardRef } from 'preact-forwardref';

const Foo = forwardRef((props, ref) => {
  return <div ref={ref} />
});

render( console.log(div)} /&gt;, document.body);
@nsivertsen
nsivertsen / import_map.json
Created April 7, 2020 08:43
Preact and htm on Deno
{
"imports": {
"htm": "https://unpkg.com/[email protected]/dist/htm.module.js",
"htm/preact": "https://unpkg.com/[email protected]/preact/index.module.js",
"preact": "https://unpkg.com/[email protected]/dist/preact.module.js",
"preact-render-to-string": "https://unpkg.com/[email protected]/dist/index.module.js"
}
}

Strict-equality

When creating elements and passing them as children we can have scenario's where we don't have to diff the children because it will still be the same vnode.

Imagine the following scenario:

const context = createContext(null);
const App = () => (