Skip to content

Commit 4027825

Browse files
committed
feat: Add preprocessor support to the cli.
1 parent 8fb561a commit 4027825

File tree

10 files changed

+206
-15
lines changed

10 files changed

+206
-15
lines changed

packages/@css-blocks/cli/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
},
4545
"devDependencies": {
4646
"@css-blocks/code-style": "^0.21.0",
47+
"@types/node-sass": "^4.11.0",
4748
"@types/yargs": "^13.0.0",
49+
"node-sass": "^4.11.0",
4850
"typescript": "~3.4.4",
4951
"watch": "^1.0.2"
5052
},

packages/@css-blocks/cli/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BlockFactory, CssBlockError } from "@css-blocks/core";
1+
import { BlockFactory, CssBlockError, Preprocessors } from "@css-blocks/core";
22
import chalk = require("chalk");
33
import fs = require("fs");
44
import fse = require("fs-extra");
@@ -68,7 +68,7 @@ export class CLI {
6868
}
6969

7070
async validate(blockFiles: Array<string>, options: ValidateOptions) {
71-
let preprocessors = options.preprocessors ? require(options.preprocessors) : {};
71+
let preprocessors: Preprocessors = options.preprocessors ? require(path.resolve(options.preprocessors)) : {};
7272
let factory = new BlockFactory({preprocessors});
7373
let errorCount = 0;
7474
for (let blockFile of blockFiles) {

packages/@css-blocks/cli/test/cli-test.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,36 @@ function fixture(...relativePathSegments: Array<string>): string {
77
return path.resolve(__dirname, "..", "..", "test", "fixtures", ...relativePathSegments);
88
}
99

10-
describe("CLI", () => {
10+
function distFile(...relativePathSegments: Array<string>): string {
11+
return path.resolve(__dirname, "..", ...relativePathSegments);
12+
}
13+
14+
describe("validate", () => {
15+
it("can check syntax for a valid block file", async () => {
16+
let cli = new CLI();
17+
await cli.run(["validate", fixture("basic/simple.block.css")]);
18+
assert.equal(cli.output, `ok\t${fixture("basic/simple.block.css")}\n`);
19+
assert.equal(cli.exitCode, 0);
20+
});
21+
it("can check syntax for a bad block file", async () => {
22+
let cli = new CLI();
23+
await cli.run(["validate", fixture("basic/error.block.css")]);
24+
assert.equal(cli.output, `error\t${fixture("basic/error.block.css")}:1:5 Two distinct classes cannot be selected on the same element: .foo.bar\n`);
25+
assert.equal(cli.exitCode, 1);
26+
});
27+
});
28+
29+
describe("validate with preprocessors", () => {
1130
it("can check syntax for a valid block file", async () => {
1231
let cli = new CLI();
13-
await cli.run(["validate", fixture("simple.block.css")]);
14-
assert.equal(cli.output, `ok\t${fixture("simple.block.css")}\n`);
32+
await cli.run(["validate", "--preprocessors", distFile("test/preprocessors"), fixture("scss/simple.block.scss")]);
33+
assert.equal(cli.output, `ok\t${fixture("scss/simple.block.scss")}\n`);
1534
assert.equal(cli.exitCode, 0);
1635
});
1736
it("can check syntax for a bad block file", async () => {
1837
let cli = new CLI();
19-
await cli.run(["validate", fixture("error.block.css")]);
20-
assert.equal(cli.output, `error\t${fixture("error.block.css")}:1:5 Two distinct classes cannot be selected on the same element: .foo.bar\n`);
38+
await cli.run(["validate", "--preprocessors", distFile("test/preprocessors"), fixture("scss/error.block.scss")]);
39+
assert.equal(cli.output, `error\t${fixture("scss/error.block.scss")}:5:5 Two distinct classes cannot be selected on the same element: .foo.bar\n`);
2140
assert.equal(cli.exitCode, 1);
2241
});
2342
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.foo {
2+
color: red;
3+
&.bar {
4+
color: blue;
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:scope {
2+
color: red;
3+
background-color: white;
4+
5+
&[state|dark] {
6+
color: darkred;
7+
background-color: gray;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Preprocessors } from "@css-blocks/core";
2+
import sass = require("node-sass");
3+
4+
const scss: Preprocessors["scss"] = async (fullPath, content, _configuration, _sourceMap) => {
5+
return new Promise((resolve, reject) => {
6+
sass.render(
7+
{
8+
file: fullPath,
9+
outFile: fullPath.replace("scss", "css"),
10+
data: content,
11+
sourceMap: true,
12+
outputStyle: "expanded",
13+
indentedSyntax: false,
14+
},
15+
(err, result) => {
16+
if (err) {
17+
reject(err);
18+
} else {
19+
let resolvedFile = {
20+
content: result.css.toString(),
21+
sourceMap: result.map.toString(),
22+
dependencies: result.stats.includedFiles,
23+
};
24+
resolve(resolvedFile);
25+
}
26+
},
27+
);
28+
});
29+
};
30+
const preprocessors: Preprocessors = { scss };
31+
32+
module.exports = preprocessors;

packages/@css-blocks/cli/tsconfig.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"compilerOptions": {
44
"composite": true,
55
"outDir": "dist",
6-
"baseUrl": "dist"
6+
"baseUrl": "dist",
7+
"paths": {
8+
"sass": ["../../../node_modules/@types/node-sass"]
9+
}
710
},
811
"references": [
912
{"path": "../core"}

0 commit comments

Comments
 (0)