|
1 | 1 | // TODO: figuire out how to use this on rollup.config.js |
2 | 2 | import * as ts from 'typescript'; |
3 | 3 |
|
4 | | -export default function transformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile> { |
5 | | - return (context: ts.TransformationContext) => (file: ts.SourceFile) => changeTransform(program, context, file); |
| 4 | +export default function transformer(program: ts.Program): Array<ts.TransformerFactory<ts.SourceFile>> { |
| 5 | + const tsSourceTransformers: Array<ts.TransformerFactory<ts.SourceFile>> = [ |
| 6 | + (context: ts.TransformationContext) => (file: ts.SourceFile) => extendClass(program, context, file), |
| 7 | + (context: ts.TransformationContext) => (file: ts.SourceFile) => addImports(context, file) |
| 8 | + ] |
| 9 | + return tsSourceTransformers; |
6 | 10 | } |
7 | 11 |
|
8 | 12 | // Simple transform add extends CustomElement |
9 | | -export function changeTransform(program: ts.Program, context: ts.TransformationContext, sf: ts.SourceFile) { |
| 13 | +export function extendClass(program: ts.Program, context: ts.TransformationContext, sf: ts.SourceFile) { |
10 | 14 | const visitor: ts.Visitor = (node) => { |
11 | 15 | // Add CustomHTMLElement to import |
12 | 16 | if (!ts.isClassDeclaration(node)) { |
13 | | - if (ts.isImportDeclaration(node) && isElementImport(node, sf)) { |
14 | | - const { importClause: { namedBindings } } = node |
15 | | - const { elements } = namedBindings as ts.NamedImports |
16 | | - |
17 | | - const specifiers: Array<ts.ImportSpecifier> = [...elements] |
18 | | - |
19 | | - const hasCustomHTMLElementImport = elements.some(n => n.getText().indexOf('CustomHTMLElement') > -1) |
20 | | - |
21 | | - if (!hasCustomHTMLElementImport) { |
22 | | - specifiers.push(ts.createImportSpecifier(void 0, ts.createIdentifier('CustomHTMLElement'))) |
23 | | - } |
24 | | - |
25 | | - return ts.createImportDeclaration( |
26 | | - [], |
27 | | - node.modifiers, |
28 | | - ts.createImportClause(void 0, ts.createNamedImports(specifiers)), |
29 | | - node.moduleSpecifier |
30 | | - ) |
31 | | - } |
32 | 17 | return ts.visitEachChild(node, visitor, context); |
33 | 18 | } |
34 | 19 |
|
@@ -67,6 +52,31 @@ export function changeTransform(program: ts.Program, context: ts.TransformationC |
67 | 52 | return ts.visitNode(sf, visitor); |
68 | 53 | }; |
69 | 54 |
|
| 55 | +export function addImports(context: ts.TransformationContext, sf: ts.SourceFile) { |
| 56 | + const visitor: ts.Visitor = (node) => { |
| 57 | + if (ts.isImportDeclaration(node) && isElementImport(node, sf)) { |
| 58 | + const { importClause: { namedBindings } } = node |
| 59 | + const { elements } = namedBindings as ts.NamedImports |
| 60 | + |
| 61 | + const specifiers: Array<ts.ImportSpecifier> = [...elements] |
| 62 | + |
| 63 | + const hasCustomHTMLElementImport = elements.some(n => n.getText().indexOf('CustomHTMLElement') > -1) |
| 64 | + |
| 65 | + if (!hasCustomHTMLElementImport) { |
| 66 | + specifiers.push(ts.createImportSpecifier(void 0, ts.createIdentifier('CustomHTMLElement'))) |
| 67 | + } |
| 68 | + |
| 69 | + return ts.createImportDeclaration( |
| 70 | + [], |
| 71 | + node.modifiers, |
| 72 | + ts.createImportClause(void 0, ts.createNamedImports(specifiers)), |
| 73 | + node.moduleSpecifier |
| 74 | + ) |
| 75 | + } |
| 76 | + return ts.visitEachChild(node, visitor, context); |
| 77 | + }; |
| 78 | + return ts.visitNode(sf, visitor); |
| 79 | +}; |
70 | 80 |
|
71 | 81 | function isElementDecorator(node: ts.Decorator, typeChecker: ts.TypeChecker): boolean { |
72 | 82 | if (!ts.isCallExpression(node.expression)) { |
|
0 commit comments