-
Notifications
You must be signed in to change notification settings - Fork 620
/
Copy pathindex.js
472 lines (433 loc) · 15.4 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import fs from 'fs';
import path from 'path';
import ts from 'typescript';
import {
AngularHtmlCompiler
} from 'meteor/angular-html-compiler';
import {
AngularScssCompiler
} from 'meteor/angular-scss-compiler';
import {
TSBuild
} from 'meteor-typescript';
import rollup from './rollup';
import {
basePath,
ROOTED,
getMeteorPath,
isRooted,
getNoRooted
} from './file-utils';
// using: regex, capture groups, and capture group variables.
const TEMPLATE_URL_REGEX = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*([,}]))/gm;
const STYLES_URLS_REGEX = /styleUrls *:(\s*\[[^\]]*?\])/g;
const LOAD_CHILDREN_REGEX = /loadChildren[\s]*:[\s]*['|"].*#{1}.*['|"]/;
const STRING_REGEX = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;
const JS_REGEX = /\.html$/;
const HTML_REGEX = /\.html$/;
const SCSS_REGEX = /\.scss$/;
const CSS_REGEX = /\.css$/;
const TS_REGEX = /\.ts$/;
const D_TS_REGEX = /\.ts$/;
const WEB_ARCH_REGEX = /^web/;
const ngCompilerOptions = {
transitiveModules: true
};
const tcOptions = {
baseUrl: basePath,
experimentalDecorators: true,
module: 'commonjs',
target: 'es2015',
noImplicitAny: false,
moduleResolution: 'node',
emitDecoratorMetadata: true,
traceResolution: false
};
const ngcOptions = {
basePath,
genDir: basePath,
generateCodeForLibraries: true,
traceResolution: false,
skipTemplateCodegen: false,
fullTemplateTypeCheck: true,
disableTypeScriptVersionCheck: true,
enableIvy: false,
};
export class AngularTsCompiler {
constructor({
aot,
rollup
}) {
this.isAot = aot;
this.isRollup = rollup;
if (this.isAot) {
this.compiler = require('@angular/compiler');
this.compilerCli = require('@angular/compiler-cli');
}
}
addFakeDynamicLoader(source, basePath) {
let fakeLoaderCode = '';
let newSource = source.replace(LOAD_CHILDREN_REGEX,
(match, url) => {
const replaced = this.replaceStringsWithFullUrls(basePath, url, true).trim();
const fixedUrl = (replaced
.split('#')[0]) +
(this.isAot ? '.ngfactory' : '') +
replaced[0];
fakeLoaderCode += ` if(!Meteor){module.dynamicImport(${fixedUrl})} `;
return `loadChildren: ${replaced}`;
})
if (fakeLoaderCode) {
newSource = fakeLoaderCode + '\n' + newSource;
console.log(newSource);
}
return newSource;
}
replaceDynamicLoadingCode(code) {
return code.replace(LOAD_CHILDREN_REGEX,
(match, url) => {
url = url.split('\'').join('').split('"').join('').split(',').join('');
const urlArr = url.split('#');
let modulePath = urlArr[0].trim();
let moduleName = urlArr[1].trim();
if (this.isAot) {
modulePath += '.ngfactory';
moduleName += 'NgFactory';
}
return `loadChildren: () => module.dynamicImport('${modulePath}').then(allModule => allModule['${moduleName}'])`;
});
}
replaceStringsWithFullUrls(basePath, urls, firstSlash) {
return urls
.replace(STRING_REGEX,
(match, quote, url) => `'${(firstSlash ? '/' : '~/../') + path.join(basePath, url)}'`)
.replace(/\\/g, '/');
}
fixResourceUrls(source, basePath) {
const newSource = source
.replace(TEMPLATE_URL_REGEX,
(match, url) => `templateUrl:${this.replaceStringsWithFullUrls(basePath, url, false)}`)
.replace(STYLES_URLS_REGEX,
(match, urls) => `styleUrls:${this.replaceStringsWithFullUrls(basePath, urls, false)}`);
return newSource;
}
processFilesForTarget(inputFiles) {
const filesMap = new Map();
const arch = inputFiles[0].getArch();
const forWeb = WEB_ARCH_REGEX.test(arch);
const prefix = forWeb ? 'client' : 'server';
// Get app ts-files.
const tsFilePaths = [];
const fullPaths = [];
let tsConfig = {};
console.time(`[${prefix}]: Collecting TypeScript source files`)
inputFiles.forEach((inputFile, index) => {
const filePath = inputFile.getPathInPackage();
if (filePath.endsWith('.ts') &&
!filePath.includes('node_modules')) {
tsFilePaths.push(filePath);
fullPaths.push(path.join(basePath, filePath));
} else if (inputFile.getBasename() == 'tsconfig.json' && !filePath.startsWith('node_modules')) {
tsConfig = JSON.parse(inputFile.getContentsAsString());
}
filesMap.set(filePath, index);
})
console.timeEnd(`[${prefix}]: Collecting TypeScript source files`)
const defaultGet = filePath => {
filePath = getMeteorPath(filePath);
let content = null;
if (filesMap.has(filePath)) {
const inputFile = inputFiles[filesMap.get(filePath)];
content = inputFile.getContentsAsString();
}
return content;
};
const {
options
} = ts.convertCompilerOptionsFromJson(tcOptions, '');
const genOptions = Object.assign({}, options, ngcOptions);
let ngcError = null;
let ngcFilesMap = new Map();
if (this.isAot) {
ngcFilesMap = this
.generateCode(fullPaths, genOptions, defaultGet)
.await();
}
const getContent = filePath => {
let content = ngcFilesMap.get(filePath);
if (!content) {
content = defaultGet(filePath);
}
if (!content) {
filePath = isRooted(filePath) ? filePath : path.join(basePath, filePath);
if (fs.existsSync(filePath)) {
content = fs.readFileSync(filePath, 'utf8');
}
}
return content;
}
const allPaths = tsFilePaths.concat(Array.from(ngcFilesMap.keys()));
if (this.isRollup) {
tsConfig = tsConfig || {};
tsConfig.compilerOptions = tsConfig.compilerOptions || {};
tsConfig.compilerOptions.module = 'es2015';
}
const buildOptions = {
arch,
compilerOptions: tsConfig.compilerOptions
};
const tsBuild = new TSBuild(allPaths, getContent, buildOptions);
let mainCodePath;
let mainCode;
const codeMap = new Map();
let fakeLoaderCode = '';
console.time(`[${prefix}]: TypeScript Files Compilation`);
for (const filePath of allPaths) {
if (!filePath.endsWith('.d.ts') && filePath.endsWith('.ts')) {
try {
const result = tsBuild.emit(filePath, filePath);
let code = result.code;
const origTargetFilePath =
getMeteorPath(filePath)
.replace('.ngfactory', '')
.replace('.shim.ngstyle', '');
const inputFile = inputFiles[filesMap.get(origTargetFilePath)] ||
inputFiles[filesMap.get(origTargetFilePath.replace('.ts', '.d.ts'))] ||
inputFiles.find(file => {
const filePath = file.getPathInPackage();
return filePath.includes('imports');
});
this._processTsDiagnostics(result.diagnostics, inputFile);
if (this.isAot && this.hasDynamicBootstrap(code)) {
code = this.removeDynamicBootstrap(code);
}
if (this.isRollup && !filePath.includes('imports/') && !filePath.includes('node_modules/')) {
mainCodePath = this.removeTsExtension(filePath) + '.js';
mainCode = code;
continue;
}
const basePath = inputFile.getPathInPackage().replace(inputFile.getBasename(), '');
if (!this.isAot) {
code = this.fixResourceUrls(code, basePath)
}
code = code.split('require("node_modules/').join('require("');
if (this.isRollup) {
code = this.addFakeDynamicLoader(code, basePath);
} else {
code = this.replaceDynamicLoadingCode(code);
}
const inputPath = inputFile.getPathInPackage();
const outputPath = this.removeTsExtension(filePath);
if (this.isRollup) {
codeMap.set(outputPath, code);
}
if (tsConfig.compilerOptions.module == 'es2015') {
code = Babel.compile(code).code;
}
const toBeAdded = {
sourcePath: inputPath,
path: outputPath + '.js',
data: code,
hash: result.hash,
sourceMap: result.sourceMap
};
inputFile.addJavaScript(toBeAdded);
} catch (e) {
console.log(`${filePath} ignored due to some errors!`);
}
}
}
if (this.isRollup) {
const inputFile = inputFiles.find(file => {
const filePath = file.getPathInPackage();
return filePath.startsWith(prefix) &&
filePath.indexOf('imports') === -1;
});
if (inputFile) {
inputFile.addJavaScript({
path: 'system.js',
data: `System = { import: function(path) { return module.dynamicImport(path) } }`
});
}
}
console.timeEnd(`[${prefix}]: TypeScript Files Compilation`);
if (this.isRollup && !mainCodePath.includes('node_modules')) {
console.time(`[${prefix}]: Rollup`);
let namedExports = null;
const namedExportsPath = path.join(basePath, 'named-exports.json');
if (fs.existsSync(namedExportsPath)) {
try {
namedExports = JSON.parse(fs.readFileSync(namedExportsPath));
} catch (e) {
console.error(
'Error: named-exports.json does not contain valid JSON'
);
console.error(e);
}
}
const bundle = rollup(codeMap, mainCode, mainCodePath,
null, namedExports, forWeb);
if (bundle) {
// Look for a ts-file in the client or server
// folder to add generated bundle.
const inputFile = inputFiles.find(file => {
const filePath = file.getPathInPackage();
return filePath.startsWith(prefix) &&
filePath.indexOf('imports') === -1;
});
if (inputFile) {
const toBeAdded = {
sourcePath: inputFile.getPathInPackage(),
path: 'bundle.js',
data: bundle
};
inputFile.addJavaScript(toBeAdded);
}
}
console.timeEnd(`[${prefix}]: Rollup`);
}
}
_processTsDiagnostics(diagnostics, inputFile) {
diagnostics.semanticErrors.forEach(error => {
const msg = `${error.fileName} (${error.line}, ${error.column}): ${error.message}`;
console.log(msg);
//inputFile.error(error);
});
}
async generateCode(filePaths, ngcOptions, getMeteorFileContent) {
console.time('TypeScript Host Created.');
const tsHost = this.createNgcTsHost(ngcOptions, getMeteorFileContent);
console.timeEnd('TypeScript Host Created.');
console.time('TypeScript Program Created.');
const tsProgram = this.createNgcTsProgram(filePaths, ngcOptions, tsHost);
console.timeEnd('TypeScript Program Created.');
const usePathMapping = !!ngcOptions.rootDirs && ngcOptions.rootDirs.length > 0;
console.time('Angular Compiler Host Created.');
const compilerHost = this.createCompilerHost(
tsProgram, ngcOptions, tsHost, usePathMapping);
console.timeEnd('Angular Compiler Host Created.');
console.time('Angular Compiler Program Created.');
const {
compiler
} =
this.compilerCli.createProgram({
rootNames: filePaths,
options: ngcOptions,
host: compilerHost
});
console.timeEnd('Angular Compiler Program Created.');
compiler._host._loadResource = compiler._host.loadResource;
compiler._host.loadResource = filePath => {
if (SCSS_REGEX.test(filePath)) {
const content = AngularScssCompiler.getContent(getMeteorPath(filePath));
if (content) {
return content;
} else {
return AngularScssCompiler.compileFile(getMeteorPath(filePath)).css.toString('utf8');
}
} else if (HTML_REGEX.test(filePath)) {
const content = AngularHtmlCompiler.getContent(getMeteorPath(filePath));
if (content) {
return content;
}
}
return compiler._host._loadResource(filePath);
}
console.time('Filtering TypeScript source files');
const ngcFilePaths = tsProgram.getSourceFiles().map(sf => sf.fileName);
console.timeEnd('Filtering TypeScript source files');
console.time('Modules Analyzed.');
const analyzeResult = await compiler.analyzeModulesAsync(ngcFilePaths);
console.timeEnd('Modules Analyzed.');
console.time('Emitting All Impls');
const generatedModules = await compiler.emitAllImpls(analyzeResult);
console.timeEnd('Emitting All Impls');
console.time('Modules Converted to TypeScript.');
const ngcFilesMap = new Map();
for (const generatedModule of generatedModules) {
const filePath = getMeteorPath(generatedModule.genFileUrl);
ngcFilesMap.set(
filePath,
generatedModule.source ||
this.compiler.toTypeScript(
generatedModule, `
/**
* @fileoverview This file is generated by the Angular template compiler.
* Do not edit.
* @suppress {suspiciousCode,uselessCode,missingProperties,missingOverride}
*/
/* tslint:disable */
`)
);
}
console.timeEnd('Modules Converted to TypeScript.')
return ngcFilesMap;
}
createNgcTsHost(ngcOptions, getFileContent) {
const defaultHost = ts.createCompilerHost(ngcOptions, true);
const customHost = {
getSourceFile: (filePath, languageVersion, onError) => {
const content = getFileContent(filePath);
if (content) {
return ts.createSourceFile(filePath, content, languageVersion, true);
}
return defaultHost.getSourceFile(filePath, languageVersion, onError);
},
realpath: filePath => filePath,
fileExists: filePath => {
const exists = defaultHost.fileExists(filePath);
return exists || !!getFileContent(filePath);
},
directoryExists: dirName => {
const exists = defaultHost.directoryExists(dirName);
return exists || !!getFileContent(path.join(dirName, 'index.ts'));
},
trace: msg => {
throw msg;
},
};
const ngcHost = Object.assign({}, defaultHost, customHost);
return ngcHost;
}
createNgcTsProgram(filePaths, ngcOptions, ngcHost) {
const program = ts.createProgram(filePaths, ngcOptions, ngcHost);
const getSourceFile = program.getSourceFile;
program.getSourceFile = filePath => {
let sf = getSourceFile.call(this, filePath);
if (sf) return sf;
filePath = getMeteorPath(filePath);
return getSourceFile.call(this, filePath);
};
return program;
}
createCompilerHost(tsProgram, ngcOptions, compilerHostContext, usePathMapping) {
return this.compilerCli.createCompilerHost({
options: ngcOptions,
host: compilerHostContext
})
}
removeTsExtension(filePath) {
if (filePath.endsWith('.ts')) {
return filePath.slice(0, -3);
}
return filePath;
}
hasDynamicBootstrap(code) {
return code.indexOf('platform-browser-dynamic') !== -1 ||
code.indexOf('DynamicServer') !== -1 ||
code.indexOf('renderModule(') !== -1;
}
removeDynamicBootstrap(code) {
function replaceAll(str, find, replace) {
return str.split(find).join(replace)
}
code = replaceAll(code, 'platform-browser-dynamic', 'platform-browser');
code = replaceAll(code, 'platformBrowserDynamic', 'platformBrowser');
code = replaceAll(code, 'platformDynamicServer', 'platformServer');
code = replaceAll(code, 'bootstrapModule', 'bootstrapModuleFactory');
code = replaceAll(code, 'renderModule', 'renderModuleFactory');
code = replaceAll(code, 'app.module', 'app.module.ngfactory');
code = replaceAll(code, 'AppModule', 'AppModuleNgFactory');
return code;
}
}