-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (152 loc) · 5.57 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var Esprima = require('esprima'),
Escodegen = require('escodegen'),
Path = require('path'),
Java = require('java');
Java.asyncOptions = { syncSuffix: '' };
Java.classpath.push(__dirname + '/java/build/ExtendedSoyToJsSrcCompiler.jar');
var SoyJsSrcOptions = Java.import('com.google.template.soy.jssrc.SoyJsSrcOptions');
var SoyFileSet = Java.import('com.google.template.soy.SoyFileSet');
var InMemCharSource = Java.import('commonjssoy.InMemCharSource');
var jsSrcOptions = new SoyJsSrcOptions();
jsSrcOptions.setShouldGenerateJsdoc(false);
jsSrcOptions.setShouldProvideRequireSoyNamespaces(true);
jsSrcOptions.setShouldProvideBothSoyNamespacesAndJsFunctions(true);
//jsSrcOptions.setShouldDeclareTopLevelNamespaces(false);
//jsSrcOptions.setShouldGenerateGoogModules(true);
jsSrcOptions.setShouldGenerateGoogMsgDefs(false);
jsSrcOptions.setGoogMsgsAreExternal(false);
jsSrcOptions.setBidiGlobalDir(0);
jsSrcOptions.setUseGoogIsRtlForBidiGlobalDir(false);
function traverse(ast, onMemberExpression) {
(function t(o) {
for (var prop in o) {
var val = o[prop];
if (val && val.type === 'MemberExpression') {
onMemberExpression(val, o);
}
if (Array.isArray(val)) {
val.forEach(function(v) { t(v); });
}else if (val && typeof val === 'object') {
t(val);
}
}
}(ast))
}
function toExpressionSrc(expression) {
return Escodegen.generate(expression)
}
function replaceWithSrc(node, src) {
var parts = Esprima.parse(src).body[0],
p;
parts = parts.expression || parts;
for (p in parts) {
node[p] = parts[p];
}
}
function createNamespaceSrc(namespaces, val) {
var root = namespaces[0],
full = namespaces.join('.');
return 'var ' + root + ' = (function(' + root + ') {' +
(namespaces.slice(0, -1).reduce(function(src, part, idx, o) {
var ns = o.slice(0, idx + 1).join('.');
return src + ns + ' = ' + ns + ' || {};\n'
}, '')) +
' ' + full + ' = ' + (val || 'null') + ';' +
' return ' + root +
'}(' + root + '));';
}
var api = module.exports = {
transpile: function(opts, callback) {
if (typeof opts === 'string') {
opts = {
content: opts
};
}
var file = opts.content,
filePath = opts.path || '<<unknown path>>',
resolver = opts.resolver || function(filePath, modulePath) { return modulePath; };
api.compileSoy(file, {
path: filePath
}, function(compileErr, soy) {
if (compileErr) {
callback(compileErr);
}else {
api.transformToCommonJs(soy, resolver.bind(this, filePath), function(transformErr, es6) {
callback(transformErr, es6);
});
}
});
},
// TODO: add setters to change SoyJsSrcOptions (and Builder options)
compileSoy: function(soyContent, opts, callback) {
var jsContent, err = null;
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
if (typeof opts.path === 'undefined') {
opts.path = '<<unknown path>>';
}
var sfsBuilder = SoyFileSet.builder();
sfsBuilder.add(InMemCharSource.create(soyContent.toString()), opts.path);
sfsBuilder.setAllowExternalCalls(true);
try {
jsContent = sfsBuilder.build().compileToJsSrc(jsSrcOptions, null).get(0);
}catch(e) {
err = e; // TODO: better handle!! Grab actual output and parse
}
callback(err, jsContent);
},
// TODO: need to fix double semicolon issue (although, should be moot when output is passed through a minifier :)
transformToCommonJs: function(contents, modulePathResolver, callback) {
var ast = Esprima.parse(contents);
if (typeof modulePathResolver === 'function' && typeof callback === 'undefined') {
callback = modulePathResolver;
modulePathResolver = null;
}
modulePathResolver = modulePathResolver || function() { return arguments[0]; };
traverse(ast, function onMemberExpression(node, parentNode) {
var replacementSrc = null,
replacementNode = parentNode,
expressionSrc = toExpressionSrc(node);
// TODO: hold off on ALL transforms until we confirm goog.module/goog.provide is equal to `_`
// ... requiring `_` indicates we've opted in to periods in namespaces to meaning file paths
switch (expressionSrc) {
case 'goog.provide':
replacementSrc = ';';
case 'goog.module':
case 'goog.provide':
var args = parentNode.arguments[0];
if (expressionSrc === 'goog.provide' && args.value !== '_') break;
replacementSrc = 'var ' + args.value + ' = exports = module.exports = {}';
//replacementSrc = createNamespaceSrc(args.value.split('.'), '{}');
break;
case 'goog.require':
var args = parentNode.arguments[0],
moduleId = args.value,
path = moduleId.replace(/\./g, Path.sep),
resolvedPath = modulePathResolver(path, moduleId);
replacementSrc = createNamespaceSrc(path.split(Path.sep), 'require(\'' + resolvedPath + '\')');
break;
case 'goog.DEBUG':
replacementSrc = 'true';
replacementNode = node;
break;
case 'goog.getCssName':
case 'goog.isBoolean':
case 'goog.isString':
case 'goog.isNumber':
case 'goog.isArray':
case 'goog.isObject':
case 'goog.isObject':
case 'goog.isObject':
// TODO: pass through for right now.
break;
}
if (replacementSrc !== null) {
replaceWithSrc(replacementNode, replacementSrc);
}
});
callback(null, Escodegen.generate(ast));
}
}