forked from gruntjs/grunt-contrib-coffee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoffee.js
253 lines (215 loc) · 7.65 KB
/
coffee.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
/*
* grunt-contrib-coffee
* http://gruntjs.com/
*
* Copyright (c) 2013 Eric Woroshow, contributors
* Licensed under the MIT license.
*/
module.exports = function(grunt) {
'use strict';
var path = require('path');
var chalk = require('chalk');
var _ = grunt.util._;
grunt.registerMultiTask('coffee', 'Compile CoffeeScript files into JavaScript', function() {
var options = this.options({
bare: false,
join: false,
sourceMap: false,
separator: grunt.util.linefeed
});
var allValidFiles = []
this.files.forEach(function (f) {
var validFiles = removeInvalidFiles(f);
allValidFiles.concat(validFiles)
if (options.sourceMap === true) {
var paths = createOutputPaths(f.dest);
// add sourceMapDir to options object
var fileOptions = _.extend({ sourceMapDir: paths.destDir }, options);
writeFileAndMap(paths, compileWithMaps(validFiles, fileOptions, paths), fileOptions);
} else if (options.join === true) {
writeFile(f.dest, concatInput(validFiles, options));
} else {
writeFile(f.dest, concatOutput(validFiles, options));
}
});
// Emit watch events if anyone is listening
if (grunt.event.listeners('coffee.compile.done').length > 0) {
grunt.event.emit('coffee.compile.done', allValidFiles, options);
}
});
var isLiterate = function (ext) {
return (ext === ".litcoffee" || ext === ".md");
};
var removeInvalidFiles = function(files) {
return files.src.filter(function(filepath) {
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
});
};
var createOutputPaths = function (destination) {
var fileName = path.basename(destination, path.extname(destination));
return {
dest: destination,
destName: fileName,
destDir: appendTrailingSlash(path.dirname(destination)),
mapFileName: fileName + '.js.map'
};
};
var appendTrailingSlash = function (path) {
if (path.length > 0) {
return path + '/';
} else {
return path;
}
};
var compileWithMaps = function (files, options, paths) {
if (!hasUniformExtensions(files)) {
return;
}
var mapOptions, filepath;
if (files.length > 1) {
mapOptions = createOptionsForJoin(files, paths, options.separator);
} else {
mapOptions = createOptionsForFile(files[0], paths);
filepath = files[0];
}
options = _.extend({
generatedFile: path.basename(paths.dest),
sourceRoot: mapOptions.sourceRoot,
sourceFiles: mapOptions.sourceFiles
}, options);
var output = compileCoffee(mapOptions.code, options, filepath);
appendFooter(output, paths, options);
return output;
};
var hasUniformExtensions = function(files) {
// get all extensions for input files
var ext = files.map(function (f) {
return path.extname(f);
});
if(_.uniq(ext).length > 1) {
grunt.fail.warn('Join and sourceMap options require input files share the same extension (found '+_.uniq(ext).join(', ')+').');
return false;
} else {
return true;
}
};
var createOptionsForJoin = function (files, paths, separator) {
var code = concatFiles(files, separator);
var targetFileName = paths.destName + '.src.coffee';
grunt.file.write(paths.destDir + targetFileName, code);
return {
code: code,
sourceFiles: [targetFileName],
sourceRoot: ''
};
};
var concatFiles = function (files, separator) {
return files.map(function (filePath) {
return grunt.file.read(filePath);
}).join(grunt.util.normalizelf(separator));
};
var createOptionsForFile = function (file, paths) {
return {
code: grunt.file.read(file),
sourceFiles: [path.basename(file)],
sourceRoot: appendTrailingSlash(path.relative(paths.destDir, path.dirname(file)))
};
};
var appendFooter = function (output, paths, options) {
// we need to sourceMappingURL to be relative to the js path
var sourceMappingDir = paths.destDir.replace(/[^/]+/g, '..') + options.sourceMapDir;
// Add sourceMappingURL to file footer
output.js = output.js + '\n/*\n//# sourceMappingURL=' + sourceMappingDir + paths.mapFileName + '\n*/';
};
var concatInput = function (files, options) {
if (!hasUniformExtensions(files)) {
return;
}
var code = concatFiles(files, options.separator);
return compileCoffee(code, options);
};
var concatOutput = function(files, options) {
return files.map(function(filepath) {
var code = grunt.file.read(filepath);
return compileCoffee(code, options, filepath);
}).join(grunt.util.normalizelf(options.separator));
};
var compileCoffee = function(code, options, filepath) {
var coffeeOptions = _.clone(options);
if(filepath) {
coffeeOptions.filename = filepath;
coffeeOptions.literate = isLiterate(path.extname(filepath));
}
try {
return require('coffee-script').compile(code, coffeeOptions);
} catch (e) {
if (e.location == null ||
e.location.first_column == null ||
e.location.first_line == null) {
grunt.log.error('Got an unexpected exception ' +
'from the coffee-script compiler. ' +
'The original exception was: ' +
e);
grunt.log.error('(The coffee-script compiler should not raise *unexpected* exceptions. ' +
'You can file this error as an issue of the coffee-script compiler: ' +
'https://github.com/jashkenas/coffee-script/issues)');
} else {
var firstColumn = e.location.first_column;
var firstLine = e.location.first_line;
var codeLine = code.split('\n')[firstLine];
var errorArrows = chalk.red('>>') + ' ';
var offendingCharacter;
if (firstColumn < codeLine.length) {
offendingCharacter = chalk.red(codeLine[firstColumn]);
} else {
offendingCharacter = '';
}
grunt.log.error(e);
grunt.log.error('In file: ' + filepath);
grunt.log.error('On line: ' + firstLine);
// Log erroneous line and highlight offending character
// grunt.log.error trims whitespace so we have to use grunt.log.writeln
grunt.log.writeln(errorArrows + codeLine.substring(0, firstColumn) +
offendingCharacter + codeLine.substring(firstColumn + 1));
grunt.log.writeln(errorArrows + grunt.util.repeat(firstColumn, ' ') +
chalk.red('^'));
}
grunt.fail.warn('CoffeeScript failed to compile.');
}
};
var writeFileAndMap = function(paths, output, options) {
if (!output || output.js.length === 0) {
warnOnEmptyFile(paths.dest);
return;
}
writeCompiledFile(paths.dest, output.js);
writeSourceMapFile(options.sourceMapDir + paths.mapFileName, output.v3SourceMap);
};
var warnOnEmptyFile = function (path) {
grunt.log.warn('Destination "' + path + '" not written because compiled files were empty.');
};
var writeFile = function (path, output) {
if (output.length < 1) {
warnOnEmptyFile(path);
return false;
} else {
grunt.file.write(path, output);
return true;
}
};
var writeCompiledFile = function (path, output) {
if (writeFile(path, output)) {
grunt.log.writeln('File ' + chalk.cyan(path) + ' created.');
}
};
var writeSourceMapFile = function (path, output) {
if (writeFile(path, output)) {
grunt.log.writeln('File ' + chalk.cyan(path) + ' created (source map).');
}
};
};