-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (57 loc) · 1.73 KB
/
index.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
'use strict';
const Mustache = require('mustache');
const crypto = require('crypto');
const path = require('path');
class AssetRevving {
constructor( opts ) {
this.mode = opts.mode;
this.env = opts.env;
this.main = opts.main;
}
templating( ) {
let template = '';
switch ( this.mode ) {
case 'css':
template = '@inherits System.Web.Mvc.WebViewPage' + '\n' + '<link rel="stylesheet" type="text/css" href="{{{file}}}" />';
break;
case 'requirejs-dev':
template = '@inherits System.Web.Mvc.WebViewPage' + '\n' + '<script data-main="{{{main}}}" src="{{{file}}}"></script>';
break;
case 'requirejs-production':
template = '@inherits System.Web.Mvc.WebViewPage' + '\n' + '<script src="{{{file}}}"></script>';
break;
default:
throw('Incorrect mode passed in:', this.mode );
}
return template;
}
renaming( filename, contents, cacheHandlerPrefix ) {
let renamed = path.basename(filename);
if ( this.env === 'production' ) {
let ext = path.extname(filename);
let base = path.basename(filename, ext);
let hash = crypto.createHash('md5').update( contents.toString() ).digest('hex');
renamed = '' + base + ext + '?v=' + hash;
}
return renamed;
}
revving( file, contents, cacheHandlerPrefix ) {
let assetPath = '';
let renamed = this.renaming( file, contents, cacheHandlerPrefix );
if ( this.env === 'production' ) {
assetPath = '/dist/production/'
}
if ( this.env === 'dev' ) {
assetPath = '/dist/dev/'
}
let template = this.templating();
let templateData = { file : '~' + assetPath + renamed };
if (this.main) {
templateData.main = assetPath + this.main;
}
return {
contents : Mustache.render(template, templateData)
}
}
}
module.exports = AssetRevving;