Skip to content

Commit

Permalink
feat: support less v4
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterLuffy committed May 28, 2021
1 parent ecacdf7 commit 7ce4b9b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -19,3 +19,5 @@ node_modules
.travis.yml
test
examples
.idea
yarn.lock
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -22,7 +22,7 @@
</tr>
<tr>
<td>Less Version</td>
<td>2.x | 3.7+</td>
<td>3.7+ | 4.0+</td>
</tr>
<tr>
<td>Gulp Version</td>
Expand Down
63 changes: 57 additions & 6 deletions index.js
@@ -1,12 +1,63 @@
var fs = require('fs');
var path = require('path');
var accord = require('accord');
var less = require('less');
var through2 = require('through2');
var replaceExt = require('replace-ext');
var assign = require('object-assign');
var applySourceMap = require('vinyl-sourcemaps-apply');
var PluginError = require('plugin-error');

var less = accord.load('less');
function inlineSources(map) {
if (map.sourcesContent) {
return Promise.resolve(map);
}

return Promise.all(
map.sources.map(function (source) {
return new Promise(function (resolve, reject) {
fs.readFile(source, 'utf8', function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
})
).then(
function (contents) {
map.sourcesContent = contents;
return map;
},
function () {
return map;
}
);
}

function renderLess(str, opts) {
return new Promise(function (resolve, reject) {
less.render(str, opts, function (err, res) {
if (err) {
reject(err);
} else {
var obj = {
result: res.css,
imports: res.imports,
};
if (opts.sourceMap && res.map) {
obj.sourcemap = JSON.parse(res.map);
inlineSources(obj.sourcemap).then(function (map) {
obj.sourcemap = map;
resolve(obj);
});
} else {
resolve(obj);
}
}
});
});
}

module.exports = function (options) {
// Mixes in default options.
Expand All @@ -15,7 +66,7 @@ module.exports = function (options) {
paths: []
}, options);

return through2.obj(function(file, enc, cb) {
return through2.obj(function (file, enc, cb) {
if (file.isNull()) {
return cb(null, file);
}
Expand All @@ -30,11 +81,11 @@ module.exports = function (options) {
opts.filename = file.path;

// Bootstrap source maps
if (file.sourceMap) {
opts.sourcemap = true;
if (file.sourceMap || opts.sourcemap) {
opts.sourceMap = true;
}

less.render(str, opts).then(function(res) {
renderLess(str, opts).then(function(res) {
file.contents = new Buffer(res.result);
file.path = replaceExt(file.path, '.css');
if (res.sourcemap) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -21,8 +21,7 @@
"author": "Chris Cowan",
"license": "MIT",
"dependencies": {
"accord": "^0.29.0",
"less": "2.6.x || ^3.7.1",
"less": "^3.7.1 || ^4.0.0",
"object-assign": "^4.0.1",
"plugin-error": "^0.1.2",
"replace-ext": "^1.0.0",
Expand Down

0 comments on commit 7ce4b9b

Please sign in to comment.