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