forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile-less.js
173 lines (150 loc) · 5.03 KB
/
compile-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
const path = Plugin.path;
const less = Npm.require('less');
const Future = Npm.require('fibers/future');
Plugin.registerCompiler({
// *.lessimport has been deprecated since 0.7.1, but it still works. We
// *recommend *.import.less or the imports subdirectory instead.
extensions: ['less', 'lessimport'],
archMatching: 'web'
}, () => new LessCompiler());
// CompileResult is {css, sourceMap}.
class LessCompiler extends MultiFileCachingCompiler {
constructor() {
super({
compilerName: 'less',
defaultCacheSize: 1024*1024*10,
});
}
getCacheKey(inputFile) {
return inputFile.getSourceHash();
}
compileResultSize(compileResult) {
return compileResult.css.length +
this.sourceMapSize(compileResult.sourceMap);
}
// The heuristic is that a file is an import (ie, is not itself processed as a
// root) if it is in a subdirectory named 'imports' or if it matches
// *.import.less. This can be overridden in either direction via an explicit
// `isImport` file option in api.addFiles.
isRoot(inputFile) {
const fileOptions = inputFile.getFileOptions();
if (fileOptions.hasOwnProperty('isImport')) {
return !fileOptions.isImport;
}
const pathInPackage = inputFile.getPathInPackage();
return !(/\.import\.less$/.test(pathInPackage) ||
/\.lessimport$/.test(pathInPackage) ||
/(?:^|\/)imports\//.test(pathInPackage));
}
compileOneFile(inputFile, allFiles) {
const importPlugin = new MeteorImportLessPlugin(allFiles);
const f = new Future;
let output;
try {
less.render(inputFile.getContentsAsBuffer().toString('utf8'), {
filename: this.getAbsoluteImportPath(inputFile),
plugins: [importPlugin],
// Generate a source map, and include the source files in the
// sourcesContent field. (Note that source files which don't themselves
// produce text (eg, are entirely variable definitions) won't end up in
// the source map!)
sourceMap: { outputSourceFiles: true }
}, f.resolver());
output = f.wait();
} catch (e) {
inputFile.error({
message: e.message,
sourcePath: decodeFilePath(e.filename),
line: e.line,
column: e.column
});
return null;
}
if (output.map) {
const map = JSON.parse(output.map);
map.sources = map.sources.map(decodeFilePath);
output.map = map;
}
const compileResult = {css: output.css, sourceMap: output.map};
const referencedImportPaths = [];
output.imports.forEach((path) => {
// Some files that show up in output.imports are not actually files; for
// example @import url("...");
if (allFiles.has(path)) {
referencedImportPaths.push(path);
}
});
return {compileResult, referencedImportPaths};
}
addCompileResult(inputFile, compileResult) {
inputFile.addStylesheet({
data: compileResult.css,
path: inputFile.getPathInPackage() + '.css',
sourceMap: compileResult.sourceMap
});
}
}
class MeteorImportLessPlugin {
constructor(allFiles) {
this.allFiles = allFiles;
this.minVersion = [2, 5, 0];
}
install(less, pluginManager) {
pluginManager.addFileManager(
new MeteorImportLessFileManager(this.allFiles));
}
}
class MeteorImportLessFileManager extends less.AbstractFileManager {
constructor(allFiles) {
super();
this.allFiles = allFiles;
}
// We want to be the only active FileManager, so claim to support everything.
supports(filename) {
// We shouldn't process files that start with `//` or a protocol because
// those are not relative to the app at all; they are probably native
// CSS imports
if (! filename.match(/^(https?:)?\/\//)) {
return true;
}
return false;
}
loadFile(filename, currentDirectory, options, environment, cb) {
const packageMatch = currentDirectory.match(/^(\{[^}]*\})/);
if (! packageMatch) {
// shouldn't happen. all filenames less ever sees should involve this {}
// thing!
cb(new Error('file without Meteor context? ' + currentDirectory));
return;
}
const currentPackagePrefix = packageMatch[1];
let resolvedFilename;
if (filename[0] === '/') {
// Map `/foo/bar.less` onto `{thispackage}/foo/bar.less`
resolvedFilename = currentPackagePrefix + filename;
} else if (filename[0] === '{') {
resolvedFilename = filename;
} else {
resolvedFilename = path.join(currentDirectory, filename);
}
if (!this.allFiles.has(resolvedFilename)) {
cb({type: 'File', message: 'Unknown import: ' + filename});
return;
}
cb(null, {
contents: this.allFiles.get(resolvedFilename)
.getContentsAsBuffer().toString('utf8'),
filename: resolvedFilename
});
}
}
function decodeFilePath (filePath) {
const match = filePath.match(/^{(.*)}\/(.*)$/);
if (! match)
throw new Error('Failed to decode Less path: ' + filePath);
if (match[1] === '') {
// app
return match[2];
}
return 'packages/' + match[1] + '/' + match[2];
}