-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.js
97 lines (86 loc) · 2.85 KB
/
plugin.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
var fs = Npm.require('fs');
var less = Npm.require('less');
var path = Npm.require('path');
var LessPluginAutoPrefix = Npm.require('less-plugin-autoprefix');
var DEFAULT_INDEX_FILE_PATH = "./client/main.less";
var OPTIONS_FILE = "config/less.json";
var generatedMessage = [
"// This file is auto generated by the grove:less package",
"// New .less files will be automatically imported at the bottom",
"// Existing contents will not be touched",
"// When you delete a less file you must manually delete the import from here as well",
"",
""
].join("\n");
var loadJSONFile = function (compileStep, filePath) {
try {
return JSON.parse( fs.readFileSync(filePath) );
} catch (e) {
compileStep.error({
message: "Failed to parse " + filePath + " as JSON"
});
return {};
}
};
var convertToPosixPath = function(filePath) {
return filePath.split(path.sep).join('/');
};
Plugin.registerSourceHandler("less", {archMatching: 'web'}, function (compileStep) {
// Reading in user configuration
var config = {};
if ( fs.existsSync(OPTIONS_FILE) ) {
config = loadJSONFile(compileStep, OPTIONS_FILE);
}
// Load order setup
if ( config.useIndex ) {
var indexFilePath = config.indexFilePath || DEFAULT_INDEX_FILE_PATH,
posixInputPath = convertToPosixPath(compileStep.inputPath);
// If this isn't the index file, add it to the index if need be
if ( posixInputPath != indexFilePath ) {
if ( fs.existsSync(indexFilePath) ) {
var lessIndex = fs.readFileSync(indexFilePath, 'utf8');
if ( lessIndex.indexOf(posixInputPath) == -1 ) {
fs.appendFileSync(indexFilePath, '\n@import "' + posixInputPath + '";', 'utf8');
}
} else {
var newFile = generatedMessage + '@import "' + posixInputPath + '";\n';
fs.writeFileSync(indexFilePath, newFile, 'utf8');
}
return; // stop here, only compile the index file
}
}
// Autoprefixing setup
var autoprefixer;
if ( config.enableAutoprefixer ) {
if ( config.autoprefixerOptions ) {
autoprefixer = new LessPluginAutoPrefix(config.autoprefixerOptions);
} else {
autoprefixer = new LessPluginAutoPrefix();
}
}
plugins = [];
if (autoprefixer) plugins.push(autoprefixer);
// Compiler options
var options = {
syncImport: true,
paths: config.includePaths || [],
plugins: plugins
};
var source = compileStep.read().toString('utf8');
less.render(source, options, function(error, output) {
if (error) {
compileStep.error({
message: "Less compiler error: " + error.message,
sourcePath: error.filename || compileStep.inputPath,
line: error.line,
column: error.column
});
} else {
compileStep.addStylesheet({
path: compileStep.inputPath + ".css",
data: output.css,
sourceMap: output.map
});
}
});
});