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 Filter = require('broccoli-filter');
|
18 | const cheerio = require('cheerio');
|
19 | const beautify_js = require('js-beautify');
|
20 | const beautify_html = require('js-beautify').html;
|
21 | const _ = require('lodash');
|
22 |
|
23 | const replaceRequireAndDefine = require('./replace-require-and-define');
|
24 |
|
25 | const amdLoadingTemplate = _.template(fs.readFileSync(path.join(__dirname, 'amd-loading.txt'), 'utf8'));
|
26 | const indexFiles = ['index.html', 'tests/index.html'];
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | module.exports = class ConvertToAMD extends Filter {
|
32 | constructor(inputTree, options = {}) {
|
33 | super(inputTree, {});
|
34 |
|
35 | this.extensions = ['js', 'html'];
|
36 |
|
37 |
|
38 | this.loader = options.amdOptions.loader;
|
39 | this.amdPackages = options.amdOptions.packages || [];
|
40 | this.excludePaths = options.amdOptions.excludePaths;
|
41 | this.loadingFilePath = (options.amdOptions.loadingFilePath || 'assets').replace(/\/$/, "");
|
42 | this.rootURL = options.rootURL || '';
|
43 | this.inline = !!options.amdOptions.inline;
|
44 |
|
45 |
|
46 |
|
47 | this.externalAmdModules = new Set();
|
48 | this.externalAmdModulesCache = new Map();
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | this.indexHtmlCaches = {
|
55 | 'index.html': {
|
56 | scriptsToLoad: [],
|
57 | loadingScript: this.loadingFilePath + '/amd-loading.js',
|
58 | afterLoadingScript: this.loadingFilePath + '/after-amd-loading.js'
|
59 | },
|
60 | 'tests/index.html': {
|
61 | scriptsToLoad: [],
|
62 | loadingScript: this.loadingFilePath + '/amd-loading-tests.js',
|
63 | afterLoadingScript: this.loadingFilePath + '/after-amd-loading-tests.js'
|
64 | }
|
65 | };
|
66 | }
|
67 |
|
68 | getDestFilePath(relativePath) {
|
69 | relativePath = super.getDestFilePath(relativePath);
|
70 | if (!relativePath) {
|
71 | return null;
|
72 | }
|
73 |
|
74 | if (relativePath.indexOf('index.html') >= 0) {
|
75 | return relativePath;
|
76 | }
|
77 |
|
78 | for (let i = 0, len = this.excludePaths.length; i < len; i++) {
|
79 | if (relativePath.indexOf(this.excludePaths[i]) === 0) {
|
80 | return null;
|
81 | }
|
82 | }
|
83 |
|
84 | if (relativePath.indexOf('.js') >= 0) {
|
85 | return relativePath;
|
86 | }
|
87 |
|
88 | return null;
|
89 | }
|
90 |
|
91 | processString(code, relativePath) {
|
92 | if (relativePath.indexOf('.js') >= 0) {
|
93 | return this._processJsFile(code, relativePath);
|
94 | }
|
95 |
|
96 | return this._processIndexFile(code, relativePath);
|
97 | }
|
98 |
|
99 | _processIndexFile(code, relativePath) {
|
100 |
|
101 | const cheerioQuery = cheerio.load(code);
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | const scriptElements = cheerioQuery('body > script');
|
107 | const scriptsToLoad = [];
|
108 | const inlineScripts = [];
|
109 | scriptElements.each(function () {
|
110 | if (cheerioQuery(this).attr('src')) {
|
111 | scriptsToLoad.push(`"${cheerioQuery(this).attr('src')}"`);
|
112 | } else {
|
113 | inlineScripts.push(cheerioQuery(this).html());
|
114 | }
|
115 | });
|
116 |
|
117 | this.indexHtmlCaches[relativePath].scriptsToLoad = scriptsToLoad;
|
118 |
|
119 |
|
120 | this.indexHtmlCaches[relativePath].afterLoadingCode = undefined;
|
121 | if (inlineScripts.length > 0) {
|
122 | this.indexHtmlCaches[relativePath].afterLoadingCode = beautify_js(replaceRequireAndDefine(inlineScripts.join('\n\n')), {
|
123 | indent_size: 2,
|
124 | max_preserve_newlines: 1
|
125 | });
|
126 | scriptsToLoad.push(`"${this.rootURL}${this.indexHtmlCaches[relativePath].afterLoadingScript}"`);
|
127 | }
|
128 |
|
129 |
|
130 | scriptElements.remove();
|
131 |
|
132 |
|
133 | return beautify_html(cheerioQuery.html(), {
|
134 | indent_size: 2,
|
135 | max_preserve_newlines: 0
|
136 | });
|
137 | }
|
138 |
|
139 | _processJsFile(code, relativePath) {
|
140 |
|
141 | const externalAmdModulesForFile = new Set();
|
142 | const modifiedSource = replaceRequireAndDefine(code, this.amdPackages, externalAmdModulesForFile);
|
143 |
|
144 |
|
145 | if (externalAmdModulesForFile.size === 0) {
|
146 |
|
147 | this.externalAmdModulesCache.delete(relativePath);
|
148 | } else {
|
149 |
|
150 | this.externalAmdModulesCache.set(relativePath, externalAmdModulesForFile);
|
151 | }
|
152 |
|
153 | return modifiedSource;
|
154 | }
|
155 |
|
156 | _buildModuleInfos() {
|
157 |
|
158 |
|
159 | const objs = [];
|
160 | const names = [];
|
161 | const adoptables = [];
|
162 | let index = 0;
|
163 | this.externalAmdModules.forEach((externalAmdModule) => {
|
164 | objs.push(`mod${index}`);
|
165 | names.push(`'${externalAmdModule}'`);
|
166 | adoptables.push(`{name:'${externalAmdModule}',obj:mod${index}}`);
|
167 | index++;
|
168 | });
|
169 |
|
170 | return {
|
171 | names: names.join(','),
|
172 | objects: objs.join(','),
|
173 | adoptables: adoptables.join(',')
|
174 | };
|
175 | }
|
176 |
|
177 | async build() {
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | this.externalAmdModules.clear();
|
183 |
|
184 | const result = await super.build();
|
185 |
|
186 |
|
187 | this.externalAmdModulesCache.forEach(externalAmdModules => {
|
188 | externalAmdModules.forEach(externalAmdModule => {
|
189 | this.externalAmdModules.add(externalAmdModule);
|
190 | });
|
191 | });
|
192 |
|
193 |
|
194 | const moduleInfos = this._buildModuleInfos();
|
195 | indexFiles.forEach(indexFile => {
|
196 |
|
197 | const indexPath = path.join(this.outputPath, indexFile);
|
198 | if (!fs.existsSync(indexPath)) {
|
199 |
|
200 | return;
|
201 | }
|
202 |
|
203 |
|
204 | const cheerioQuery = cheerio.load(fs.readFileSync(indexPath));
|
205 | const amdScripts = [ `<script src="${this.loader}" data-amd=true></script>` ];
|
206 | const scripts = this.indexHtmlCaches[indexFile].scriptsToLoad.join(',');
|
207 | const loadingScript = beautify_js(amdLoadingTemplate(_.assign(moduleInfos, { scripts })), {
|
208 | indent_size: 2,
|
209 | max_preserve_newlines: 1
|
210 | });
|
211 |
|
212 | if (this.inline) {
|
213 |
|
214 | amdScripts.push(`<script>${loadingScript}</script>`);
|
215 | } else {
|
216 |
|
217 | amdScripts.push(`<script src="${this.rootURL}${this.indexHtmlCaches[indexFile].loadingScript}" data-amd-loading=true></script>`);
|
218 | fs.writeFileSync(path.join(this.outputPath, this.indexHtmlCaches[indexFile].loadingScript), loadingScript);
|
219 | }
|
220 |
|
221 |
|
222 | if (this.indexHtmlCaches[indexFile].afterLoadingCode) {
|
223 | fs.writeFileSync(path.join(this.outputPath, this.indexHtmlCaches[indexFile].afterLoadingScript), this.indexHtmlCaches[indexFile].afterLoadingCode);
|
224 | }
|
225 |
|
226 | cheerioQuery('body').prepend(amdScripts.join('\n'));
|
227 | const html = beautify_html(cheerioQuery.html(), {
|
228 | indentSize: 2,
|
229 | max_preserve_newlines: 0
|
230 | });
|
231 | fs.writeFileSync(indexPath, html);
|
232 | });
|
233 |
|
234 | return result;
|
235 | }
|
236 | }
|