-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathless.js
290 lines (239 loc) · 7.32 KB
/
less.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
var css = require("$css");
var loader = require("@loader");
var lessEngine = require("./less-engine");
exports.instantiate = css.instantiate;
var options = loader.lessOptions || {};
if(lessEngine.options) {
lessEngine.options.async = true;
}
// default optimization value.
options.optimization |= lessEngine.optimization;
// We store sources so files are only fetched once and shared between
// Steal and the Less File Manager
exports.fetch = function(load, fetch){
var p = Promise.resolve(false);
if(this.liveReloadInstalled) {
var loader = this, args = arguments;
p = loader.import("live-reload", { name: module.id })
.then(function(liveReload){
return liveReload.isReloading();
});
}
var loader = this, args = arguments;
return p.then(function(isReloading){
if(isReloading) {
removeSource(load.address);
return fetch.apply(loader, args);
}
var p = getSource(load.address);
if(p) {
return p;
}
p = fetch.call(loader, load);
addSource(load.address, p);
return p;
});
};
exports.translate = function(load) {
var loader = this;
var address = load.address.replace(/^file\:/,"");
var useFileCache = true;
var pathParts = (address+'').split('/');
pathParts[pathParts.length - 1] = ''; // Remove filename
if (typeof window !== 'undefined') {
pathParts = (load.address+'').split('/');
pathParts[pathParts.length - 1] = ''; // Remove filename
}
function renderLess() {
var filename = address;
if(loader._nodeRequire) {
filename = loader._nodeRequire("path").resolve(address);
}
var renderOptions = {
filename: filename,
useFileCache: useFileCache
};
var pluginPromises = [];
for (var prop in options){
if( prop !== 'plugins') {
renderOptions[prop] = options[prop];
}
}
renderOptions.paths = (options.paths || []).concat(pathParts.join('/'));
renderOptions.plugins = [];
if(options.plugins) {
options.plugins.forEach(function(plugin) {
if(typeof plugin === 'string') {
pluginPromises.push(loader.import(plugin).then(function(resolvedPlugin){
renderOptions.plugins.push(resolvedPlugin);
}));
}
});
}
if (stealLessPlugin !== undefined) {
renderOptions.plugins.push(stealLessPlugin);
}
renderOptions.relativeUrls = options.relativeUrls === undefined ? true : options.relativeUrls;
var done = function(output) {
// Put the source map on metadata if one was created.
load.metadata.map = output.map;
load.metadata.includedDeps = output.imports || [];
return output.css;
};
return Promise.all(pluginPromises).then(function(){
var p = Promise.resolve(
lessEngine.render(load.source, renderOptions)
);
return p.then(done).then(null, function(err){
// 404.
//!steal-remove-start
if(err.type === "File" && /404/.test(err.message)) {
if(loader._addSourceInfoToError) {
var fn = err.filename.split("/").pop();
var msg = "The stylesheet [" + fn + "] wasn't able to fetch a dependency.\n" +
"This could be because:\n\n" +
"\t - The dependency hasn't been saved yet.\n" +
"\t - The path is incorrect.\n\n" +
"The below snippet shows the file we were unable to fetch.\n" +
"See https://stealjs.com/docs/StealJS.error-messages.html#404-not-found for more information.";
var newError = new Error(msg);
var pos = { line: err.line, column: err.column };
return loader._addSourceInfoToError(newError, pos, load, "@import");
}
}
//!steal-remove-end
return Promise.reject(err);
});
});
}
if(loader.liveReloadInstalled) {
return loader["import"]("live-reload", { name: module.id })
.then(function(reload){
if(reload.isReloading()) {
useFileCache = false;
}
})
.then(renderLess, renderLess)
.catch(function(err) {
console.error(err.message + " in " + err.filename + ":" + err.line);
console.dir(err);
});
}
return renderLess();
};
exports.locateScheme = true;
exports.buildType = "css";
// plugin to rewrite locate:// paths in imports
var stealLessPlugin = undefined;
if (lessEngine.FileManager) {
var FileManager = lessEngine.FileManager;
function StealLessManager() {
this.PATTERN = /locate:\/\/([a-z0-9/._@-]*)/ig;
}
StealLessManager.prototype = new FileManager();
StealLessManager.prototype.supports = function(filename) {
return true;
};
StealLessManager.prototype.locate = function(filename, currentDirectory) {
return Promise.resolve(loader.normalize(filename, currentDirectory))
.then(function(name){
return loader.locate({name: name, metadata: {}});
});
};
StealLessManager.prototype.parseFile = function(file) {
var self = this;
var promises = [];
// collect locate promises
file.contents.replace(self.PATTERN, function (whole, path, index) {
promises.push(self.locate(path, file.filename.replace(loader.baseURL, '')).then(function(filename) {
filename = filename.replace(/^file\:/,"");
return {
str: relative(file._directory, filename),
loc: index,
del: whole.length
}
}));
});
return Promise.all(promises).then(function(spliceDefs) {
for(var i = spliceDefs.length; i--;) {
var def = spliceDefs[i];
file.contents = file.contents.slice(0, def.loc) + def.str + file.contents.slice(def.loc + def.del);
}
return file;
});
};
StealLessManager.prototype.loadFile = function(filename, currentDirectory, options, environment, callback) {
var self = this,
_callback = callback,
path = (currentDirectory + filename),
directory = normalizePath(path.substring(0, path.lastIndexOf('/')+1)),
promise;
callback = function(err, file) {
if (err) {
return _callback.call(self, err);
}
file._directory = directory;
self.parseFile(file).then(function(file) {
_callback.call(self, null, file);
});
};
promise = FileManager.prototype.loadFile.call(this, filename, currentDirectory, options, environment, callback);
// when promise is returned we must wrap promise, when one is not, the wrapped callback is used
if (promise && typeof promise.then == 'function') {
return promise.then(function(file) {
file._directory = directory;
return self.parseFile(file);
});
}
};
stealLessPlugin = {
install: function(less, pluginManager) {
pluginManager.addFileManager(new StealLessManager());
}
};
exports.StealLessManager = StealLessManager;
}
var getSource = function(url){
return loader._lessSources && loader._lessSources[url];
}
var addSource = function(url, p){
if(!loader._lessSources) {
loader._lessSources = {};
}
if(!loader._lessSources[url]) {
loader._lessSources[url] = Promise.resolve(p);
}
};
var removeSource = function(url){
if(loader._lessSources) {
delete loader._lessSources[url];
}
};
var normalizePath = function(path) {
var parts = path.split('/'),
normalized = [];
for (var i = 0 ; i< parts.length; i++) {
var part = parts[i];
if (part != '.') { // ignore './'
if (part == '..') { // remove part preceding '../'
normalized.pop();
} else {
normalized.push(part);
}
}
}
return normalized.join('/');
};
var relative = function(base, path){
var uriParts = path.split("/"),
baseParts = base.split("/"),
result = [];
while ( uriParts.length && baseParts.length && uriParts[0] == baseParts[0] ) {
uriParts.shift();
baseParts.shift();
}
for (var i = 0 ; i < baseParts.length-1; i++) {
result.push("../");
}
return result.join("") + uriParts.join("/");
};