Skip to content

Commit 161e9ac

Browse files
author
Vincent Peybernes
committed
Starting project
0 parents  commit 161e9ac

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.idea

LICENSE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Copyright (c) 2016 Vincent Peybernes <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
5+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
6+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions
9+
of the Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
13+
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
14+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# gulp-less-json-import
2+
3+
This is a gulp preprocessor for the less compilation. It provide a directive **@json-import** to import variables
4+
defined in a json file.
5+
6+
It inject the Less formated data in place of the directive in the file buffer without write it on disk.
7+
8+
## Exemple
9+
10+
In less
11+
```Less
12+
@json-import "../relative/path.json";
13+
@json-import "../relative/path/extOmit";
14+
```
15+
16+
In gulp
17+
```javascript
18+
var gulp = require('gulp');
19+
var less = require('gulp-less');
20+
var lessJsonImport = require('gulp-less-json-import');
21+
22+
gulp.src(['./less/**/*.less', '!./less/**/_*.less'])
23+
.pipe(lessJsonImport())
24+
.pipe(less())
25+
.pipe(gulp.dest('./css'));
26+
```

index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
var NAME = 'gulp-less-json-import';
4+
5+
var through2 = require('through2');
6+
var gutil = require('gulp-util');
7+
var StringDecoder = require('string_decoder').StringDecoder;
8+
var Buffer = require('buffer').Buffer;
9+
var path = require('path');
10+
var fs = require('fs');
11+
12+
var PluginError = gutil.PluginError;
13+
14+
var importMatcher = /^\s?@json-import "(.*?)";/;
15+
16+
module.exports = function () {
17+
return through2.obj(function(file, encoding, callback){
18+
19+
if(file.isStream()){
20+
return callback(new PluginError(NAME, 'Stream mode not supported'));
21+
}
22+
23+
var decoder = new StringDecoder(encoding);
24+
var fileDir = path.dirname(file.path);
25+
26+
var content = decoder.write(file.contents).split('\n');
27+
28+
for(var i = 0; i < content.length; i++){
29+
var match = importMatcher.exec(content[i]);
30+
if(!match) continue;
31+
32+
var jsonPath = path.resolve(path.join(fileDir, match[1]));
33+
34+
if(path.parse(jsonPath).ext != '.json'){
35+
jsonPath += '.json';
36+
}
37+
38+
var jsonContent = {};
39+
40+
try{
41+
jsonContent = require(jsonPath);
42+
} catch (e){
43+
gutil.log(NAME, 'can\'t load "'+jsonPath+'"');
44+
}
45+
46+
var lessContent = [];
47+
for(var key in jsonContent){
48+
lessContent.push('@');
49+
lessContent.push(key);
50+
lessContent.push(': ');
51+
lessContent.push(jsonContent[key]);
52+
lessContent.push(';\n');
53+
}
54+
55+
content[i] = lessContent.join('');
56+
57+
58+
}
59+
60+
file.contents = new Buffer(content.join('\n'));
61+
62+
this.push(file);
63+
64+
callback();
65+
});
66+
};

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "gulp-less-json-import",
3+
"version": "1.0.0",
4+
"description": "Import Less variables from json files",
5+
"main": "index.js",
6+
"author":{
7+
"name": "Vincent Peybernes",
8+
"email": "[email protected]"
9+
},
10+
"license": "MIT",
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/Techniv/gulp-less-json-import.git"
14+
},
15+
"dependencies": {
16+
"through2": "^2.0.0",
17+
"gulp-util": "^3.0.6"
18+
},
19+
20+
"engines": {
21+
"node": ">=0.12.0"
22+
}
23+
}

0 commit comments

Comments
 (0)