|
| 1 | +import { |
| 2 | + AnalysisOptions, |
| 3 | + Block, |
| 4 | + BlockFactory, |
| 5 | + CssBlockError, |
| 6 | + Options as CSSBlocksOptions, |
| 7 | + ResolvedConfiguration as CSSBlocksConfiguration, |
| 8 | + StyleMapping, |
| 9 | + TemplateValidatorOptions, |
| 10 | + resolveConfiguration, |
| 11 | +} from "@css-blocks/core"; |
| 12 | +import { Syntax } from "@glimmer/syntax"; |
| 13 | +import { ObjectDictionary, unionInto } from "@opticss/util"; |
| 14 | + |
| 15 | +import { EmberAnalysis } from "./EmberAnalysis"; |
| 16 | +import { FileLocator } from "./FileLocator"; |
| 17 | +import { HandlebarsTemplate, TEMPLATE_TYPE } from "./HandlebarsTemplate"; |
| 18 | +import { TemplateAnalyzingRewriter } from "./TemplateAnalyzingRewriter"; |
| 19 | + |
| 20 | +export type GlimmerStyleMapping = StyleMapping<TEMPLATE_TYPE>; |
| 21 | + |
| 22 | +export interface AnalyzedTemplate { |
| 23 | + template: HandlebarsTemplate; |
| 24 | + block: Block; |
| 25 | + analysis: EmberAnalysis; |
| 26 | +} |
| 27 | + |
| 28 | +export class AnalyzingRewriteManager { |
| 29 | + elementCount: number; |
| 30 | + cssBlocksOpts: CSSBlocksConfiguration; |
| 31 | + validationOptions: TemplateValidatorOptions; |
| 32 | + analysisOptions: AnalysisOptions; |
| 33 | + templateBlocks: ObjectDictionary<Block | undefined>; |
| 34 | + analyses: Map<string, EmberAnalysis>; |
| 35 | + fileLocator: FileLocator; |
| 36 | + blockFactory: BlockFactory; |
| 37 | + possibleStylesheetExtensions: Array<string>; |
| 38 | + templates: Map<string, HandlebarsTemplate>; |
| 39 | + |
| 40 | + constructor( |
| 41 | + blockFactory: BlockFactory, |
| 42 | + fileLocator: FileLocator, |
| 43 | + analysisOptions: AnalysisOptions, |
| 44 | + cssBlocksOpts: CSSBlocksOptions, |
| 45 | + ) { |
| 46 | + this.validationOptions = analysisOptions && analysisOptions.validations || {}; |
| 47 | + this.blockFactory = blockFactory; |
| 48 | + this.fileLocator = fileLocator; |
| 49 | + this.analysisOptions = analysisOptions; |
| 50 | + this.cssBlocksOpts = resolveConfiguration(cssBlocksOpts); |
| 51 | + let extensions = new Set(Object.keys(this.cssBlocksOpts.preprocessors)); |
| 52 | + extensions.add("css"); |
| 53 | + this.possibleStylesheetExtensions = [...extensions]; |
| 54 | + this.elementCount = 0; |
| 55 | + this.templateBlocks = {}; |
| 56 | + this.analyses = new Map(); |
| 57 | + this.templates = new Map(); |
| 58 | + } |
| 59 | + |
| 60 | + async discoverTemplatesWithBlocks(): Promise<number> { |
| 61 | + let count = 0; |
| 62 | + for (let templatePath of this.fileLocator.possibleTemplatePaths()) { |
| 63 | + let stylesheet = this.fileLocator.findStylesheetForTemplate(templatePath, this.possibleStylesheetExtensions); |
| 64 | + if (stylesheet) { |
| 65 | + let block = await this.blockFactory.getBlock(this.fileLocator.blockIdentifier(stylesheet)); |
| 66 | + this.registerTemplate(templatePath, block); |
| 67 | + count++; |
| 68 | + } |
| 69 | + } |
| 70 | + return count; |
| 71 | + } |
| 72 | + |
| 73 | + registerTemplate(template: string, block: Block) { |
| 74 | + this.templateBlocks[template] = block; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param templatePath relative path to template |
| 79 | + */ |
| 80 | + templateAnalyzerAndRewriter(templatePath: string, syntax: Syntax): TemplateAnalyzingRewriter { |
| 81 | + if (this.analyses.get(templatePath)) { |
| 82 | + throw new CssBlockError(`Internal Error: Template at ${templatePath} was already analyzed.`); |
| 83 | + } |
| 84 | + let block = this.templateBlocks[templatePath]; |
| 85 | + let template = new HandlebarsTemplate(templatePath, templatePath); |
| 86 | + this.templates.set(templatePath, template); |
| 87 | + let analysis = new EmberAnalysis(template, this.validationOptions); |
| 88 | + this.analyses.set(templatePath, analysis); |
| 89 | + return new TemplateAnalyzingRewriter(template, block, analysis, this.cssBlocksOpts, syntax); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Iterates through all the analyses objects for all the templates and |
| 94 | + * creates a set of reservedClassNames here. These are used by the block |
| 95 | + * compiler to ensure the classnames that are output don't collide with user |
| 96 | + * specified style aliases. |
| 97 | + */ |
| 98 | + reservedClassNames(): Set<string> { |
| 99 | + let allReservedClassNames = new Set<string>(); |
| 100 | + for (let analysis of this.analyses.values()) { |
| 101 | + unionInto(allReservedClassNames, analysis.reservedClassNames()); |
| 102 | + } |
| 103 | + return allReservedClassNames; |
| 104 | + } |
| 105 | + |
| 106 | + *analyzedTemplates(): Generator<AnalyzedTemplate, void> { |
| 107 | + let templatePaths = Object.keys(this.templateBlocks); |
| 108 | + for (let templatePath of templatePaths) { |
| 109 | + let block = this.templateBlocks[templatePath]!; |
| 110 | + let template = this.templates.get(templatePath); |
| 111 | + if (!template) { |
| 112 | + throw new CssBlockError(`Internal Error: Template at ${templatePath} was not yet analyzed.`); |
| 113 | + } |
| 114 | + let analysis = this.analyses.get(templatePath); |
| 115 | + if (!analysis) { |
| 116 | + throw new CssBlockError(`Internal Error: Template at ${templatePath} was not yet analyzed.`); |
| 117 | + } |
| 118 | + yield { |
| 119 | + template, |
| 120 | + analysis, |
| 121 | + block, |
| 122 | + }; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments