|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import ts from 'typescript'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Creates a TypeScript Transformer to process Worker and SharedWorker entry points and transform |
| 13 | + * the URL instances to reference the built and bundled worker code. This uses a callback process |
| 14 | + * similar to the component stylesheets to allow the main esbuild plugin to process files as needed. |
| 15 | + * Unsupported worker expressions will be left in their origin form. |
| 16 | + * @param getTypeChecker A function that returns a TypeScript TypeChecker instance for the program. |
| 17 | + * @returns A TypeScript transformer factory. |
| 18 | + */ |
| 19 | +export function createWorkerTransformer( |
| 20 | + fileProcessor: (file: string, importer: string) => string, |
| 21 | +): ts.TransformerFactory<ts.SourceFile> { |
| 22 | + return (context: ts.TransformationContext) => { |
| 23 | + const nodeFactory = context.factory; |
| 24 | + |
| 25 | + const visitNode: ts.Visitor = (node: ts.Node) => { |
| 26 | + // Check if the node is a valid new expression for a Worker or SharedWorker |
| 27 | + // TODO: Add global scope check |
| 28 | + if ( |
| 29 | + !ts.isNewExpression(node) || |
| 30 | + !ts.isIdentifier(node.expression) || |
| 31 | + (node.expression.text !== 'Worker' && node.expression.text !== 'SharedWorker') |
| 32 | + ) { |
| 33 | + // Visit child nodes of non-Worker expressions |
| 34 | + return ts.visitEachChild(node, visitNode, context); |
| 35 | + } |
| 36 | + |
| 37 | + // Worker should have atleast one argument but not more than two |
| 38 | + if (!node.arguments || node.arguments.length < 1 || node.arguments.length > 2) { |
| 39 | + return node; |
| 40 | + } |
| 41 | + |
| 42 | + // First argument must be a new URL expression |
| 43 | + const workerUrlNode = node.arguments[0]; |
| 44 | + // TODO: Add global scope check |
| 45 | + if ( |
| 46 | + !ts.isNewExpression(workerUrlNode) || |
| 47 | + !ts.isIdentifier(workerUrlNode.expression) || |
| 48 | + workerUrlNode.expression.text !== 'URL' |
| 49 | + ) { |
| 50 | + return node; |
| 51 | + } |
| 52 | + |
| 53 | + // URL must have 2 arguments |
| 54 | + if (!workerUrlNode.arguments || workerUrlNode.arguments.length !== 2) { |
| 55 | + return node; |
| 56 | + } |
| 57 | + |
| 58 | + // URL arguments must be a string and then `import.meta.url` |
| 59 | + if ( |
| 60 | + !ts.isStringLiteralLike(workerUrlNode.arguments[0]) || |
| 61 | + !ts.isPropertyAccessExpression(workerUrlNode.arguments[1]) || |
| 62 | + !ts.isMetaProperty(workerUrlNode.arguments[1].expression) || |
| 63 | + workerUrlNode.arguments[1].name.text !== 'url' |
| 64 | + ) { |
| 65 | + return node; |
| 66 | + } |
| 67 | + |
| 68 | + const filePath = workerUrlNode.arguments[0].text; |
| 69 | + const importer = node.getSourceFile().fileName; |
| 70 | + |
| 71 | + // Process the file |
| 72 | + const replacementPath = fileProcessor(filePath, importer); |
| 73 | + |
| 74 | + // Update if the path changed |
| 75 | + if (replacementPath !== filePath) { |
| 76 | + return nodeFactory.updateNewExpression( |
| 77 | + node, |
| 78 | + node.expression, |
| 79 | + node.typeArguments, |
| 80 | + // Update Worker arguments |
| 81 | + ts.setTextRange( |
| 82 | + nodeFactory.createNodeArray( |
| 83 | + [ |
| 84 | + nodeFactory.updateNewExpression( |
| 85 | + workerUrlNode, |
| 86 | + workerUrlNode.expression, |
| 87 | + workerUrlNode.typeArguments, |
| 88 | + // Update URL arguments |
| 89 | + ts.setTextRange( |
| 90 | + nodeFactory.createNodeArray( |
| 91 | + [ |
| 92 | + nodeFactory.createStringLiteral(replacementPath), |
| 93 | + workerUrlNode.arguments[1], |
| 94 | + ], |
| 95 | + workerUrlNode.arguments.hasTrailingComma, |
| 96 | + ), |
| 97 | + workerUrlNode.arguments, |
| 98 | + ), |
| 99 | + ), |
| 100 | + node.arguments[1], |
| 101 | + ], |
| 102 | + node.arguments.hasTrailingComma, |
| 103 | + ), |
| 104 | + node.arguments, |
| 105 | + ), |
| 106 | + ); |
| 107 | + } else { |
| 108 | + return node; |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + return (sourceFile) => { |
| 113 | + // Skip transformer if there are no Workers |
| 114 | + if (!sourceFile.text.includes('Worker')) { |
| 115 | + return sourceFile; |
| 116 | + } |
| 117 | + |
| 118 | + return ts.visitEachChild(sourceFile, visitNode, context); |
| 119 | + }; |
| 120 | + }; |
| 121 | +} |
0 commit comments