-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
192 lines (157 loc) · 6.89 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
/* jshint node: true */
'use strict';
var fs = require('fs');
var path = require('path');
var recast = require('recast');
var stew = require('broccoli-stew');
var funnel = require('broccoli-funnel');
var merge = require('broccoli-merge-trees');
var concat = require('broccoli-concat');
var clone = require('clone');
var header = 'import RouteRecognizer from \'ember-route-recognizer/-private/route-recognizer\';\n';
var serialized = 'var serialized = JSON.parse(\'{}\');\n'
// Grab the first object inside of the patch.
var patch;
recast.visit(recast.parse(fs.readFileSync(__dirname + '/lib/patch.js', 'utf8')), {
visitObjectExpression: function(path) {
patch = path.value.properties;
return false;
}
});
module.exports = {
name: 'ember-route-recognizer',
shouldIncludeChildAddon: function(childAddon) {
if (childAddon.name === 'loader.js') {
return false;
} else {
return this._super.shouldIncludeChildAddon.apply(this, arguments);
}
},
preprocessTree: function(type, tree) {
return tree;
if (type !== 'js') { return tree; }
// Get router.js
var originalRouterTree = stew.find(tree, '*/router.js');
originalRouterTree = stew.rename(originalRouterTree, 'router.js', 'router.original.js');
originalRouterTree = stew.map(originalRouterTree, '*/router.original.js', function(content) {
content = content.replace(/import routerSetup.*/g, '');
content = content.replace(/extend\(routerSetup.*\{/g, 'extend({');
return content;
});
var modifiedRouterTree = stew.map(tree, '*/router.js', function(content) {
// Prepend the import for the new route-recognizer.
content = header + serialized + content;
// Parse the AST and walk it to insert the patch.
var ast = recast.parse(content);
recast.visit(ast, {
visitMemberExpression: function(path) {
// Find `Router.map` and its arguments.
if (path.value.property.name === 'map' && path.value.object.name === 'Router') {
path.parentPath.replace();
}
// Find `Router.extend` and its arguments.
if (path.value.property.name === 'extend' && path.value.object.property.name === 'Router') {
var args = path.parentPath.value.arguments;
var extendObject = args[args.length - 1];
extendObject.properties = extendObject.properties.concat(patch);
}
this.traverse(path);
}
});
return recast.print(ast).code;
});
var result = new merge([originalRouterTree, modifiedRouterTree]);
return result;
},
postprocessTree: function(type, tree) {
return tree;
if (type !== 'js') { return tree; }
var addonTree = require('broccoli-babel-transpiler')(this.treeFor('addon'), getBabelOptions(this));
var loaderjsPath = require.resolve('loader.js');
var loaderjsTree = new funnel(path.dirname(loaderjsPath), {
destDir: '_precompile'
});
var emberPath = path.join(this.app.bowerDirectory, 'ember');
var emberTree = new funnel(emberPath, {
include: ['ember.prod.js'],
destDir: '_precompile'
});
var config;
var configPath = path.join(this.app.name, 'config', 'environments', this.app.env + '.json');
var configTree = this.app._configTree();
configTree = stew.map(configTree, configPath, function(content) {
config = JSON.parse(content);
var module = fs.readFileSync(__dirname + '/lib/config-module.js', 'utf8');
module = module.replace(/\{\{MODULE_PREFIX\}\}/g, config.modulePrefix);
module = module.replace(/\{\{CONFIG_JSON\}\}/g, content);
return module;
});
configTree = stew.find(configTree, configPath);
configTree = stew.mv(configTree, configPath, '_precompile/config.js');
var routerTree = stew.find(tree, '*/router.original.js');
routerTree = stew.mv(routerTree, '*/router.original.js', '_precompile/router.js');
var serialized;
var precompile = new merge([loaderjsTree, emberTree, configTree, routerTree, addonTree]);
precompile = concat(precompile, {
outputFile: 'precompile.js',
headerFiles: ['_precompile/loader.js'],
footerFiles: ['_precompile/ember.prod.js', '_precompile/config.js', '_precompile/router.js'],
inputFiles: ['**/*']
});
precompile = stew.map(precompile, 'precompile.js', function(content) {
// Patch Ember.
content = content.replace('_initRouterJs: function () {', '_initRouterJs: function (options, recognizer) {');
content = content.replace('new _router4.default();', 'new _router4.default(options, recognizer);');
content = content.replace('function Router(_options)', 'function Router(_options, recognizer)');
content = content.replace(/this.recognizer[^;]*/, 'this.recognizer = new recognizer();');
var footer = fs.readFileSync(__dirname + '/lib/precompile-footer.js', 'utf8');
footer = footer.replace('dummy/router.original', config.modulePrefix + '/router.original')
fs.writeFileSync('precompile.js', content + footer, 'utf8');
serialized = require(path.join(process.cwd(), '/precompile'));
fs.unlink('precompile.js')
return JSON.stringify(serialized.routes);
});
precompile = stew.rename(precompile, 'precompile.js', 'precompile.json');
var result = new merge([precompile, tree]);
// Insert the route-alias application lookup.
result = stew.map(result, '**/initializers/recognizer-route-alias.js', function(content) {
content = content.replace('{}', JSON.stringify(serialized.lookup));
return content;
});
result = stew.rm(result, '*/router.original.js')
result = stew.map(result, '*/router.js', function(content) {
content = content.replace("JSON.parse('{}')", "JSON.parse('"+serialized.routes+"')");
return content;
});
return result;
}
};
function getBabelOptions(addonContext) {
var options = clone(getAddonOptions(addonContext));
// Ensure modules aren't compiled unless explicitly set to compile
options.blacklist = options.blacklist || ['es6.modules'];
// do not enable non-standard transforms
if (!('nonStandard' in options)) {
options.nonStandard = false;
}
// Don't include the `includePolyfill` flag, since Babel doesn't care
delete options.includePolyfill;
if (options.compileModules === true) {
if (options.blacklist.indexOf('es6.modules') >= 0) {
options.blacklist.splice(options.blacklist.indexOf('es6.modules'), 1);
}
delete options.compileModules;
} else {
if (options.blacklist.indexOf('es6.modules') < 0) {
options.blacklist.push('es6.modules');
}
}
// Ember-CLI inserts its own 'use strict' directive
options.blacklist.push('useStrict');
options.highlightCode = false;
return options;
}
function getAddonOptions(addonContext) {
var baseOptions = (addonContext.parent && addonContext.parent.options) || (addonContext.app && addonContext.app.options);
return baseOptions && baseOptions.babel || {};
}