Skip to content

Commit aaed465

Browse files
committed
first
0 parents  commit aaed465

File tree

7 files changed

+272
-0
lines changed

7 files changed

+272
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/*

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
js4
2+
===
3+
4+
Write CSS with ES6 JavaScript.
5+
6+
# Install
7+
8+
```bash
9+
npm install -g js4 # may require sudo
10+
```
11+
12+
# Usage
13+
14+
Write your CSS in JS in a file, for example `css.js`:
15+
16+
```js
17+
new Rule('body', { color : 'red' });
18+
19+
new Rule('#id, .class > [attr]', { 'text-align' : 'center' });
20+
```
21+
22+
Then compile it from terminal:
23+
24+
```bash
25+
js4 css.js
26+
```
27+
28+
Outputs:
29+
30+
```css
31+
body {
32+
color: red;
33+
}
34+
35+
#id, .class > [attr] {
36+
text-align: center;
37+
}
38+
```
39+
40+
To compile to a source file:
41+
42+
```bash
43+
js4 css.js > css.css
44+
```
45+
46+
# Unleash the power of JavaScript
47+
48+
Now you can use JS to do advanced stuff in your CSS such as declarations or scopes:
49+
50+
```js
51+
52+
const COLOR = 'red';
53+
54+
for ( let i = 0; i < 10; i ++ ) {
55+
new Rule(`#p-${i}`, { color : COLOR, width : `${i * 25}px` });
56+
}
57+
```
58+
59+
# Mixins
60+
61+
You can use mixin and reusable rules such as:
62+
63+
```js
64+
let reusable = new Rule('.foo', { color : 'red' });
65+
66+
new Rule('.bar', { reusable, 'padding' : '10px' });
67+
```
68+
69+
```css
70+
.foo {
71+
color: red;
72+
}
73+
74+
.bar {
75+
color: red;
76+
padding: 10px;
77+
}
78+
```
79+
80+
If you don't want the mixin to appear on CSS, do like this:
81+
82+
```js
83+
let reusable = new Rule('.foo', { color : 'red' }, { render : false });
84+
85+
new Rule('.bar', { reusable, 'padding' : '10px' });
86+
```

app/js4.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
class Rule {
4+
constructor (selector, attributes = {}, options = {}) {
5+
this.selector = selector;
6+
this.attributes = attributes;
7+
this.options = options;
8+
9+
rules.push(this);
10+
}
11+
12+
output (prop, val) {
13+
let ret = '';
14+
15+
if ( typeof val === 'string' ) {
16+
ret += ` ${prop}: ${val};\n`;
17+
}
18+
else if ( typeof val === 'object' ) {
19+
for ( let prop in val ) {
20+
ret += this.output(prop, val[prop]);
21+
}
22+
}
23+
24+
return ret;
25+
}
26+
27+
toString () {
28+
if ( this.options.render === false ) {
29+
return '';
30+
}
31+
32+
let output = `${this.selector} {\n`;
33+
34+
for ( let rule in this.attributes ) {
35+
output += this.output(rule, this.attributes[rule]);
36+
}
37+
38+
output += '}';
39+
40+
return output;
41+
}
42+
}
43+
44+
export default Rule;

app/render.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
import fs from 'fs';
6+
import babel from 'babel';
7+
import Rule from './js4';
8+
9+
global.Rule = Rule;
10+
global.rules = [];
11+
12+
let src = process.argv[2];
13+
14+
require('babel/register')({
15+
modules : 'common',
16+
stage : 0
17+
})
18+
19+
let js4 = require(src);
20+
21+
let output = rules.map(rule => rule.toString());
22+
23+
console.log(output.join("\n"));

dist/js4.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, '__esModule', {
4+
value: true
5+
});
6+
7+
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
8+
9+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
10+
11+
var Rule = (function () {
12+
function Rule(selector) {
13+
var attributes = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
14+
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
15+
16+
_classCallCheck(this, Rule);
17+
18+
this.selector = selector;
19+
this.attributes = attributes;
20+
this.options = options;
21+
22+
rules.push(this);
23+
}
24+
25+
_createClass(Rule, [{
26+
key: 'output',
27+
value: function output(prop, val) {
28+
var ret = '';
29+
30+
if (typeof val === 'string') {
31+
ret += ' ' + prop + ': ' + val + ';\n';
32+
} else if (typeof val === 'object') {
33+
for (var _prop in val) {
34+
ret += this.output(_prop, val[_prop]);
35+
}
36+
}
37+
38+
return ret;
39+
}
40+
}, {
41+
key: 'toString',
42+
value: function toString() {
43+
if (this.options.render === false) {
44+
return '';
45+
}
46+
47+
var output = this.selector + ' {\n';
48+
49+
for (var rule in this.attributes) {
50+
output += this.output(rule, this.attributes[rule]);
51+
}
52+
53+
output += '}';
54+
55+
return output;
56+
}
57+
}]);
58+
59+
return Rule;
60+
})();
61+
62+
exports['default'] = Rule;
63+
module.exports = exports['default'];

dist/render.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env node
2+
3+
4+
'use strict';
5+
6+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
7+
8+
var _fs = require('fs');
9+
10+
var _fs2 = _interopRequireDefault(_fs);
11+
12+
var _babel = require('babel');
13+
14+
var _babel2 = _interopRequireDefault(_babel);
15+
16+
var _js4 = require('./js4');
17+
18+
var _js42 = _interopRequireDefault(_js4);
19+
20+
global.Rule = _js42['default'];
21+
global.rules = [];
22+
23+
var src = process.argv[2];
24+
25+
require('babel/register')({
26+
modules: 'common',
27+
stage: 0
28+
});
29+
30+
var js4 = require(src);
31+
32+
var output = rules.map(function (rule) {
33+
return rule.toString();
34+
});
35+
36+
console.log(output.join("\n"));

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "js4",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"bin": {
7+
"js4" : "node dist/render"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1",
11+
"transpile": "babel app/ --modules common --stage 1 --out-dir dist;",
12+
"hot-transpile": "babel app/ --watch --modules common --stage 1 --out-dir dist;"
13+
},
14+
"author": "",
15+
"license": "ISC",
16+
"dependencies": {
17+
"babel": "^5.8.20"
18+
}
19+
}

0 commit comments

Comments
 (0)