Skip to content

Commit 6ba4a29

Browse files
committed
feat: Broccoli plugin for css-blocks.
1 parent b505b27 commit 6ba4a29

File tree

9 files changed

+192
-0
lines changed

9 files changed

+192
-0
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "@css-blocks/broccoli",
3+
"version": "0.0.1",
4+
"description": "CSS Blocks Broccoli Addon",
5+
"main": "dist/src/index.js",
6+
"author": "Adam Miller <[email protected]>",
7+
"license": "MIT",
8+
"keywords": [
9+
"css-blocks",
10+
"css blocks",
11+
"broccoli-plugin"
12+
],
13+
"scripts": {
14+
"test": "mocha --opts test/mocha.opts dist/test",
15+
"compile": "rm -rf dist && tsc -p tsconfig.json",
16+
"pretest": "yarn run compile",
17+
"posttest": "yarn run lint",
18+
"prepublish": "yarn run compile && yarn run lintall",
19+
"lint": "tslint -t msbuild --project . -c tslint.cli.json",
20+
"lintall": "tslint -t msbuild --project . -c tslint.release.json",
21+
"lintfix": "tslint -t msbuild --project . -c tslint.cli.json --fix",
22+
"coverage": "istanbul cover -i dist/src/**/*.js --dir ./build/coverage node_modules/mocha/bin/_mocha -- dist/test --opts test/mocha.opts",
23+
"remap": "remap-istanbul -i build/coverage/coverage.json -o coverage -t html",
24+
"docs": "typedoc --out ./docs .",
25+
"watch": "watch 'yarn run test' src test types-local --wait=1"
26+
},
27+
"devDependencies": {
28+
"@css-blocks/code-style": "^0.17.0"
29+
},
30+
"dependencies": {
31+
"@glimmer/compiler": "^0.33.0",
32+
"@glimmer/syntax": "^0.33.0",
33+
"broccoli-funnel": "^2.0.1",
34+
"broccoli-merge-trees": "^3.0.0",
35+
"broccoli-plugin": "^1.3.0",
36+
"colors": "^1.2.1",
37+
"css-blocks": "^0.17.0",
38+
"debug": "^3.1.0",
39+
"opticss": "file:../../../opticss/packages/opticss",
40+
"postcss": "^6.0.19",
41+
"walk-sync": "^0.3.2"
42+
}
43+
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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;
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* This file takes heavy inspiration from other Broccoli Plugins written in
3+
* Typescript to provide sane typings for common Broccoli utilities.
4+
*/
5+
/* tslint:disable */
6+
export const BroccoliPlugin: BroccoliPlugin.Static = require("broccoli-plugin");
7+
export const walkSync: WalkSync = require("walk-sync");
8+
9+
declare function require(id: string): any;
10+
11+
export namespace BroccoliPlugin {
12+
export interface PluginOptions {
13+
name?: string;
14+
annotation?: string;
15+
persistentOutput?: boolean;
16+
}
17+
18+
export interface Plugin {
19+
inputPaths: string[];
20+
outputPath: string;
21+
cachePath: string;
22+
}
23+
24+
export interface Static {
25+
new(inputNodes: any[], options?: any): Plugin;
26+
}
27+
}
28+
29+
export interface WalkSync {
30+
(path: string, options?: any): string[];
31+
entries(path: string, options?: any): WalkSync.Entry[];
32+
}
33+
34+
export namespace WalkSync {
35+
export type Row = string | RegExp[];
36+
37+
export interface Entry {
38+
relativePath: string;
39+
basePath: string;
40+
fullPath: string;
41+
checksum: string;
42+
mode: number;
43+
size: number;
44+
mtime: Date;
45+
isDirectory(): boolean;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as assert from "assert";
2+
3+
describe("Broccoli Plugin Test", () => {
4+
it("runs tests", () => {
5+
assert.ok(1);
6+
});
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": [
4+
"src"
5+
]
6+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"baseUrl": "dist"
6+
},
7+
"include": [
8+
"src",
9+
"test"
10+
],
11+
"exclude": [
12+
"sanity",
13+
"dist",
14+
"node_modules"
15+
]
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "http://json.schemastore.org/tslint",
3+
"extends": "@css-blocks/code-style/configs/tslint.cli.json"
4+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@css-blocks/code-style"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "http://json.schemastore.org/tslint",
3+
"extends": "@css-blocks/code-style/configs/tslint.release.json"
4+
}

0 commit comments

Comments
 (0)