-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
127 lines (100 loc) · 3.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// v0.0.9a
var path = require("path");
var through = require('through2');
var less = require('less');
var assign = require('object-assign');
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:
"browserify": {
"transform": [
[ "node-lessify", { "textMode": true } ]
]
}
*/
var currentWorkingDir = process.cwd();
var packageConfig;
try {
packageConfig = require(currentWorkingDir + "/package.json");
} catch (e) {
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
*/
var packagePluginOptions = packageConfig &&
packageConfig.browserify &&
packageConfig.browserify["transform-packageConfig"] &&
packageConfig.browserify["transform-packageConfig"]["node-lessify"];
module.exports = function (file, transformOptions) {
if (!/\.css$|\.less$/.test(file)) {
return through();
}
// 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 compileOptions = assign({}, curTransformOptions.compileOptions || {});
compileOptions.paths = [].concat(compileOptions.paths || [], [".", myDirName]);
return through(write, end);
function write(chunk, enc, next) {
buffer += chunk.toString();
next();
}
function end(done) {
var self = this;
// CSS is LESS so no need to check extension
less.render(buffer, compileOptions).then(
function(output) {
// 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 = " + compiled + ";" + func_end;
}
self.push(compiled);
self.push(null);
// emit change event for watchers
output.imports.forEach(function (f) {
self.emit('file', f);
});
done();
},
function (err, output) {
if (err) {
var msg = err.message;
if (err.line) {
msg += " on " + err.line;
}
if (err.column) {
msg += ":" + err.column;
}
if (err.extract) {
msg += "\n" + err.extract + "\n";
}
done(new Error(msg, file, err.line));
self.emit('file', err.filename);
}
});
}
};