-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenvironment.js
99 lines (69 loc) · 1.95 KB
/
environment.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
'use strict';
//
// Require some modules
//
var UglifyJS = require('uglify-js');
var Csso = require('csso');
var Mincer = require('mincer');
//
// Configure Mincers logger, by default, all
// messages are going to the middle of nowhere
//
Mincer.logger.use(console);
//
// Create and export environment
//
var environment = module.exports = new Mincer.Environment(__dirname);
//
// Configure environment load paths (where to find ssets)
//
environment.appendPath('javascripts');
environment.appendPath('stylesheets');
environment.appendPath('images');
environment.appendPath('javascripts/vendor');
environment.appendPath('stylesheets/vendor');
environment.appendPath('images/vendor');
environment.appendPath('templates');
//
// Define environment helper that will be available in the processed assets
// See `assets/stylesheets/app.css.ejs` for example of `asset_path` usage.
//
environment.registerHelper('asset_path', function (logicalPath) {
var asset = environment.findAsset(logicalPath);
if (!asset) {
throw new Error("File " + logicalPath + " not found");
}
return '/assets/' + asset.digestPath;
});
//
// Set JS and CSS compressors
//
if ('production' === process.env.NODE_ENV) {
//
// In production we assume that assets are not changed between requests,
// so we use cached version of environment. See API docs for details.
//
environment = environment.index;
//
// Enable JS and CSS compression
//
environment.jsCompressor = function (context, data, callback) {
try {
var ast = UglifyJS.parser.parse(data);
ast = UglifyJS.uglify.ast_mangle(ast);
ast = UglifyJS.uglify.ast_squeeze(ast);
callback(null, UglifyJS.uglify.gen_code(ast));
} catch (err) {
callback(err);
}
};
environment.cssCompressor = function (context, data, callback) {
try {
callback(null, Csso.justDoIt(data));
} catch (err) {
callback(err);
}
};
}
//
// "Th-th-th-that's all folks!"