1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | 'use strict';
|
14 |
|
15 | const fs = require('fs');
|
16 | const path = require('path');
|
17 | const cheerio = require('cheerio');
|
18 | const Filter = require('broccoli-filter');
|
19 | const esprima = require('esprima');
|
20 | const eswalk = require('esprima-walk');
|
21 | const _ = require('lodash');
|
22 | const beautify_js = require('js-beautify');
|
23 | const beautify_html = require('js-beautify').html;
|
24 | var SilentError = require('silent-error');
|
25 |
|
26 |
|
27 | let root;
|
28 |
|
29 |
|
30 | var indexHtmlCache = {
|
31 | app: {
|
32 | modulesAsString: '',
|
33 | startScript: '',
|
34 | startFileName: ''
|
35 | },
|
36 | test: {
|
37 | modulesAsString: '',
|
38 | startScript: '',
|
39 | startFileName: ''
|
40 | }
|
41 | };
|
42 |
|
43 |
|
44 | const startTemplate = _.template(fs.readFileSync(path.join(__dirname, 'start-template.txt'), 'utf8'));
|
45 |
|
46 |
|
47 | const identifiers = {
|
48 | 'require': 'eriuqer',
|
49 | 'define': 'enifed'
|
50 | };
|
51 |
|
52 | const literals = {
|
53 | 'require': '\'eriuqer\'',
|
54 | '(require)': '\'(eriuqer)\''
|
55 | };
|
56 |
|
57 | module.exports = {
|
58 |
|
59 | name: 'ember-cli-amd',
|
60 |
|
61 | amdModules: new Set(),
|
62 |
|
63 | included: function(app) {
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | root = app.project.root;
|
69 |
|
70 |
|
71 | if (!app.options.amd) {
|
72 | return new SilentError('ember-cli-amd: No amd options specified in the ember-cli-build.js file.');
|
73 | }
|
74 |
|
75 |
|
76 | app.options.amd = _.merge({ packages: [], excludePaths: [] }, app.options.amd);
|
77 |
|
78 |
|
79 | if (!app.options.amd.loader) {
|
80 | throw new Error('ember-cli-amd: You must specify a loader option the amd options in ember-cli-build.js.');
|
81 | }
|
82 | },
|
83 |
|
84 | postprocessTree: function(type, tree) {
|
85 |
|
86 |
|
87 | if (!this.app.options.amd) {
|
88 | return tree;
|
89 | }
|
90 |
|
91 | if (type !== 'all') {
|
92 | return tree;
|
93 | }
|
94 |
|
95 |
|
96 | return new RequireFilter(tree, {
|
97 | amdPackages: this.app.options.amd.packages,
|
98 | amdModules: this.amdModules,
|
99 | excludePaths: this.app.options.amd.excludePaths
|
100 | });
|
101 | },
|
102 |
|
103 | postBuild: function(result) {
|
104 |
|
105 | if (!this.app.options.amd) {
|
106 | return;
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | const moduleInfos = this.buildModuleInfos();
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | var amdConfigScript;
|
124 | if (this.app.options.amd.configPath) {
|
125 | amdConfigScript = fs.readFileSync(path.join(root, this.app.options.amd.configPath), 'utf8');
|
126 | }
|
127 |
|
128 |
|
129 | this.indexBuilder({
|
130 | directory: result.directory,
|
131 | indexFile: this.app.options.outputPaths.app.html,
|
132 | indexHtmlCache: indexHtmlCache.app,
|
133 | amdConfigScript,
|
134 | startSrc: 'amd-start',
|
135 | moduleInfos
|
136 | });
|
137 |
|
138 |
|
139 | this.indexBuilder({
|
140 | directory: result.directory,
|
141 | indexFile: 'tests/index.html',
|
142 | indexHtmlCache: indexHtmlCache.test,
|
143 | amdConfigScript,
|
144 | startSrc: 'amd-test-start',
|
145 | moduleInfos
|
146 | });
|
147 | },
|
148 |
|
149 | indexBuilder: function(config) {
|
150 |
|
151 |
|
152 |
|
153 | var indexPath = path.join(config.directory, config.indexFile);
|
154 |
|
155 | var indexHtml;
|
156 | try {
|
157 | indexHtml = fs.readFileSync(indexPath, 'utf8');
|
158 | } catch (e) {
|
159 |
|
160 | return null;
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | const cheerioQuery = cheerio.load(indexHtml);
|
167 | const amdScriptElements = cheerioQuery('script[data-amd]')
|
168 | if (amdScriptElements.length === 1 && config.indexHtmlCache.modulesAsString === config.moduleInfos.names) {
|
169 | return config.indexHtmlCache;
|
170 | }
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | var scriptElements = cheerioQuery('body > script');
|
176 | var scriptsToLoad = [];
|
177 | var scriptsToPostExecute = [];
|
178 | scriptElements.each(function() {
|
179 | if (cheerioQuery(this).attr('src')) {
|
180 | scriptsToLoad.push(`"${cheerioQuery(this).attr('src')}"`)
|
181 | } else {
|
182 | scriptsToPostExecute.push(cheerioQuery(this).html());
|
183 | }
|
184 | });
|
185 |
|
186 |
|
187 | scriptElements.remove();
|
188 |
|
189 |
|
190 |
|
191 | if (scriptsToPostExecute.length > 0) {
|
192 | var afterLoadingScript = replaceRequireAndDefine(scriptsToPostExecute.join('\n\n'));
|
193 | fs.writeFileSync(path.join(config.directory, 'afterLoading.js'), beautify_js(afterLoadingScript, {
|
194 | indent_size: 2
|
195 | }));
|
196 | scriptsToLoad.push('"/afterLoading.js"');
|
197 | }
|
198 |
|
199 |
|
200 | config.indexHtmlCache.modulesAsString = config.moduleInfos.names;
|
201 |
|
202 |
|
203 | var amdScripts = '';
|
204 | if (this.app.options.amd.configPath) {
|
205 | amdScripts += '<script>' + config.amdConfigScript + '</script>';
|
206 | } else if (this.app.options.amd.configScript) {
|
207 | amdScripts += '<script>' + this.app.options.amd.configScript + '</script>';
|
208 | }
|
209 |
|
210 |
|
211 | var loaderSrc = this.app.options.amd.loader;
|
212 | amdScripts += `<script src="${loaderSrc}" data-amd="true"></script>`;
|
213 |
|
214 |
|
215 | var startScript = startTemplate(_.assign(config.moduleInfos, {
|
216 | scripts: scriptsToLoad.join(',')
|
217 | }));
|
218 |
|
219 |
|
220 | amdScripts += '<script>' + startScript + '</script>';
|
221 |
|
222 |
|
223 | cheerioQuery('body').prepend(amdScripts);
|
224 |
|
225 |
|
226 | var html = beautify_html(cheerioQuery.html(), {
|
227 | indent_size: 2
|
228 | });
|
229 |
|
230 |
|
231 | fs.writeFileSync(indexPath, html);
|
232 |
|
233 | return config.indexHtmlCache;
|
234 | },
|
235 |
|
236 | buildModuleInfos: function() {
|
237 |
|
238 |
|
239 | const objs = [];
|
240 | const names = [];
|
241 | const adoptables = [];
|
242 | let index = 0;
|
243 | this.amdModules.forEach(function(amdModule) {
|
244 | objs.push(`mod${index}`);
|
245 | names.push(`'${amdModule}'`);
|
246 | adoptables.push(`{name:'${amdModule}',obj:mod${index}}`);
|
247 | index++;
|
248 | });
|
249 |
|
250 | return {
|
251 | names: names.join(','),
|
252 | objects: objs.join(','),
|
253 | adoptables: adoptables.join(','),
|
254 | vendor: path.parse(this.app.options.outputPaths.vendor.js).name
|
255 | };
|
256 | }
|
257 | };
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | function RequireFilter(inputTree, options) {
|
264 | if (!(this instanceof RequireFilter)) {
|
265 | return new RequireFilter(inputTree, options);
|
266 | }
|
267 |
|
268 | Filter.call(this, inputTree, options);
|
269 |
|
270 | options = options || {};
|
271 |
|
272 | this.inputTree = inputTree;
|
273 | this.files = options.files || [];
|
274 | this.description = options.description;
|
275 | this.amdPackages = options.amdPackages || [];
|
276 | this.amdModules = options.amdModules;
|
277 | this.excludePaths = options.excludePaths;
|
278 | }
|
279 |
|
280 | RequireFilter.prototype = Object.create(Filter.prototype);
|
281 | RequireFilter.prototype.constructor = RequireFilter;
|
282 |
|
283 | RequireFilter.prototype.extensions = ['js'];
|
284 | RequireFilter.prototype.targetExtension = 'js';
|
285 |
|
286 | RequireFilter.prototype.getDestFilePath = function(relativePath) {
|
287 | relativePath = Filter.prototype.getDestFilePath.call(this, relativePath);
|
288 | if (!relativePath) {
|
289 | return relativePath;
|
290 | }
|
291 | for (var i = 0, len = this.excludePaths.length; i < len; i++) {
|
292 | if (relativePath.indexOf(this.excludePaths[i]) === 0) {
|
293 | return null;
|
294 | }
|
295 | }
|
296 | return relativePath;
|
297 | }
|
298 | RequireFilter.prototype.processString = function(code) {
|
299 | return replaceRequireAndDefine(code, this.amdPackages, this.amdModules);
|
300 | };
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | function write(arr, str, range) {
|
311 | const offset = range[0];
|
312 | arr[offset] = str;
|
313 | for (let i = offset + 1; i < range[1]; i++) {
|
314 | arr[i] = undefined;
|
315 | }
|
316 | }
|
317 |
|
318 |
|
319 |
|
320 | function replaceRequireAndDefine(code, amdPackages, amdModules) {
|
321 |
|
322 | const ast = esprima.parseScript(code, {
|
323 | range: true
|
324 | });
|
325 |
|
326 |
|
327 | const buffer = code.split('');
|
328 |
|
329 |
|
330 | eswalk(ast, function(node) {
|
331 | if (!node) {
|
332 | return;
|
333 | }
|
334 |
|
335 | switch (node.type) {
|
336 | case 'CallExpression':
|
337 |
|
338 | if (!amdPackages || !amdModules) {
|
339 |
|
340 | break;
|
341 | }
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | if (node.callee.name === 'define') {
|
347 |
|
348 | if (node.arguments.length < 2 || node.arguments[1].type !== 'ArrayExpression' || !node.arguments[1].elements) {
|
349 | return;
|
350 | }
|
351 |
|
352 | node.arguments[1].elements.forEach(function(element) {
|
353 | if (element.type !== 'Literal') {
|
354 | return;
|
355 | }
|
356 |
|
357 | const isAMD = amdPackages.some(function(amdPackage) {
|
358 | if (typeof element.value !== 'string') {
|
359 | return false;
|
360 | }
|
361 | return element.value.indexOf(amdPackage + '/') === 0 || element.value === amdPackage;
|
362 | });
|
363 |
|
364 | if (!isAMD) {
|
365 | return;
|
366 | }
|
367 |
|
368 | amdModules.add(element.value);
|
369 |
|
370 | });
|
371 |
|
372 | return;
|
373 | }
|
374 |
|
375 |
|
376 | if (node.callee.name === 'eval' && node.arguments[0].type === 'Literal' && typeof node.arguments[0].value === 'string') {
|
377 | const evalCode = node.arguments[0].value;
|
378 | const evalCodeAfter = replaceRequireAndDefine(evalCode, amdPackages, amdModules);
|
379 | if (evalCode !== evalCodeAfter) {
|
380 | write(buffer, "eval(" + JSON.stringify(evalCodeAfter) + ");", node.range);
|
381 | }
|
382 | }
|
383 |
|
384 | return;
|
385 |
|
386 | case 'Identifier':
|
387 | {
|
388 |
|
389 | if (!identifiers.hasOwnProperty(node.name)) {
|
390 | return;
|
391 | }
|
392 |
|
393 | const identifier = identifiers[node.name];
|
394 | if (!identifier) {
|
395 | return;
|
396 | }
|
397 |
|
398 | write(buffer, identifier, node.range);
|
399 | }
|
400 | return;
|
401 |
|
402 | case 'Literal':
|
403 | {
|
404 |
|
405 | if (!literals.hasOwnProperty(node.value)) {
|
406 | return;
|
407 | }
|
408 |
|
409 | const literal = literals[node.value];
|
410 | if (!literal) {
|
411 | return;
|
412 | }
|
413 |
|
414 | write(buffer, literal, node.range);
|
415 | }
|
416 | return;
|
417 | }
|
418 | });
|
419 |
|
420 |
|
421 | return buffer.join('');
|
422 | } |
\ | No newline at end of file |