metalsmith-less
Advanced tools
Comparing version 1.0.3 to 2.0.0
120
lib/index.js
var _ = require('lodash') | ||
var async = require('async') | ||
var BPromise = require('bluebird') | ||
var Joi = require('joi') | ||
var less = require('less') | ||
var multimatch = require('multimatch') | ||
var Parser = require('less').Parser | ||
var path = require('path') | ||
var DEFAULT_PATTERN = '**/*.less' | ||
var MAP_INPUT_KEY = 'input' | ||
var MAP_RE = /less/g | ||
var INPUT_SOURCE = 'input' | ||
var OPTIONS_SCHEMA = Joi.object().keys({ | ||
pattern: Joi.any([ | ||
Joi.string(), | ||
Joi.array().items(Joi.string()), | ||
]).default('**/*.less'), | ||
render: Joi.object(), | ||
useDynamicSourceMap: Joi.boolean().default(false), | ||
}) | ||
@@ -14,66 +22,46 @@ module.exports = plugin | ||
function plugin(options) { | ||
options = options || {} | ||
var pattern = options.pattern || DEFAULT_PATTERN | ||
var useDefaultSourceMap = options.useDefaultSourceMap || false | ||
var parseOptions = options.parse || {} | ||
var renderOptions = options.render || {} | ||
var parser = new Parser(parseOptions) | ||
var validation = Joi.validate((options || {}), OPTIONS_SCHEMA) | ||
if (validation.error) throw validation.error | ||
var useDynamicSourceMap = validation.value.useDynamicSourceMap | ||
var renderOptions = validation.value.render | ||
return function (files, metalsmith, done) { | ||
var paths = Object.keys(files).filter(function (path) { | ||
return multimatch(path, pattern).length > 0 | ||
}) | ||
var iterator = convert.bind(null, { | ||
files: files, | ||
parser: parser, | ||
renderOptions: renderOptions, | ||
useDefaultSourceMap: useDefaultSourceMap | ||
}) | ||
async.each(paths, iterator, done) | ||
return BPromise | ||
.filter(Object.keys(files), function (_path) { | ||
return multimatch(_path, validation.value.pattern).length > 0 | ||
}) | ||
.map(function (_path) { | ||
var destination = _path.replace(MAP_RE, 'css') | ||
var mapDestination | ||
var map | ||
if (useDynamicSourceMap) { | ||
mapDestination = destination + '.map' | ||
renderOptions = _.chain(renderOptions) | ||
.cloneDeep() | ||
.omit('useDynamicSourceMap') | ||
.extend({ | ||
sourceMap: { | ||
outputSourceFiles: true, | ||
sourceMapURL: path.basename(mapDestination), | ||
}, | ||
}) | ||
.value() | ||
} | ||
return less.render(files[_path].contents.toString(), renderOptions) | ||
.then(function (output) { | ||
var mapInputIndex | ||
files[destination] = { | ||
contents: new Buffer(output.css), | ||
} | ||
if (useDynamicSourceMap) { | ||
map = JSON.parse(output.map) | ||
mapInputIndex = map.sources.indexOf(MAP_INPUT_KEY) | ||
if (mapInputIndex) map.sources[mapInputIndex] = path.join(metalsmith._source, _path) | ||
files[mapDestination] = { | ||
contents: new Buffer(JSON.stringify(map)), | ||
} | ||
} | ||
}) | ||
}) | ||
.nodeify(done) | ||
} | ||
} | ||
function convert(options, filePath, done) { | ||
var files = options.files | ||
var parser = options.parser | ||
var renderOptions = options.renderOptions | ||
var useDefaultSourceMap = options.useDefaultSourceMap | ||
var data = files[filePath] | ||
parser.parse(data.contents.toString(), function (err, tree) { | ||
if (err) return done(err) | ||
var destination = filePath.replace(MAP_RE, 'css') | ||
var sourceMapDestination | ||
var sourceMap | ||
var contents | ||
if (useDefaultSourceMap) { | ||
sourceMapDestination = destination + '.map' | ||
renderOptions = _.chain(renderOptions) | ||
.clone() | ||
.extend({ | ||
outputSourceFiles: true, | ||
sourceMap: true, | ||
sourceMapBasepath: undefined, | ||
sourceMapFilename: undefined, | ||
sourceMapOutputFilename: undefined, | ||
sourceMapURL: path.basename(sourceMapDestination), | ||
writeSourceMap: function (res) { | ||
// This method is invoked synchronously during `tree.toCSS()`. | ||
var sm = JSON.parse(res) | ||
var inputIndex = sm.sources.indexOf(INPUT_SOURCE) | ||
if (inputIndex) sm.sources[inputIndex] = 'src/' + filePath | ||
sourceMap = JSON.stringify(sm) | ||
} | ||
}) | ||
.value() | ||
} | ||
contents = tree.toCSS(renderOptions) | ||
if (useDefaultSourceMap) { | ||
files[sourceMapDestination] = { | ||
contents: new Buffer(sourceMap) | ||
} | ||
} | ||
files[destination] = { | ||
contents: new Buffer(contents) | ||
} | ||
return done(null) | ||
}) | ||
} |
{ | ||
"name": "metalsmith-less", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "A LESS plugin for Metalsmith", | ||
@@ -22,13 +22,14 @@ "main": "./lib/", | ||
"dependencies": { | ||
"async": "^0.9.0", | ||
"less": "^1.7.5", | ||
"lodash": "^2.4.1", | ||
"multimatch": "^1.0.0" | ||
"bluebird": "^2.9.14", | ||
"joi": "^6.0.8", | ||
"less": "^2.4.0", | ||
"lodash": "^3.5.0", | ||
"multimatch": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"assert-dir-equal": "^1.0.1", | ||
"crispy": "^2.0.0", | ||
"metalsmith": "^0.11.0", | ||
"mocha": "^1.21.4" | ||
"crispy": "^3.1.2", | ||
"metalsmith": "^1.3.0", | ||
"mocha": "^2.2.1" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # metalsmith-less | ||
A [LESS][less] plugin for [Metalsmith][metalsmith]. | ||
A [LESS](http://lesscss.org/) plugin for [Metalsmith](http://www.metalsmith.io/). | ||
@@ -19,25 +19,21 @@ ## Installation | ||
Metalsmith(__dirname) | ||
.use(less(options)) | ||
.build() | ||
new Metalsmith(__dirname) | ||
.use(less(options)) | ||
.build() | ||
``` | ||
### Options | ||
### **`options`** `Object` | ||
- **`pattern`** `String pattern|Array<String> pattern` | ||
- **`pattern`** `String|Array<String>` | ||
A [pattern][multimatch] to filter source files. Default `**/*.less`. | ||
The [pattern](https://github.com/sindresorhus/multimatch) to filter source files. Default `**/*.less`. | ||
- **`parse`** `Object parseOptions` | ||
- **`render`** `Object` | ||
An object that gets passed along to [`new less.Parser(parseOptions)`][less config]. Default `null`. | ||
The options passed to [`less.render(String[, Object])`](http://lesscss.org/usage/#programmatic-usage). Unfortunately, this method is *undocumented*. See https://github.com/less/less-docs/issues/212 for more information. Default `undefined`. | ||
- **`render`** `Object renderOptions` | ||
- **`useDynamicSourceMap`** `Boolean` | ||
An object that gets passed along to [`tree.toCSS(renderOptions)`][less config]. Default `null`. | ||
Overrides the supplied source map configuration with a dynamic file-level configuration. This is the easiest way to enable source maps in your Metalsmith build. Default `false`. | ||
- **`useDefaultSourceMap`** `Boolean useDefaultSourceMap` | ||
A switch to enable the default source map configuration. LESS's source map API doesn't play nice with Metalsmith, so this is a heavy-handed approach to sensible source maps. Default `false`. | ||
## Tests | ||
@@ -52,6 +48,1 @@ | ||
MIT License, see [LICENSE](https://github.com/christophercliff/metalsmith-less/blob/master/LICENSE.md) for details. | ||
[less]: http://lesscss.org/ | ||
[less config]: http://lesscss.org/#using-less-configuration | ||
[metalsmith]: http://www.metalsmith.io/ | ||
[multimatch]: https://github.com/sindresorhus/multimatch |
@@ -5,3 +5,3 @@ var assertDir = require('assert-dir-equal') | ||
describe('metalsmith-less', function () { | ||
describe('the plugin', function () { | ||
@@ -22,5 +22,7 @@ it('should convert less to css', function (done) { | ||
pattern: 'less/index.less', | ||
parse: { | ||
paths: ['test/fixtures/import/src/less'] | ||
} | ||
render: { | ||
paths: [ | ||
'test/fixtures/import/src/less', | ||
], | ||
}, | ||
})) | ||
@@ -37,7 +39,12 @@ .build(function (err) { | ||
.use(less({ | ||
pattern: 'less/index.less', | ||
useDefaultSourceMap: true, | ||
parse: { | ||
paths: ['test/fixtures/source-map/src/less'] | ||
} | ||
pattern: [ | ||
'less/entry-1.less', | ||
'less/entry-2.less', | ||
], | ||
render: { | ||
paths: [ | ||
'test/fixtures/source-map/src/less', | ||
], | ||
}, | ||
useDynamicSourceMap: true, | ||
})) | ||
@@ -44,0 +51,0 @@ .build(function (err) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9516
34
143
5
46
+ Addedbluebird@^2.9.14
+ Addedjoi@^6.0.8
+ Addedajv@4.11.8(transitive)
+ Addedarrify@1.0.1(transitive)
+ Addedasap@2.0.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@0.2.01.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.6.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedbluebird@2.11.0(transitive)
+ Addedboom@2.10.1(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedco@4.6.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addedcryptiles@2.0.5(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addederrno@0.1.8(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.1.4(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhar-schema@1.0.5(transitive)
+ Addedhar-validator@4.2.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhawk@3.1.3(transitive)
+ Addedhoek@2.16.3(transitive)
+ Addedhttp-signature@1.1.1(transitive)
+ Addedimage-size@0.5.5(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedisemail@1.2.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjoi@6.10.1(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-stable-stringify@1.1.1(transitive)
+ Addedjsonify@0.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedless@2.7.3(transitive)
+ Addedlodash@3.10.1(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedmultimatch@2.1.0(transitive)
+ Addedoauth-sign@0.8.2(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedperformance-now@0.2.0(transitive)
+ Addedpromise@7.3.1(transitive)
+ Addedprr@1.0.1(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedqs@6.4.1(transitive)
+ Addedrequest@2.81.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsntp@1.0.9(transitive)
+ Addedsource-map@0.5.7(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtopo@1.1.0(transitive)
+ Addedtough-cookie@2.3.4(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)
- Removedasync@^0.9.0
- Removedamdefine@1.0.1(transitive)
- Removedasn1@0.1.11(transitive)
- Removedassert-plus@0.1.5(transitive)
- Removedasync@0.9.2(transitive)
- Removedaws-sign2@0.5.0(transitive)
- Removedboom@0.4.2(transitive)
- Removedclean-css@2.2.23(transitive)
- Removedcombined-stream@0.0.7(transitive)
- Removedcommander@2.2.0(transitive)
- Removedcryptiles@0.2.2(transitive)
- Removedctype@0.5.3(transitive)
- Removeddelayed-stream@0.0.5(transitive)
- Removedforever-agent@0.5.2(transitive)
- Removedform-data@0.1.4(transitive)
- Removedgraceful-fs@3.0.12(transitive)
- Removedhawk@1.1.1(transitive)
- Removedhoek@0.9.1(transitive)
- Removedhttp-signature@0.10.1(transitive)
- Removedless@1.7.5(transitive)
- Removedlodash@2.4.2(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedmime@1.2.11(transitive)
- Removedmime-types@1.0.2(transitive)
- Removedminimatch@1.0.0(transitive)
- Removedmultimatch@1.0.1(transitive)
- Removednatives@1.1.6(transitive)
- Removednode-uuid@1.4.8(transitive)
- Removedoauth-sign@0.3.0(transitive)
- Removedqs@1.0.2(transitive)
- Removedrequest@2.40.0(transitive)
- Removedsigmund@1.0.1(transitive)
- Removedsntp@0.2.4(transitive)
- Removedsource-map@0.1.43(transitive)
- Removedtldts@6.1.49(transitive)
- Removedtldts-core@6.1.49(transitive)
- Removedtough-cookie@5.0.0(transitive)
- Removedtunnel-agent@0.4.3(transitive)
Updatedless@^2.4.0
Updatedlodash@^3.5.0
Updatedmultimatch@^2.0.0