-
Notifications
You must be signed in to change notification settings - Fork 2
/
gvc.js
68 lines (58 loc) · 2 KB
/
gvc.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
// Some views require a global window object to be present
GLOBAL.window = {
location: {
pathname: ''
},
document: {
createElement: function () {
return {
addEventListener: function () {},
setAttribute: function () {},
appendChild: function () {}
};
},
createTextNode: function () {
return {};
}
},
addEventListener: function () {}
};
GLOBAL.document = GLOBAL.window.document;
GLOBAL.XMLHttpRequest = function () {
return {
send: function () {},
addEventListener: function () {},
open: function () {},
setRequestHeader: function () {}
};
};
var gvc = {},
fs = require('graceful-fs'),
path = require('path'),
browserify = require('browserify'),
gelTransform = require('gel-transform');
gvc.transform = function transform(file) {
return gelTransform(file);
};
gvc.parse = function parse (filename, outputPath, callback) {
var object = browserify(filename).transform(gvc.transform);
object.bundle({}, function (error, data) {
if (error) return callback(error);
try {
// Dirty hack for finding which function to call on the Browserified module
var entryPoint = data.split(/\n/).slice(-2)[0].split('[').slice(-1)[0].replace('])', '');
var json = JSON.stringify(eval(data)(entryPoint));
// Cache buster
json = json.replace('{',
'{"_" : "<style>*{display:none;}</style><script>window.location = window.location + \'?_bust=\' + +new Date();<\/script>", ');
outputFilename = path.join(outputPath, path.basename(filename, path.extname(filename)) + '.json');
fs.writeFile(outputFilename, json, function (error) {
if (error) return callback(error);
callback(null, json);
});
} catch(exception){
callback(exception);
}
});
};
module.exports = gvc;