node-lessify
Advanced tools
Comparing version 0.1.1 to 0.1.2
139
index.js
@@ -8,37 +8,48 @@ // v0.0.9a | ||
var textMode = false, | ||
func_start = "(function() { var head = document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css';", | ||
func_end = "if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } head.appendChild(style);}())"; | ||
var func_start = "(function() { var head = document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css';", | ||
func_end = "if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } head.appendChild(style);}())"; | ||
var defaultOptions = { | ||
compileOptions: { | ||
compress: true | ||
} | ||
}; | ||
/* | ||
you can pass options to the transform from your package.json file like so: | ||
"browserify": { | ||
"transform-options": { | ||
"node-lessify": "textMode" | ||
} | ||
} | ||
NOTE: This is deprecated since it is now possible to do this like so: | ||
you can pass options to the transform from your package.json file like so: | ||
"browserify": { | ||
"transform": [ | ||
[ "node-lessify", { "textMode": true } ] | ||
] | ||
} | ||
*/ | ||
"browserify": { | ||
"transform-options": { | ||
"node-lessify": "textMode" | ||
} | ||
} | ||
NOTE: This is deprecated since it is now possible to do this like so: | ||
"browserify": { | ||
"transform": [ | ||
[ "node-lessify", { "textMode": true } ] | ||
] | ||
} | ||
*/ | ||
var currentWorkingDir = process.cwd(); | ||
var packageConfig; | ||
try { | ||
var options = require(process.cwd() + "/package.json"); | ||
packageConfig = require(currentWorkingDir + "/package.json"); | ||
} catch (e) { | ||
var options = {}; | ||
}; | ||
packageConfig = undefined; | ||
} | ||
/* | ||
textMode simply compiles the LESS into a single string of CSS and passes it back without adding the code that automatically appends that CSS to the page | ||
*/ | ||
textMode simply compiles the LESS into a single string of CSS and passes it back without adding the code that automatically appends that CSS to the page | ||
*/ | ||
if (options.browserify && options.browserify["transform-options"] && options.browserify["transform-options"]["node-lessify"] == "textMode") { | ||
textMode = true; | ||
} | ||
var packagePluginOptions = packageConfig && | ||
packageConfig.browserify && | ||
packageConfig.browserify["transform-packageConfig"] && | ||
packageConfig.browserify["transform-packageConfig"]["node-lessify"]; | ||
module.exports = function(file, package_options) { | ||
module.exports = function (file, transformOptions) { | ||
if (!/\.css$|\.less$/.test(file)) { | ||
@@ -48,54 +59,62 @@ return through(); | ||
// supplement options with those from package | ||
for (var key in package_options) { | ||
if (package_options.hasOwnProperty(key) && key !== "_flags") { | ||
options[key] = package_options[key]; | ||
} | ||
} | ||
// set the curTransformOptions using the given plugin options | ||
var curTransformOptions = assign({}, defaultOptions, packagePluginOptions || {}, transformOptions || {}); | ||
curTransformOptions._flags = undefined; // clear out the _flag property | ||
var buffer = "", mydirName = path.dirname(file); | ||
var buffer = "", | ||
myDirName = path.dirname(file); | ||
var compileOptions = assign({}, curTransformOptions.compileOptions || {}, { | ||
paths: [".", myDirName] // override the "paths" property | ||
}); | ||
return through(write, end); | ||
function write(chunk, enc, next) { | ||
buffer += chunk.toString(); | ||
next(); | ||
} | ||
function write(chunk, enc, next) { | ||
buffer += chunk.toString(); | ||
next(); | ||
} | ||
function end(done) { | ||
var self = this; | ||
var self = this; | ||
// CSS is LESS so no need to check extension | ||
less.render(buffer, assign({ | ||
paths: [".", mydirName], | ||
compress: true | ||
}, { | ||
plugins: package_options.plugins ? package_options.plugins : undefined | ||
}), function(e, output) { | ||
if (e) { | ||
var msg = e.message; | ||
if (e.line) { | ||
msg += ", line " + e.line; | ||
//DEBUG | ||
//console.log('compileOptions = ', compileOptions); | ||
// CSS is LESS so no need to check extension | ||
less.render(buffer, compileOptions, function (err, output) { | ||
if (err) { | ||
var msg = err.message; | ||
if (err.line) { | ||
msg += ", line " + err.line; | ||
} | ||
if (e.column) { | ||
msg += ", column " + e.column; | ||
if (err.column) { | ||
msg += ", column " + err.column; | ||
} | ||
if (e.extract) { | ||
msg += ": \"" + e.extract + "\""; | ||
if (err.extract) { | ||
msg += ": \"" + err.extract + "\""; | ||
} | ||
return done(new Error(msg, file, e.line)); | ||
return done(new Error(msg, file, err.line)); | ||
} | ||
compiled = output.css; | ||
if (textMode || package_options.textMode) { | ||
compiled = "module.exports = " + JSON.stringify(compiled) + ";"; | ||
// small hack to output the file path of the LESS source file | ||
// so that we can differentiate | ||
var compiled = JSON.stringify( | ||
output.css + | ||
(curTransformOptions.appendLessSourceUrl ? | ||
'/*# sourceURL=' + path.relative(currentWorkingDir, file).replace(/\\/g, '/') + ' */' : '') | ||
); | ||
if (curTransformOptions.textMode) { | ||
compiled = "module.exports = " + compiled + ";"; | ||
} else { | ||
compiled = func_start + "var css = " + JSON.stringify(compiled) + ";" + func_end; | ||
compiled = func_start + "var css = " + compiled + ";" + func_end; | ||
} | ||
self.push(compiled); | ||
self.push(null); | ||
self.push(null); | ||
output.imports.forEach(function(f) { | ||
output.imports.forEach(function (f) { | ||
self.emit('file', f); | ||
@@ -105,4 +124,4 @@ }); | ||
done(); | ||
}); | ||
}); | ||
} | ||
}; |
{ | ||
"name": "node-lessify", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "LESS precompiler and CSS plugin for Browserify", | ||
@@ -27,9 +27,9 @@ "main": "index.js", | ||
"dependencies": { | ||
"less": "^2.5.3", | ||
"less": "^2.6.1", | ||
"object-assign": "^4.0.1", | ||
"through2": "^2.0.0" | ||
"through2": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^10.0.0", | ||
"less-plugin-autoprefix": "^1.5.1" | ||
"browserify": "^13.0.0", | ||
"less-plugin-autoprefix": "*" | ||
}, | ||
@@ -36,0 +36,0 @@ "bugs": { |
node-lessify | ||
============ | ||
Version 0.1.1 | ||
Version 0.1.2 | ||
[![Build Status](https://travis-ci.org/wilson428/node-lessify.png)](https://travis-ci.org/wilson428/node-lessify) | ||
[![Dependency Status](https://david-dm.org/TimeMagazine/time-interactive.svg)](https://david-dm.org/TimeMagazine/time-interactive) | ||
@@ -55,2 +56,7 @@ LESS 2.0 precompiler and CSS plugin for Browserify. Inspired by [node-underscorify](https://github.com/maxparm/node-underscorify). | ||
### Append Less file source URL | ||
As a workaround to LESS source map issues (e.g. css style lines not referring to the correct LESS file), we can output | ||
only the source LESS file name for each require() call of a LESS file. This will at least allow us to distinguish | ||
STYLE elements. | ||
### Plugins | ||
@@ -65,3 +71,7 @@ You can pass a `plugins` argument to get less plugins like [autoprefix](https://www.npmjs.com/package/less-plugin-autoprefix): | ||
var b = browserify(sampleLESS); | ||
b.transform(lessify, {plugins: [autoprefix] }); | ||
b.transform(lessify, { | ||
compileOptions: { | ||
plugins: [autoprefix] | ||
} | ||
}); | ||
@@ -71,2 +81,4 @@ Note: This does not currently work via `package.json` arguments, since the plugins need to be required separately, but we're working on it. | ||
## Changes | ||
**v0.1.2**: Updated dependencies | ||
**v0.1.1**: Updated dependencies | ||
@@ -73,0 +85,0 @@ |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
12030
144
96
Updatedless@^2.6.1
Updatedthrough2@^2.0.1