Skip to content

Instantly share code, notes, and snippets.

View unional's full-sized avatar
🏎️
just-web!!!

Homa Wong unional

🏎️
just-web!!!
View GitHub Profile
@sindresorhus
sindresorhus / esm-package.md
Last active April 26, 2024 03:53
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
# update local to remove all remote-tracking branches
git fetch --all -p
# remove local branches with no remote branches
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D
@unional
unional / event-error.js
Last active November 17, 2017 00:18
Error thrown in event listener affects emit code
const EventEmitter = require('events')
const emitter = new Eventemitter()
function shouldNotThrow() {
try {
emitter.emit('x')
}
catch {
// unfortunately, error is thrown
@jcalz
jcalz / builderPattern.ts
Created July 25, 2017 14:44
Builder Pattern example code
type IBuilder<T> = {
[k in keyof T]: (arg: T[k]) => IBuilder<T>
} & { build(): T }
function proxyBuilder<T>(): IBuilder<T> {
var built: any = {};
var builder = new Proxy({}, {
get: function(target, prop, receiver) {
if (prop === 'build') return () => built;
return (x: any): any => {
@Rich-Harris
Rich-Harris / module-loading.md
Last active April 19, 2023 09:11
Dynamic module loading done right

Dynamic module loading done right

Follow-up to Top-level await is a footgun – maybe read that first

Here are some things I believe to be true:

  1. Static module syntax is beneficial in lots of ways – code is easier to write (you get better linting etc) and easier to optimise (tree-shaking and other things that are only really possible with static syntax), and most importantly, faster to load (it's trivial for a module loader to load multiple dependencies concurrently when they're declared with a static syntax – not so with imperative statements like require(...) or await import(...)).
  2. App startup time is perhaps when performance is most critical. (You already know this, I don't need to cite the studies.)
  3. If you're in favour of constructs that jeopardise app startup time, you are anti-user. Top-level await is such a construct.
@Rich-Harris
Rich-Harris / footgun.md
Last active April 19, 2024 07:47
Top-level `await` is a footgun

Edit — February 2019

This gist had a far larger impact than I imagined it would, and apparently people are still finding it, so a quick update:

  • TC39 is currently moving forward with a slightly different version of TLA, referred to as 'variant B', in which a module with TLA doesn't block sibling execution. This vastly reduces the danger of parallelizable work happening in serial and thereby delaying startup, which was the concern that motivated me to write this gist
  • In the wild, we're seeing (async main(){...}()) as a substitute for TLA. This completely eliminates the blocking problem (yay!) but it's less powerful, and harder to statically analyse (boo). In other words the lack of TLA is causing real problems
  • Therefore, a version of TLA that solves the original issue is a valuable addition to the language, and I'm in full support of the current proposal, which you can read here.

I'll leave the rest of this document unedited, for archaeological

@GianlucaGuarini
GianlucaGuarini / post-merge
Last active August 22, 2023 20:54 — forked from sindresorhus/post-merge
Git hook that gets triggered after any 'git pull' whenever one of the files specified has changed. Useful to update any web application dependency using bower npm or composer
#/usr/bin/env bash
# MIT © Sindre Sorhus - sindresorhus.com
# forked by Gianluca Guarini
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep -E --quiet "$1" && eval "$2"
}
@rxaviers
rxaviers / gist:7360908
Last active April 26, 2024 06:50
Complete list of github markdown emoji markup

People

:bowtie: :bowtie: 😄 :smile: 😆 :laughing:
😊 :blush: 😃 :smiley: ☺️ :relaxed:
😏 :smirk: 😍 :heart_eyes: 😘 :kissing_heart:
😚 :kissing_closed_eyes: 😳 :flushed: 😌 :relieved:
😆 :satisfied: 😁 :grin: 😉 :wink:
😜 :stuck_out_tongue_winking_eye: 😝 :stuck_out_tongue_closed_eyes: 😀 :grinning:
😗 :kissing: 😙 :kissing_smiling_eyes: 😛 :stuck_out_tongue:
@sirkitree
sirkitree / node.js
Last active December 24, 2018 15:31
Parse a PHP file with Node.js and convert an array defined in the PHP script into a JSON object which the Node app can use.
var runner = require('child_process');
runner.exec(
'php -r \'include("settings.php"); print json_encode($databases);\'',
function (err, stdout, stderr) {
var connection = JSON.parse(stdout).default.default;
console.log(connection.database);
// result botdb
}