Skip to content

Instantly share code, notes, and snippets.

View longlho's full-sized avatar

Long Ho longlho

View GitHub Profile
@longlho
longlho / transformer-p1.ts
Created March 31, 2019 19:48
transformer-p1
const visitor: ts.Visitor = (node: ts.Node): ts.Node => {
if (ts.isImportDeclaration(node) && node.moduleSpecifier) {
// This is the node we're looking for
}
return ts.visitEachChild(node, visitor)
}
@longlho
longlho / transformer-entry.ts
Last active March 31, 2019 19:29
transformer entry point
export function transform(opts: YourTransformerOpts): ts.TransformerFactory<ts.SourceFile> {
return ctx => sf => transformNode(sf, opts)
}
@longlho
longlho / program-emit.ts
Created March 24, 2019 19:23
sample ts pipeline
const emitResult = program.emit(
undefined,
(fileName, content) => {
ts.sys.writeFile(fileName, `/* @generated */${ts.sys.newLine}${content}`);
},
undefined,
undefined,
{
before: [
inlineImg({
@longlho
longlho / options.ts
Created March 24, 2019 19:15
Sample TS Compiler Options
const CJS_CONFIG: ts.CompilerOptions = {
experimentalDecorators: true,
jsx: ts.JsxEmit.React,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
noEmitOnError: false,
noUnusedLocals: true,
noUnusedParameters: true,
stripInternal: true,
declaration: true,
@longlho
longlho / compile.ts
Created March 24, 2019 19:01
TS Compiler Wrapper
function compiler (configFilePath: string) {
// tslint:disable-next-line no-any
const host: ts.ParseConfigFileHost = ts.sys as any;
// Fix after https://github.com/Microsoft/TypeScript/issues/18217
host.onUnRecoverableConfigFileDiagnostic = printDiagnostic;
const parsedCmd = ts.getParsedCommandLineOfConfigFile(configFilePath, undefined, host);
host.onUnRecoverableConfigFileDiagnostic = undefined;
const {options, fileNames} = parsedCmd;
@longlho
longlho / css-modules.tsx
Created March 21, 2018 23:12
css-modules
import * as styles from 'foo.css'
type CssMap = typeof styles
export const legacyCss: CssMap = {
container: 'mc-foo',
content: 'mc-content'
}
export interface Props {
css: CssMap
@longlho
longlho / compile-css.ts
Created August 13, 2017 18:57
Extract CSS type definitions
import * as postcss from 'postcss'
import { basename, resolve, join } from 'path'
import { outputFileSync } from 'fs-extra'
import { sync as globSync } from 'glob'
import { readFileSync } from 'fs'
import { watch as chokidarWatch } from 'chokidar'
const { NODE_ENV } = process.env
// Change CSS glob pattern
@longlho
longlho / test.js
Created February 23, 2017 16:32
intl tree-shaking
// lib.js
import formatRelative from 'intl-relativeformat'
export function foo () {
formatRelative()
}
export function bar () {}
// main.js
@longlho
longlho / ioslocaleidentifiers.csv
Last active January 6, 2017 18:07 — forked from jacobbubu/ioslocaleidentifiers.csv
iOS Locale Identifiers
mr Marathi
bs Bosnian
ee_TG Ewe (Togo)
ms Malay
kam_KE Kamba (Kenya)
mt Maltese
ha Hausa
es_HN Spanish (Honduras)
ml_IN Malayalam (India)
ro_MD Romanian (Moldova)
@longlho
longlho / kittens_script.js
Last active August 29, 2015 14:24
Kittens scripts
(function () {
var res = window.gamePage.resPool.resources;
function findRes (type) {
return res.filter(function (r) {
return r.name === type;
})[0];
}
function setRes (type, amt) {