forked from HuasoFoundries/systemjs-less-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
less-builder.js
107 lines (87 loc) · 3.2 KB
/
less-builder.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
var isWindows = typeof process !== 'undefined' && process.platform && process.platform.match(/^win/),
fs = System._nodeRequire('fs'),
path = System._nodeRequire('path'),
lessBundlePath = System.normalizeSync('./lessjs/less.node.js', module.id);
function escape(source) {
return source
.replace(/(["\\])/g, '\\$1')
.replace(/[\f]/g, "\\f")
.replace(/[\b]/g, "\\b")
.replace(/[\n]/g, "\\n")
.replace(/[\t]/g, "\\t")
.replace(/[\r]/g, "\\r")
.replace(/[\u2028]/g, "\\u2028")
.replace(/[\u2029]/g, "\\u2029");
}
var isWin = process.platform.match(/^win/);
function fromFileURL(address) {
address = address.replace(/^file:(\/+)?/i, '');
if (!isWin) {
address = '/' + address;
} else {
address = address.replace(/\//g, '\\');
}
return address;
}
var cssInject = [
"(function(c){var d=document,a='appendChild',i='styleSheet',s=d.createElement('style')",
"s.type='text/css'",
"d.getElementsByTagName('head')[0][a](s)",
"s[i]?s[i].cssText=c:s[a](d.createTextNode(c))",
"})"
].join(';');
exports.bundle = function (loads, compileOpts, outputOpts) {
var loader = this;
var writeStubs = typeof outputOpts === 'undefined';
outputOpts = outputOpts || compileOpts;
var stubDefines = writeStubs ? loads.map(function (load) {
return (compileOpts.systemGlobal || 'System') + ".register('" + load.name + "', [], false, function() {});";
}).join('\n') : [];
var rootURL = loader.rootURL || fromFileURL(loader.baseURL);
var outFile = loader.separateCSS ? outputOpts.outFile.replace(/\.js$/, '-from-less.css') : rootURL;
var importRegex = /@import\s+["']([^"']+)["'];/g;
var urlRegex = /url\(["']?(.+?)["']?\)/g;
var lessOutput = loads.map(function (load) {
var loadAddress = fromFileURL(load.address);
var loadDirname = path.dirname(loadAddress);
var relativeLoadDirname = loadDirname.replace(rootURL, "").replace(/\\/g, '/');
return load.source
.replace(importRegex, function (match, importUrl) {
return match.replace(importUrl, loadDirname + "/" + importUrl);
})
.replace(urlRegex, function (match, url) {
return match.replace(url, relativeLoadDirname + "/" + url);
});
})
.reduce(function (sourceA, sourceB) {
return sourceA + sourceB;
}, '');
var less = loader._nodeRequire(lessBundlePath.substr(isWindows ? 8 : 7)); //getLess();
return less.render(lessOutput, {
compress: false,
sourceMap: outputOpts.sourceMaps,
relativeUrls: true,
rootpath: loader.rootpath || ""
})
.then(function (data) {
var cssOutput = data.css;
var urlRegex = /url\(["']?(.+?)["']?\)/g;
var slashRegex = /\\/g;
var loadDir = process.cwd().replace(slashRegex, '/') + "/";
cssOutput = cssOutput.replace(urlRegex, function (match, url) {
return match.replace(loadDir, "").replace(slashRegex, '/');
});
// write a separate CSS file if necessary
if (loader.separateCSS) {
if (outputOpts.sourceMaps) {
fs.writeFileSync(outFile + '.map', data.map.toString());
cssOutput += '/*# sourceMappingURL=' + outFile.split(/[\\/]/).pop() + '.map*/';
}
fs.writeFileSync(outFile, cssOutput);
return stubDefines;
}
return [stubDefines, cssInject, '("' + escape(cssOutput) + '");'].join('\n');
}).catch(function (e) {
console.trace(e);
});
};