|
| 1 | +/** |
| 2 | + * TypeScript transform to remove comments and unnecessary white space from GLSL source. |
| 3 | + * A template string is considered GLSL source if: |
| 4 | + a) the file matches the pattern specified in the plugin config; or |
| 5 | + b) it is tagged as glsl`...` |
| 6 | + * Usage with ts-patch: |
| 7 | + { |
| 8 | + "plugins": [ |
| 9 | + { |
| 10 | + "transform": "ocular-dev-tools/ts-transform-remove-glsl-comments", |
| 11 | + "pattern": ["*.glsl.ts"] |
| 12 | + } |
| 13 | + ] |
| 14 | + } |
| 15 | + */ |
| 16 | +import * as path from 'path'; |
| 17 | +import type {Program, TransformationContext, SourceFile, Node} from 'typescript'; |
| 18 | +import type {TransformerExtras, PluginConfig} from 'ts-patch'; |
| 19 | +import minimatch from 'minimatch'; |
| 20 | + |
| 21 | +// inline comment is only safe to remove if it's followed by a return (i.e. end of comment) |
| 22 | +const INLINE_COMMENT_REGEX = /\s*\/\/.*[\n\r]/g; |
| 23 | +const BLOCK_COMMENT_REGEX = /\s*\/\*(\*(?!\/)|[^*])*\*\//g; |
| 24 | +const WHITESPACE_REGEX = /\s*[\n\r]\s*/gm; |
| 25 | +const DEFAULT_PATTERNS = []; |
| 26 | + |
| 27 | +type RemoveGLSLCommentsPluginConfig = PluginConfig & { |
| 28 | + /** Glob patterns of shader files to include. */ |
| 29 | + pattern?: string[]; |
| 30 | +}; |
| 31 | + |
| 32 | +export default function ( |
| 33 | + program: Program, |
| 34 | + pluginConfig: RemoveGLSLCommentsPluginConfig, |
| 35 | + {ts}: TransformerExtras |
| 36 | +) { |
| 37 | + const {pattern = DEFAULT_PATTERNS} = pluginConfig; |
| 38 | + |
| 39 | + return (ctx: TransformationContext) => { |
| 40 | + const {factory} = ctx; |
| 41 | + |
| 42 | + return (sourceFile: SourceFile) => { |
| 43 | + const isShaderFile = matchFilePath(sourceFile.fileName, pattern); |
| 44 | + |
| 45 | + function replaceShaderString(node: Node): Node { |
| 46 | + if (ts.isNoSubstitutionTemplateLiteral(node)) { |
| 47 | + const text = node.rawText ?? ''; |
| 48 | + // Convert source text to string content |
| 49 | + const newText = filterShaderSource(text); |
| 50 | + if (newText === text) { |
| 51 | + return node; |
| 52 | + } |
| 53 | + return factory.createNoSubstitutionTemplateLiteral(newText, newText); |
| 54 | + } |
| 55 | + if (ts.isTemplateLiteralToken(node)) { |
| 56 | + const text = node.rawText ?? ''; |
| 57 | + const newText = filterShaderSource(text); |
| 58 | + if (newText === text) { |
| 59 | + return node; |
| 60 | + } |
| 61 | + if (ts.isTemplateHead(node)) { |
| 62 | + return factory.createTemplateHead(newText, newText); |
| 63 | + } |
| 64 | + if (ts.isTemplateMiddle(node)) { |
| 65 | + return factory.createTemplateMiddle(newText, newText); |
| 66 | + } |
| 67 | + if (ts.isTemplateTail(node)) { |
| 68 | + return factory.createTemplateTail(newText, newText); |
| 69 | + } |
| 70 | + return node; |
| 71 | + } |
| 72 | + return ts.visitEachChild(node, replaceShaderString, ctx); |
| 73 | + } |
| 74 | + |
| 75 | + function visit(node: Node): Node { |
| 76 | + if ( |
| 77 | + ts.isTaggedTemplateExpression(node) && |
| 78 | + // First child is the tag identifier |
| 79 | + node.getChildAt(0).getText() === 'glsl' |
| 80 | + ) { |
| 81 | + // Strip the template tag |
| 82 | + return replaceShaderString(node.getChildAt(1)); |
| 83 | + } |
| 84 | + if (isShaderFile && ts.isTemplateLiteral(node)) { |
| 85 | + return replaceShaderString(node); |
| 86 | + } |
| 87 | + return ts.visitEachChild(node, visit, ctx); |
| 88 | + } |
| 89 | + return ts.visitNode(sourceFile, visit); |
| 90 | + }; |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +function matchFilePath(filePath: string, includePatterns: string[]): boolean { |
| 95 | + const relPath = path.relative(process.env.PWD ?? '', filePath); |
| 96 | + for (const pattern of includePatterns) { |
| 97 | + if (minimatch(relPath, pattern)) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + } |
| 101 | + return false; |
| 102 | +} |
| 103 | + |
| 104 | +function filterShaderSource(source: string): string { |
| 105 | + return source |
| 106 | + .replace(INLINE_COMMENT_REGEX, '\n') |
| 107 | + .replace(BLOCK_COMMENT_REGEX, '') |
| 108 | + .replace(WHITESPACE_REGEX, '\n'); |
| 109 | +} |
0 commit comments