Skip to content

Commit fa2a536

Browse files
committed
fix: Fix problems with JSX and Webpack integrations.
- Parse any imported css file as a block file, not just those ending in .block.css. - Rewrite to 'className' attribute, not 'class'. - Add '.js' to list of Analyzable file extensions.
1 parent 72463eb commit fa2a536

File tree

6 files changed

+38
-14
lines changed

6 files changed

+38
-14
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"@types/chai": "^3.5.2",
2020
"@types/debug": "0.0.29",
2121
"@types/loader-utils": "^1.1.3",
22-
"@types/minimatch": "^2.0.29",
2322
"@types/mocha": "^2.2.41",
2423
"@types/node": "^8.0.0",
2524
"@types/prettier": "^1.8.0",

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

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"babel-types": "7.0.0-beta.3",
6161
"babylon": "7.0.0-beta.46",
6262
"debug": "^2.6.8",
63-
"minimatch": "^3.0.4",
6463
"object.values": "^1.0.4",
6564
"opticss": "^0.3.0"
6665
}

packages/@css-blocks/jsx/src/transformer/babel.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface CssBlocksVisitor {
5252
}
5353

5454
const CAN_PARSE_EXTENSIONS = {
55+
".js": true,
5556
".tsx": true,
5657
".jsx": true,
5758
};
@@ -166,7 +167,7 @@ export function makePlugin(transformOpts: { rewriter: Rewriter }): () => PluginO
166167
attributeValue = stringLiteral(classMapping.staticClasses.join(" "));
167168
}
168169
if (attributeValue) {
169-
newClassAttr = jSXAttribute(jSXIdentifier("class"), attributeValue);
170+
newClassAttr = jSXAttribute(jSXIdentifier("className"), attributeValue);
170171
}
171172

172173
let classAttrs = this.elementAnalyzer.classAttributePaths(path);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as minimatch from "minimatch";
2-
const BLOCK_PATTERN = new minimatch.Minimatch("*.block.*", { matchBase: true });
1+
import * as path from "path";
32
export function isBlockFilename(filename: string): boolean {
4-
return BLOCK_PATTERN.match(filename);
3+
return path.parse(filename).ext === ".css";
54
}

packages/@css-blocks/jsx/test/analyzer/typed-file-analysis.ts

+34-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,39 @@ export class Test {
1212
mock.restore();
1313
}
1414

15+
@test "Is able to parse vanilla without a `jsx` extension."() {
16+
mock({
17+
"bar.css": `
18+
:scope { color: blue; }
19+
.pretty { color: red; }
20+
.pretty[state|color=yellow] {
21+
color: yellow;
22+
}
23+
`,
24+
});
25+
26+
return parse(`
27+
import bar from 'bar.css';
28+
import objstr from 'obj-str';
29+
30+
let style = objstr({
31+
[bar.pretty]: true,
32+
[bar.pretty.color("yellow")]: true
33+
});
34+
35+
<div class={style}></div>;`,
36+
"test-file.js",
37+
{ types: "none" },
38+
).then((analyzer: Analyzer) => {
39+
let result = analyzer.serialize();
40+
let analysis = result.analyses[0];
41+
assert.deepEqual(analysis.stylesFound, ["bar.pretty", "bar.pretty[state|color=yellow]"]);
42+
});
43+
}
44+
1545
@test "Is able to parse typescript"() {
1646
mock({
17-
"bar.block.css": `
47+
"bar.css": `
1848
:scope { color: blue; }
1949
.pretty { color: red; }
2050
.pretty[state|color=yellow] {
@@ -24,7 +54,7 @@ export class Test {
2454
});
2555

2656
return parse(`
27-
import bar from 'bar.block.css';
57+
import bar from 'bar.css';
2858
import objstr from 'obj-str';
2959
3060
function fooGood<T extends { x: number }>(obj: T): T {
@@ -54,7 +84,7 @@ export class Test {
5484

5585
@test "Is able to parse flow"() {
5686
mock({
57-
"bar.block.css": `
87+
"bar.css": `
5888
:scope { color: blue; }
5989
.pretty { color: red; }
6090
.pretty[state|color=yellow] {
@@ -64,7 +94,7 @@ export class Test {
6494
});
6595

6696
return parse(`
67-
import bar from 'bar.block.css';
97+
import bar from 'bar.css';
6898
import objstr from 'obj-str';
6999
70100
let color: string = "yellow";

yarn.lock

-4
Original file line numberDiff line numberDiff line change
@@ -1070,10 +1070,6 @@
10701070
version "3.0.3"
10711071
resolved "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
10721072

1073-
"@types/minimatch@^2.0.29":
1074-
version "2.0.29"
1075-
resolved "https://registry.npmjs.org/@types/minimatch/-/minimatch-2.0.29.tgz#5002e14f75e2d71e564281df0431c8c1b4a2a36a"
1076-
10771073
"@types/mocha@^2.2.41":
10781074
version "2.2.48"
10791075
resolved "https://registry.npmjs.org/@types/mocha/-/mocha-2.2.48.tgz#3523b126a0b049482e1c3c11877460f76622ffab"

0 commit comments

Comments
 (0)