forked from systemjs/plugin-less
-
Notifications
You must be signed in to change notification settings - Fork 0
/
less.js
76 lines (64 loc) · 2.39 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
var CSSPluginBase = require('css/css-plugin-base.js');
var isWin = typeof process != 'undefined' && process.platform.match(/^win/);
function fromFileURL(url) {
return url.substr(7 + !!isWin).replace(/\//g, isWin ? '\\' : '/');
}
module.exports = new CSSPluginBase(function compile(style, address, opts) {
var loader = this;
// use a file path in Node and a URL in the browser
var filename = this.builder ? fromFileURL(address) : address;
var systemPlugin = {
install: function(less, pluginManager) {
pluginManager.addFileManager(makeFileManager(less, loader, address))
},
minVersion: [2, 1, 1]
}
return System['import']('lesscss', module.id)
.then(function(less) {
return less.render(style, {
plugins: [systemPlugin],
filename: filename,
rootpath: (!opts.fileAsRoot || !loader.builder) && filename.replace(/[^/]+$/, ''),
paths: opts.fileAsRoot && [filename.replace(/[^/]+$/, '')],
relativeUrls: opts.relativeUrls || false,
sourceMap: loader.builder && {
sourceMapBasepath: filename.replace(/[^/]+$/, '')
}
});
})
.then(function(output) {
return {
css: output.css + (loader.builder ? '' : ('/*# sourceURL=' + filename + '*/')),
map: output.map,
// style plugins can optionally return a modular module
// source as well as the stylesheet above
moduleSource: null,
moduleFormat: null
};
});
});
// based on webkit less-loader
function makeFileManager(less, loader, address) {
function SystemFileManager(){ less.FileManager.apply(this, arguments) }
SystemFileManager.prototype = Object.create(less.FileManager.prototype);
SystemFileManager.prototype.supports = function(filename, currentDirectory, options, environment) {
// SystemFileManager handles all the files
return true;
};
SystemFileManager.prototype.supportsSync = function(filename, currentDirectory, options, environment) {
return false
};
SystemFileManager.prototype.loadFile = function(filename, currentDirectory, options, environment, callback) {
System['normalize'](filename, address).then(function(path){
return System['fetch']({ address: path, metadata: {} })
}).then(function(contents){
callback(null, {
contents: contents,
filename: filename
})
}).catch(function(reason){
callback(reason, null)
})
}
return new SystemFileManager()
}