Skip to content

Commit 931151a

Browse files
author
Krasimir Tsonev
committed
Adding support of JSON importing
1 parent 536ad09 commit 931151a

File tree

6 files changed

+84
-20
lines changed

6 files changed

+84
-20
lines changed

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ For AbsurdJS, the following code is valid CSS:
1515

1616
*type*, *elementstyle* and *responsive* are just [plugins](https://github.com/krasimir/absurd#plugins) which return pure CSS.
1717

18-
19-
2018
======
2119

2220
- [Installation](#installation)
@@ -70,6 +68,27 @@ After that, in your nodejs application */app.js*
7068
// ...
7169
});
7270

71+
### Importing JSON
72+
73+
The library supports importing of pure JSON. Of course this approach doesn't give you an access to the API, but it is still useful if you want to skip the typical Nodejs module definition. For example:
74+
75+
// styles.json
76+
{
77+
"body": {
78+
"margin": "0",
79+
"padding": "0",
80+
"fontSize": "1em",
81+
"p": {
82+
"line-height": "30px"
83+
}
84+
}
85+
}
86+
87+
// app.js
88+
Absurd("styles.json").compile(function(err, css) {
89+
// ...
90+
});
91+
7392
### Compiling to file
7493

7594
var output = "./css/styles.css";
@@ -224,12 +243,14 @@ Of course you can't put everything in a single file. That's why there is *.impor
224243
api.import(__dirname + '/config/colors.js');
225244
api.import(__dirname + '/config/sizes.js');
226245
api.import([
227-
__dirname + '/layout/grid.js',
228-
__dirname + '/forms/login-form.js',
229-
__dirname + '/forms/feedback-form.js'
246+
__dirname + '/layout/grid.json',
247+
__dirname + '/forms/login-form.json',
248+
__dirname + '/forms/feedback-form.json'
230249
]);
231250
}
232251

252+
Pay attention to the types of the files. The first two are actually JavaScript files, because they need an access to the API (to define mixins, plugins etc ...). The second *import* statement adds the actual styling. Of course it is not necessary to use this approach, but writing pure JSON sometimes may be a better option.
253+
233254
### Using variables
234255

235256
There is no need to use something special. Because that's pure javascript you may write:
@@ -417,6 +438,8 @@ is compiled to
417438
]);
418439
}
419440

441+
AbsurdJS supports importing of JavaScript and JSON files.
442+
420443
### .storage([key], [value])
421444

422445
Setting value:

index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ module.exports = absurd = function(path) {
2727
_path(API);
2828
} else {
2929
if(_path !== false) {
30-
try {
31-
requireUncached(_path.source)(API);
32-
} catch(err) {
33-
console.log("Error: I can't find '" + _path.source + "'.");
34-
}
30+
API.import(_path.source);
3531
}
3632
}
3733
Processor(

lib/api/import.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
var requireUncached = require('../helpers/RequireUncached.js');
1+
var requireUncached = require('../helpers/RequireUncached.js'),
2+
fs = require("fs");
23
module.exports = function(API) {
3-
return function(path) {
4-
if(typeof path == 'string') {
4+
var importFile = function(path) {
5+
if(path.split('.').pop().toLowerCase() === "json") {
6+
try {
7+
var jsonData = fs.readFileSync(path, {encoding: "utf8"});
8+
var json = JSON.parse(jsonData);
9+
API.add(json);
10+
} catch(err) {
11+
console.log("Error during importing of '" + path + "'", err);
12+
}
13+
} else {
514
try {
615
requireUncached(path)(API)
716
} catch(err) {
8-
console.log("Error: I can't find '" + path + "'.");
17+
console.log("Error: I can't find '" + path + "'.", err);
918
}
19+
}
20+
}
21+
return function(path) {
22+
if(typeof path == 'string') {
23+
importFile(path);
1024
} else {
1125
for(var i=0; p=path[i]; i++) {
12-
try {
13-
requireUncached(p)(API);
14-
} catch(err) {
15-
console.log("Error: I can't find '" + p + "'.");
16-
}
26+
importFile(p);
1727
}
1828
}
1929
return API;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "absurd",
3-
"version": "0.0.20",
3+
"version": "0.0.21",
44
"homepage": "https://github.com/krasimir/absurd",
55
"description": "CSS preprocessor",
66
"main": "index.js",

tests/data/styles.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"body": {
3+
"margin": "0",
4+
"padding": "0",
5+
"fontSize": "1em",
6+
"p": {
7+
"line-height": "30px"
8+
}
9+
}
10+
}

tests/using.json.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
describe("Import JSON instead of .js file", function() {
2+
3+
var Absurd = require('../index.js');
4+
5+
it("importing json (api usage)", function(done) {
6+
Absurd(function(api) {
7+
api.import(__dirname + "/data/styles.json");
8+
}).compile(function(err, css) {
9+
expect(err).toBe(null);
10+
expect(css).toBeDefined();
11+
expect(css).toBe("body {\n margin: 0;\n padding: 0;\n font-size: 1em;\n}\nbody p {\n line-height: 30px;\n}\n");
12+
done();
13+
});
14+
});
15+
16+
it("importing json", function(done) {
17+
Absurd(__dirname + "/data/styles.json").compile(function(err, css) {
18+
expect(err).toBe(null);
19+
expect(css).toBeDefined();
20+
expect(css).toBe("body {\n margin: 0;\n padding: 0;\n font-size: 1em;\n}\nbody p {\n line-height: 30px;\n}\n");
21+
done();
22+
});
23+
});
24+
25+
});

0 commit comments

Comments
 (0)