forked from HuasoFoundries/systemjs-less-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
less-builder.js
133 lines (111 loc) · 3.78 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
}
function rewriteURLs(loads, rootURL) {
var importRegex = /@import\s+["']([^"']+)["'];/g;
var urlRegex = /url\(["']?([^"']+)["']?\)/g;
return loads.map(function (load) {
var loadAddress = fromFileURL(load.address);
var loadDirname = path.dirname(loadAddress);
var relativeLoadDirname = loadDirname.replace(rootURL, "");
return Object.assign({}, load, {
source: load.source
.replace(importRegex, function (match, importUrl) {
return match.replace(importUrl, loadDirname + "/" + importUrl);
})
.replace(urlRegex, function (match, url) {
return match.replace(url, relativeLoadDirname + "/" + url);
})
});
});
}
function getLess(loader) {
return loader._nodeRequire(lessBundlePath.substr(isWindows ? 8 : 7)); //getLess();
}
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 lessOutput = rewriteURLs(loads, rootURL).map(function (load) {
return load.source;
}).join('');
var less = getLess(loader);
var lessOptions = Object.assign({
compress: false,
sourceMap: outputOpts.sourceMaps
}, loader.lessOptions || {});
return less.render(lessOutput, lessOptions)
.then(function (data) {
var cssOutput = data.css;
// write a separate CSS file if necessary
if (loader.separateCSS) {
if (lessOptions.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);
});
};
exports.listAssets = function (loads, compileOpts, outputOpts) {
var loader = this;
outputOpts = outputOpts || compileOpts;
var rootURL = loader.rootURL || fromFileURL(loader.baseURL),
lessOutput = rewriteURLs(loads, rootURL);
var less = getLess(loader);
var lessOptions = Object.assign({
compress: false,
sourceMap: outputOpts.sourceMaps
}, loader.lessOptions || {});
return Promise.all(lessOutput.map(function (load) {
return less.render(load.source, lessOptions)
.then(function (data) {
return {
url: load.address,
source: data.css,
sourceMap: lessOptions.sourceMaps ? data.map.toString() : null,
type: 'css'
};
}).catch(function (e) {
console.trace(e);
});
}));
};