/
coffee_engine.js
108 lines (79 loc) · 2.23 KB
/
coffee_engine.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
/**
* class CoffeeEngine
*
* Engine for the CoffeeScript compiler. You will need `coffee-script` Node
* module installed in order to use [[Mincer]] with `*.coffee` and
* `*.litcoffee` files:
*
* npm install coffee-script
*
*
* ##### SUBCLASS OF
*
* [[Template]]
**/
'use strict';
// stdlib
var extname = require('path').extname;
// 3rd-party
var _ = require('lodash');
var path = require('path');
var coffee; // initialized later
// internal
var Template = require('../template');
var prop = require('../common').prop;
////////////////////////////////////////////////////////////////////////////////
// Class constructor
var CoffeeEngine = module.exports = function CoffeeEngine() {
Template.apply(this, arguments);
coffee = coffee || Template.libs.coffee || require('coffee-script');
};
require('util').inherits(CoffeeEngine, Template);
// Internal (private) options storage
var options = { bare: true };
/**
* CoffeeEngine.configure(opts) -> Void
* - opts (Object):
*
* Allows to set CoffeeScript compilation options.
* Default: `{bare: true}`.
*
* ##### Example
*
* CoffeeScript.configure({bare: true});
**/
CoffeeEngine.configure = function (opts) {
options = _.clone(opts);
};
// Render data
CoffeeEngine.prototype.evaluate = function (context/*, locals*/) {
var loc, compileOpts, result;
compileOpts = _.assign({}, options, {
literate: extname(this.file) === '.litcoffee'
});
try {
// coffescript result change structure, depending on
// soursemaps option.
if (context.environment.isEnabled('source_maps')) {
compileOpts = _.assign(compileOpts, {
inline: true,
sourceMap: true,
sourceFiles: [ path.basename(context.pathname) ]
});
result = coffee.compile(this.data, compileOpts);
this.data = result.js;
this.map = result.v3SourceMap;
} else {
this.data = coffee.compile(this.data, compileOpts);
}
} catch (err) {
loc = err.location;
if (loc) {
loc = 'L' + (loc.first_line + 1) + ':' + (loc.first_column + 1);
err.message += ' at ' + loc;
}
throw err;
}
};
// Expose default MimeType of an engine
prop(CoffeeEngine, 'defaultMimeType', 'application/javascript');