|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | +import { promisify } from "util"; |
| 4 | + |
| 5 | +import { TemplateTypes } from "@opticss/template-api"; |
| 6 | +import { Analyzer } from "css-blocks"; |
| 7 | + |
| 8 | +import { BroccoliPlugin } from "./utils"; |
| 9 | + |
| 10 | +const readdirAsync = promisify(fs.readdirSync) as (path: string) => Promise<string[]>; |
| 11 | +const symlinkAsync = promisify(fs.symlinkSync) as (from: string, to: string) => Promise<void>; |
| 12 | + |
| 13 | +interface BroccoliOptions { |
| 14 | + entry: string[]; |
| 15 | + analyzer: Analyzer<keyof TemplateTypes>; |
| 16 | +} |
| 17 | + |
| 18 | +class BroccoliCSSBlocks extends BroccoliPlugin { |
| 19 | + |
| 20 | + private analyzer: Analyzer<keyof TemplateTypes>; |
| 21 | + private entry: string[]; |
| 22 | + |
| 23 | + // tslint:disable-next-line:prefer-whatever-to-any |
| 24 | + constructor(inputNode: any, options: BroccoliOptions) { |
| 25 | + super([inputNode], { |
| 26 | + name: "broccoli-css-blocks", |
| 27 | + }); |
| 28 | + this.analyzer = options.analyzer; |
| 29 | + this.entry = options.entry; |
| 30 | + } |
| 31 | + |
| 32 | + async build() { |
| 33 | + |
| 34 | + // This build step is just a pass-through of all files! |
| 35 | + // We're just analyzing right now. |
| 36 | + let files = await readdirAsync(this.inputPaths[0]); |
| 37 | + for (let file of files) { |
| 38 | + try { |
| 39 | + await symlinkAsync( |
| 40 | + path.join(this.inputPaths[0], file), |
| 41 | + path.join(this.outputPath, file), |
| 42 | + ); |
| 43 | + } catch (e) { |
| 44 | + console.log("Error linking", path.join(this.inputPaths[0], file), "to output directory."); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Oh hey look, we're analyzing. |
| 49 | + await this.analyzer.analyze(...this.entry); |
| 50 | + |
| 51 | + // Here we'd compile the blocks, optionally optimize our output, |
| 52 | + // and inject the final CSS file into the tree. Then, attach our |
| 53 | + // StyleMapping data to whatever shared memory data transport we |
| 54 | + // have to pass to funnel rewrite data to our Rewriter. |
| 55 | + |
| 56 | + return this.analyzer; |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +module.exports = BroccoliCSSBlocks; |
0 commit comments