-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (82 loc) · 2.6 KB
/
Copy pathindex.js
File metadata and controls
92 lines (82 loc) · 2.6 KB
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
var loaderUtils = require('loader-utils'),
Promise = require('bluebird'),
soynode = Promise.promisifyAll(require('soynode')),
fs = Promise.promisifyAll(require('fs')),
rimrafAsync = Promise.promisify(require('rimraf')),
path = require('path'),
EOL = require('os').EOL;
module.exports = function(source) {
var query = loaderUtils.parseQuery(this.query),
callback = this.async(),
tempDir = path.join(__dirname, Math.random() + Date.now() + ''),
namespace = /\{namespace\s+(.*?)\}/.exec(source)[1].split(' ')[0],
filename = path.basename(this.resourcePath, '.soy'),
yuiAdd = '',
lang = query.lang,
index = 0,
// TODO(jrubstein) Make this configurable through query.
regex = /LANG\(\\'(.*?)\\'\)/g,
debug = 'var goog = {};';
if (query.yui) {
yuiAdd = [
'var Y;',
'YUI.add(\'' + filename + '\', function(Y1) {',
' Y = Y1;',
'}, \'1.0.0\', {',
' \'requires\': [\'wt2-templates\']',
'});',
].join(EOL);
}
if (query.debug) {
debug += EOL + 'goog.DEBUG = true';
}
this.cacheable && this.cacheable();
soynode.setOptions({
outputDir: '/',
uniqueDir: false,
eraseTemporaryFiles: false
});
fs.mkdirAsync(tempDir)
// Get the temp directory path
.then(function() {
dirPath = tempDir;
// Handle drive letters in windows environments (C:\)
if (dirPath.indexOf(':') !== -1) {
dirPath = dirPath.split(':')[1];
}
return path.join(dirPath, 'source.soy');
// Write the raw source template into the temp directory
}).then(function(soyPath) {
return fs.writeFileAsync(path.resolve(soyPath), source).return(soyPath);
// Run the compiler on the raw template
}).then(function(soyPath) {
return soynode.compileTemplateFilesAsync([soyPath]).return(soyPath);
// Read the newly compiled source
}).then(function(soyPath) {
return fs.readFileAsync(path.resolve(soyPath) + '.js');
// Return utils and module return value, shimmed for module encapsulation.
}).then(function(template) {
template = template + '';
if (lang) {
var results = [], result;
while ((result = regex.exec(template)) !== null) {
results.push(result[1]);
}
for (; index < results.length; index++) {
template = template.replace('LANG(\\\'' + results[index]+ '\\\')', lang[results[index]]);
}
}
return callback(null, [
yuiAdd,
debug,
template,
'module.exports = ' + namespace + ';'
].join(EOL));
// Handle any errors
}).catch(function(e) {
return callback(e);
// Cleanup temp directory
}).finally(function(template) {
return rimrafAsync(tempDir).return(template);
});
};