buddy-plugin-less
Version:
Buddy plugin for less source transforms (via Less)
89 lines (78 loc) • 2.05 kB
JavaScript
;
const less = require('less');
const FILE_EXTENSIONS = ['less'];
const WORKFLOW_WRITEABLE = [
'inline',
'compile',
'transpile'
];
module.exports = {
name: 'less',
type: 'css',
/**
* Register plugin
* @param {Config} config
*/
register (config) {
config.registerFileDefinitionAndExtensionsForType(define, FILE_EXTENSIONS, this.type);
}
};
/**
* Extend 'File' with new behaviour
* @param {Class} File
* @param {Object} utils
* @returns {Class}
*/
function define (File, utils) {
const { debug, error, strong } = utils.cnsl;
return class LESSFile extends File {
/**
* Constructor
* @param {String} id
* @param {String} filepath
* @param {Object} options
* - {Object} caches
* - {Object} fileExtensions
* - {Function} fileFactory
* - {Object} globalAliases
* - {Array} npmModulepaths
* - {Object} pluginOptions
* - {Object} runtimeOptions
*/
constructor (id, filepath, options) {
super(id, filepath, options);
this.workflows.writeable = WORKFLOW_WRITEABLE;
}
/**
* Compile file contents
* @param {Object} buildOptions
* - {Boolean} batch
* - {Boolean} bootstrap
* - {Boolean} boilerplate
* - {Boolean} browser
* - {Boolean} bundle
* - {Boolean} compress
* - {Array} ignoredFiles
* - {Boolean} helpers
* - {Boolean} watchOnly
* @param {Function} fn(err)
*/
compile (buildOptions, fn) {
const options = Object.assign(
this.hasMaps ? { sourceMap: {} } : {},
this.options.pluginOptions.less || {}
);
less.render(this.content, options, (err, result) => {
if (err) {
if (!this.options.runtimeOptions.watch) return fn(err);
error(err, 4, false);
}
const { css: content, map } = result;
this.setContent(content);
if (map) this.setMap(map);
debug(`compile: ${strong(this.relpath)}`, 4);
fn();
});
}
};
}