This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const emitResult = program.emit( | |
undefined, | |
(fileName, content) => { | |
ts.sys.writeFile(fileName, `/* @generated */${ts.sys.newLine}${content}`); | |
}, | |
undefined, | |
undefined, | |
{ | |
before: [ | |
inlineImg({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function transform(opts: YourTransformerOpts): ts.TransformerFactory<ts.SourceFile> { | |
return ctx => sf => transformNode(sf, opts) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |