-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgulp-plugin-add-less-import.js
29 lines (27 loc) · 1.09 KB
/
gulp-plugin-add-less-import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const path = require('path');
const through = require('through-gulp');
// exporting the plugin
module.exports = addLessImport;
function addLessImport({ themePath, commomPath }) {
// creating a stream through which each file will pass
return through(function(file, encoding, callback) {
let html = file.contents.toString('utf-8');
const filepath = file.history && file.history[0];
const fileDir = path.dirname(filepath);
const themeRelativePath = path.relative(fileDir, themePath);
const commonRelativePath = path.relative(fileDir, commomPath);
const fileDirList = fileDir.split(/\//);
const isComponents = fileDirList[fileDirList.length - 2] === 'components';
if (isComponents) {
html = `${`@import '${themeRelativePath}';` + '\n' + `@import '${commonRelativePath}';` + '\n'}${html}`;
} else {
html = `${`@import '${themeRelativePath}';` + '\n'}${html}`;
}
file.contents = new Buffer(html);
this.push(file);
callback();
}, (callback) => {
// just pipe data next, just callback to indicate that the stream's over
callback();
}, 16);
}