Skip to content

Commit 13acb73

Browse files
authored
Merge pull request #2 from bigardone/master
Tailwind upgrade to v0.6.1
2 parents 6a4e7fa + 2927f20 commit 13acb73

File tree

3 files changed

+35680
-16487
lines changed

3 files changed

+35680
-16487
lines changed

scripts/convert.js

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,68 @@
77

88
// to use this run `node ./convert.js`
99

10-
const postcss = require("postcss");
11-
const _ = require("lodash");
12-
const exportPath = "./src/Tailwind/Classes.elm";
10+
const postcss = require('postcss');
11+
const _ = require('lodash');
1312

14-
fs = require("fs");
13+
const exportPath = './src/Tailwind/Classes.elm';
1514

16-
const css = fs.readFileSync("./scripts/tailwind.css", "utf8"); // minificated version introduces problems with compound selectors
15+
fs = require('fs');
16+
17+
const css = fs.readFileSync('./scripts/tailwind.css', 'utf8'); // minificated version introduces problems with compound selectors
1718

1819
const root = postcss.parse(css);
1920

2021
const classObjs = {};
2122

22-
const defaultIndentation = " ".repeat(4);
23+
const defaultIndentation = ' '.repeat(4);
2324

24-
const ruleFormatter = rule => {
25-
let def = rule.toString().replace("{-", "{ -");
25+
const ruleFormatter = (rule) => {
26+
let def = rule.toString().replace('{-', '{ -');
2627
def = setCorrectIndentation(def);
2728
return def;
2829
};
2930

30-
const setCorrectIndentation = text => {
31+
const setCorrectIndentation = (text) => {
3132
// normalize indentation
3233
if (/ {4}/.test(text)) {
33-
text = text.replace(/\n {2}/g, "\n");
34+
text = text.replace(/\n {2}/g, '\n');
3435
}
3536
// set indentation
36-
text = text.replace(/\n/g, "\n" + defaultIndentation);
37+
text = text.replace(/\n/g, `\n${defaultIndentation}`);
3738
return text;
3839
};
3940

4041
/**
4142
* This will walk through each of the css rules in tailwind
4243
* and pull out the relevent information.
4344
*/
44-
root.walkRules(rule => {
45+
root.walkRules((rule) => {
4546
//
4647
// Ignore anything that's not a class
4748
//
48-
if (!rule.selector.startsWith(".")) return;
49+
if (!rule.selector.startsWith('.')) return;
4950

5051
//
5152
// Ignore responsive variations in favor of using utility classes for that.
5253
//
53-
if (rule.selector.startsWith(".sm\\:")) return;
54-
if (rule.selector.startsWith(".md\\:")) return;
55-
if (rule.selector.startsWith(".lg\\:")) return;
56-
if (rule.selector.startsWith(".xl\\:")) return;
54+
if (rule.selector.startsWith('.sm\\:')) return;
55+
if (rule.selector.startsWith('.md\\:')) return;
56+
if (rule.selector.startsWith('.lg\\:')) return;
57+
if (rule.selector.startsWith('.xl\\:')) return;
5758

5859
let name = rule.selector;
5960

6061
//
6162
// If we're dealing with a rule that's selecting an :after or :before, ignore the rule
6263
//
63-
if (name.indexOf(":after") != -1 || name.indexOf(":before") != -1) {
64+
if (name.indexOf(':after') != -1 || name.indexOf(':before') != -1) {
6465
return;
6566
}
6667

6768
//
6869
// Remove the leading dot
6970
//
70-
name = name.replace(/^\S*\./, "");
71+
name = name.replace(/^\S*\./, '');
7172

7273
//
7374
// Throw away anything after a comma or a space
@@ -78,51 +79,59 @@ root.walkRules(rule => {
7879
//
7980
// Replace the \: with a __
8081
//
81-
elmName = elmName.split("\\:").join("__");
82+
elmName = elmName.split('\\:').join('__');
8283

8384
//
8485
// Replace the negative margin with a 'neg'
8586
//
86-
elmName = elmName.replace(/-m([lrtbxy])/g, "neg_m$1");
87+
elmName = elmName.replace(/-m([lrtbxy])/g, 'neg_m$1');
8788

8889
//
8990
// Change a leading dash to a 'neg'
9091
//
91-
elmName = elmName.replace(/^-/, "neg");
92+
elmName = elmName.replace(/^-/, 'neg');
9293

9394
//
9495
// Replace dashes with underscores
9596
//
96-
elmName = elmName.replace(/-/g, "_");
97+
elmName = elmName.replace(/-/g, '_');
9798

9899
//
99100
// Change the \/ in fractions to `over`
100101
//
101-
elmName = elmName.replace(/\\\//g, "over");
102+
elmName = elmName.replace(/\\\//g, 'over');
103+
104+
//
105+
// Remove :focus
106+
//
107+
elmName = elmName.replace(':focus', '');
108+
109+
//
110+
// Remove :hover
111+
//
112+
elmName = elmName.replace(':hover', '');
102113

103114
//
104115
// Before using "name", but after basing elmName on it, escape the backslack in the Elm string
105116
//
106-
name = name.replace(/\\/g, "\\\\");
117+
name = name.replace(/\\/g, '\\\\');
107118

108119
const obj = {
109120
name,
110121
elmName,
111-
def: ruleFormatter(rule)
122+
def: ruleFormatter(rule),
112123
};
113124

114125
console.log(obj);
115126

116-
if (name in classObjs)
117-
classObjs[name].def += "\n" + defaultIndentation + obj.def; // class has been already registered, only append new def
127+
if (name in classObjs) { classObjs[name].def += `\n${defaultIndentation}${obj.def}`; } // class has been already registered, only append new def
118128
else classObjs[name] = obj;
119129
});
120130

121-
const classes = _(classObjs).sortBy("name");
131+
const classes = _(classObjs).sortBy('name');
122132

123133
// creates an elm variable for each class
124-
const elmify = cl => {
125-
return `
134+
const elmify = cl => `
126135
{-| This class maps to this CSS definition:
127136
128137
${cl.def}
@@ -134,7 +143,6 @@ ${cl.elmName} =
134143
135144
136145
`;
137-
};
138146

139147
// // string of the elm file
140148
const elmString = `
@@ -160,7 +168,7 @@ They do however show the minifed css definition as their comment.
160168
161169
# Classes and their Definitions
162170
163-
@docs ${classes.map(({ elmName }) => elmName).join(", ")}
171+
@docs ${classes.map(({ elmName }) => elmName).join(', ')}
164172
165173
-}
166174
@@ -173,24 +181,24 @@ type TailwindClass = TailwindClass String
173181
sm : TailwindClass -> TailwindClass
174182
sm (TailwindClass c) =
175183
TailwindClass ("sm:" ++ c)
176-
184+
177185
178186
{-| Add a size prefix to the tailwind rule, making it only apply to that screen size and above.
179187
Notice, doesn't make sure the class being passed in is going to work with a responsive prefix.. Not all tailwind rules are responsive-capable.
180188
-}
181189
md : TailwindClass -> TailwindClass
182190
md (TailwindClass c) =
183191
TailwindClass ("md:" ++ c)
184-
185-
192+
193+
186194
{-| Add a size prefix to the tailwind rule, making it only apply to that screen size and above.
187195
Notice, doesn't make sure the class being passed in is going to work with a responsive prefix.. Not all tailwind rules are responsive-capable.
188196
-}
189197
lg : TailwindClass -> TailwindClass
190198
lg (TailwindClass c) =
191199
TailwindClass ("lg:" ++ c)
192200
193-
201+
194202
{-| Add a size prefix to the tailwind rule, making it only apply to that screen size and above.
195203
Notice, doesn't make sure the class being passed in is going to work with a responsive prefix.. Not all tailwind rules are responsive-capable.
196204
-}
@@ -199,13 +207,13 @@ xl (TailwindClass c) =
199207
TailwindClass ("xl:" ++ c)
200208
201209
202-
${classes.map(elmify).join("")}`;
210+
${classes.map(elmify).join('')}`;
203211

204212
// writing the string to the file
205-
fs.writeFile(exportPath, elmString, function(err) {
213+
fs.writeFile(exportPath, elmString, (err) => {
206214
if (err) {
207215
return console.log(err);
208216
}
209217

210-
console.log(exportPath, "was saved!");
218+
console.log(exportPath, 'was saved!');
211219
});

0 commit comments

Comments
 (0)