Skip to content

Instantly share code, notes, and snippets.

View longlho's full-sized avatar

Long Ho longlho

View GitHub Profile
@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 / 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 / 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 / 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 / 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-p2.ts
Created March 31, 2019 19:49
transformer-p2
const visitor: ts.Visitor = (node: ts.Node): ts.Node => {
if (ts.isImportDeclaration(node) && node.moduleSpecifier) {
const newNode = ts.getMutableClone(node) // Clone the node
}
return ts.visitEachChild(node, visitor)
}
@longlho
longlho / transformer-p3.ts
Last active March 31, 2019 19:53
transformer-p3
const visitor: ts.Visitor = (node: ts.Node): ts.Node => {
if (ts.isImportDeclaration(node) && node.moduleSpecifier) {
const newNode = ts.getMutableClone(node) // Clone the node
const oldPathWithQuotes = node.moduleSpecifier.getText(sf)
const newPath = rewritePath(oldPathWithQuotes.slice(1, oldPathWithQuotes.length - 2))
newNode.moduleSpecifier = ts.createStringLiteral(newPath)
}
return ts.visitEachChild(node, visitor)
}
@longlho
longlho / transformer-p4.ts
Created March 31, 2019 19:56
transformer-p4
const visitor: ts.Visitor = (node: ts.Node): ts.Node => {
if (ts.isImportDeclaration(node) && node.moduleSpecifier) {
const newNode = ts.getMutableClone(node) // Clone the node
const oldPathWithQuotes = node.moduleSpecifier.getText(sf)
const newPath = rewritePath(oldPathWithQuotes.slice(1, oldPathWithQuotes.length - 2))
newNode.moduleSpecifier = ts.createStringLiteral(newPath)
ts.setSourceMapRange(newNode, ts.getSourceMapRange(node))
return newNode
}
return ts.visitEachChild(node, visitor)
@longlho
longlho / transformer-p5.ts
Last active August 20, 2019 02:38
transformer-p5.ts
if (ts.isImportDeclaration(node)) {
// Handle `import foo.css`
if (CSS_EXTENSION_REGEX.test(node.moduleSpecifier.getText())) {
cssPath = resolveCssPath(
node.moduleSpecifier.getText(),
sf,
tsImportResolver
);
newNode = importVisitor(cssPath, node);
@longlho
longlho / transformer-p6.ts
Created August 20, 2019 02:38
transformer-p6.ts
if (newNode) {
// Link external CSS file in source map
const externalCssSource = ts.createSourceMapSource(
cssPath,
readFileSync(cssPath, "utf-8")
);
ts.setSourceMapRange(newNode, {
source: externalCssSource,
pos: node.pos,
end: node.end