Skip to content

Instantly share code, notes, and snippets.

@Craga89
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save Craga89/96f7fcad1f35b08bf88d to your computer and use it in GitHub Desktop.

Select an option

Save Craga89/96f7fcad1f35b08bf88d to your computer and use it in GitHub Desktop.
LESS - Generate a deep list of all a file's @imports
var gulp = require('gulp'),
watch = require('gulp-watch'),
getImports = require('./lessImportList');
gulp.task('less', function(done) {
getImports(['less/themes/default/app.less'], function(imports) {
watch(imports)
.pipe(/* Task logic here */)
});
});
var through = require('through2'),
fs = require('fs'),
gulp = require('gulp'),
gutil = require('gulp-util'),
watch = require('gulp-watch'),
gulpLess = require('gulp-less'),
less = require('less'),
PluginError = gutil.PluginError;
const PLUGIN_NAME = 'gulp-watch-less';
function getImportsList(file, options, cb) {
// Parse options
if (!options) options = {};
options.filename = file.path;
// Create new parser instance
var parser = new less.Parser(options);
// Parse the source into AST tree via LESS
parser.parse(file.contents.toString('utf8'), function (err, tree) {
if (err) { throw new PluginError(PLUGIN_NAME, err); }
// Return array of all `@import`ed files via the parsers `imports` object!
cb(Object.keys(parser.imports.files));
});
}
function gulpWatchLess(glob, options, cb) {
var watchFiles = [],
watchStream;
// Handles (glob, cb) signature
if (typeof options === 'function') {
cb = options;
options = {};
}
if (!options) options = {};
// Setup watch steam on the provided globs
watchStream = watch(glob, options, cb);
// Grab reference to all files in the provided glob via `gulp-src`
gulp.src(glob).pipe(
// Create a pass-through stream
through.obj(function(file, enc, cb) {
// Null files passthrough
if(file.isNull()) { return cb(); }
// Don't support streams
else if(file.isStream()) {
throw new PluginError(PLUGIN_NAME, 'Streams not supported!');
}
// Grab the list of imports, passing throuh less options if defined
getImportsList(file, options.less, function(imports) {
// Push returned imports list onto the watched files array
watchFiles = watchFiles.concat(imports);
cb();
});
// Passthrough file
this.push(file);
},
// On stream end, watch the files and pipe to output stream
function(cb) { watch(watchFiles).pipe(watchStream); })
);
return watchStream;
}
module.exports = gulpWatchLess;
var fs = require('fs'),
less = require('less');
module.exports = function(path, opts, cb) {
// Support (path, cb) signature
if(typeof opts === 'function') {
cb = opts; opts = null;
}
// Read the file (async)
fs.readFile(path, function(err, source) {
if(err) { throw new Error(err); }
// Setup options
opts || (opts = {});
opts.filename = path;
// Create new parser instance
parser = new less.Parser(opts);
// Parse the source into AST tree via LESS
parser.parse(source.toString('utf8'), function (err, tree) {
if (err) { throw new Error(err); }
// Return array of all `@import`ed files via the parsers `imports` object!
cb(Object.keys(parser.imports.files));
});
});
};
var getImports = require('./lessImportList');
// Sample usage - log all @import paths to the console :)
getImports('less/themes/default/app.less', function(imports) {
console.log(imports); // Log to the console
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment