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 |
|
44 |
|
45 |
|
46 | this.externalAmdModules = new Set();
|
47 | this.externalAmdModulesCache = new Map();
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | this.indexHtmlCaches = {
|
54 | 'index.html': {
|
55 | scriptsToLoad: [],
|
56 | loadingScript: this.rootURL + this.loadingFilePath + '/amd-loading.js',
|
57 | afterLoadingScript: this.rootURL + this.loadingFilePath + '/after-amd-loading.js'
|
58 | },
|
59 | 'tests/index.html': {
|
60 | scriptsToLoad: [],
|
61 | loadingScript: this.rootURL + this.loadingFilePath + '/amd-loading-tests.js',
|
62 | afterLoadingScript: this.rootURL + this.loadingFilePath + '/after-amd-loading-tests.js'
|
63 | }
|
64 | };
|
65 | }
|
66 |
|
67 | getDestFilePath(relativePath) {
|
68 | relativePath = super.getDestFilePath(relativePath);
|
69 | if (!relativePath) {
|
70 | return null;
|
71 | }
|
72 |
|
73 | if (relativePath.indexOf('index.html') >= 0) {
|
74 | return relativePath;
|
75 | }
|
76 |
|
77 | for (let i = 0, len = this.excludePaths.length; i < len; i++) {
|
78 | if (relativePath.indexOf(this.excludePaths[i]) === 0) {
|
79 | return null;
|
80 | }
|
81 | }
|
82 |
|
83 | if (relativePath.indexOf('.js') >= 0) {
|
84 | return relativePath;
|
85 | }
|
86 |
|
87 | return null;
|
88 | }
|
89 |
|
90 | processString(code, relativePath) {
|
91 | if (relativePath.indexOf('.js') >= 0) {
|
92 | return this._processJsFile(code, relativePath);
|
93 | }
|
94 |
|
95 | return this._processIndexFile(code, relativePath);
|
96 | }
|
97 |
|
98 | _processIndexFile(code, relativePath) {
|
99 |
|
100 | const cheerioQuery = cheerio.load(code);
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | const scriptElements = cheerioQuery('body > script');
|
106 | const scriptsToLoad = [];
|
107 | const inlineScripts = [];
|
108 | scriptElements.each(function () {
|
109 | if (cheerioQuery(this).attr('src')) {
|
110 | scriptsToLoad.push(`"${cheerioQuery(this).attr('src')}"`);
|
111 | } else {
|
112 | inlineScripts.push(cheerioQuery(this).html());
|
113 | }
|
114 | });
|
115 |
|
116 | this.indexHtmlCaches[relativePath].scriptsToLoad = scriptsToLoad;
|
117 |
|
118 |
|
119 | this.indexHtmlCaches[relativePath].afterLoadingCode = undefined;
|
120 | if (inlineScripts.length > 0) {
|
121 | this.indexHtmlCaches[relativePath].afterLoadingCode = beautify_js(replaceRequireAndDefine(inlineScripts.join('\n\n')), {
|
122 | indent_size: 2,
|
123 | max_preserve_newlines: 1
|
124 | });
|
125 | scriptsToLoad.push(`"${this.indexHtmlCaches[relativePath].afterLoadingScript}"`);
|
126 | }
|
127 |
|
128 |
|
129 | scriptElements.remove();
|
130 | const amdScripts = [
|
131 | `<script src="${this.loader}" data-amd=true></script>`,
|
132 | `<script src="${this.indexHtmlCaches[relativePath].loadingScript}" data-amd-loading=true></script>`
|
133 | ];
|
134 | cheerioQuery('body').prepend(amdScripts.join('\n'));
|
135 |
|
136 |
|
137 | return beautify_html(cheerioQuery.html(), {
|
138 | indent_size: 2,
|
139 | max_preserve_newlines: 0
|
140 | });
|
141 | }
|
142 |
|
143 | _processJsFile(code, relativePath) {
|
144 |
|
145 | const externalAmdModulesForFile = new Set();
|
146 | const modifiedSource = replaceRequireAndDefine(code, this.amdPackages, externalAmdModulesForFile);
|
147 |
|
148 |
|
149 | if (externalAmdModulesForFile.size === 0) {
|
150 |
|
151 | this.externalAmdModulesCache.delete(relativePath);
|
152 | } else {
|
153 |
|
154 | this.externalAmdModulesCache.set(relativePath, externalAmdModulesForFile);
|
155 | }
|
156 |
|
157 | return modifiedSource;
|
158 | }
|
159 |
|
160 | _buildModuleInfos() {
|
161 |
|
162 |
|
163 | const objs = [];
|
164 | const names = [];
|
165 | const adoptables = [];
|
166 | let index = 0;
|
167 | this.externalAmdModules.forEach((externalAmdModule) => {
|
168 | objs.push(`mod${index}`);
|
169 | names.push(`'${externalAmdModule}'`);
|
170 | adoptables.push(`{name:'${externalAmdModule}',obj:mod${index}}`);
|
171 | index++;
|
172 | });
|
173 |
|
174 | return {
|
175 | names: names.join(','),
|
176 | objects: objs.join(','),
|
177 | adoptables: adoptables.join(',')
|
178 | };
|
179 | }
|
180 |
|
181 | async build() {
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | this.externalAmdModules.clear();
|
187 |
|
188 | const result = await super.build();
|
189 |
|
190 |
|
191 | this.externalAmdModulesCache.forEach(externalAmdModules => {
|
192 | externalAmdModules.forEach(externalAmdModule => {
|
193 | this.externalAmdModules.add(externalAmdModule);
|
194 | });
|
195 | });
|
196 |
|
197 |
|
198 | const moduleInfos = this._buildModuleInfos();
|
199 | indexFiles.forEach(indexFile => {
|
200 |
|
201 |
|
202 | const scripts = this.indexHtmlCaches[indexFile].scriptsToLoad.join(',');
|
203 | const loadingScript = amdLoadingTemplate(_.assign(moduleInfos, { scripts }));
|
204 | fs.writeFileSync(path.join(this.outputPath, this.indexHtmlCaches[indexFile].loadingScript), beautify_js(loadingScript, {
|
205 | indent_size: 2,
|
206 | max_preserve_newlines: 1
|
207 | }));
|
208 |
|
209 |
|
210 | if (this.indexHtmlCaches[indexFile].afterLoadingCode) {
|
211 | fs.writeFileSync(path.join(this.outputPath, this.indexHtmlCaches[indexFile].afterLoadingScript), this.indexHtmlCaches[indexFile].afterLoadingCode);
|
212 | }
|
213 | });
|
214 |
|
215 | return result;
|
216 | }
|
217 | } |
\ | No newline at end of file |