| 
									
										
										
										
											2020-09-22 15:42:33 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  | import {PluginObj, transformSync} from '@babel/core'; | 
					
						
							| 
									
										
										
										
											2020-11-16 18:12:31 +01:00
										 |  |  | import * as ts from 'typescript'; | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 22:31:44 +01:00
										 |  |  | import {needsLinking} from '../../../linker'; | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  | import {createEs2015LinkerPlugin} from '../../../linker/babel'; | 
					
						
							| 
									
										
										
										
											2020-12-21 10:06:23 +00:00
										 |  |  | import {MockFileSystemNative} from '../../../src/ngtsc/file_system/testing'; | 
					
						
							|  |  |  | import {MockLogger} from '../../../src/ngtsc/logging/testing'; | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  | import {compileFiles, CompileFn, setCompileFn} from '../mock_compile'; | 
					
						
							| 
									
										
										
										
											2020-09-22 15:42:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * A function to compile the given code in two steps: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * - first compile the code in partial mode | 
					
						
							|  |  |  |  * - then compile the partially compiled code using the linker | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This should produce the same output as the full AOT compilation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const linkedCompile: CompileFn = (data, angularFiles, options) => { | 
					
						
							| 
									
										
										
										
											2020-11-16 18:12:31 +01:00
										 |  |  |   if (options !== undefined && options.target !== undefined && | 
					
						
							|  |  |  |       options.target < ts.ScriptTarget.ES2015) { | 
					
						
							|  |  |  |     pending('ES5 is not supported in the partial compilation tests'); | 
					
						
							|  |  |  |     throw new Error('ES5 is not supported in the partial compilation tests'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  |   const compiledFiles = compileFiles(data, angularFiles, {...options, compilationMode: 'partial'}); | 
					
						
							| 
									
										
										
										
											2020-12-21 10:06:23 +00:00
										 |  |  |   const fileSystem = new MockFileSystemNative(); | 
					
						
							|  |  |  |   const logger = new MockLogger(); | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  |   const linkerPlugin = createEs2015LinkerPlugin({ | 
					
						
							| 
									
										
										
										
											2020-12-21 10:06:23 +00:00
										 |  |  |     fileSystem, | 
					
						
							|  |  |  |     logger, | 
					
						
							| 
									
										
										
										
											2020-11-16 18:16:07 +01:00
										 |  |  |     // enableI18nLegacyMessageIdFormat defaults to false in `compileFiles`.
 | 
					
						
							|  |  |  |     enableI18nLegacyMessageIdFormat: false, | 
					
						
							|  |  |  |     ...options, | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 22:31:44 +01:00
										 |  |  |   const source = | 
					
						
							|  |  |  |       compiledFiles | 
					
						
							|  |  |  |           .map(file => applyLinker({path: file.fileName, source: file.source}, linkerPlugin)) | 
					
						
							|  |  |  |           .join('\n'); | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return {source}; | 
					
						
							| 
									
										
										
										
											2020-09-22 15:42:33 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Runs the provided code through the Babel linker plugin, if the file has the .js extension. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-12-15 22:31:44 +01:00
										 |  |  |  * @param file The absolute file path and its source to be transformed using the linker. | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  |  * @param linkerPlugin The linker plugin to apply. | 
					
						
							|  |  |  |  * @returns The file's source content, which has been transformed using the linker if necessary. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-12-15 22:31:44 +01:00
										 |  |  | function applyLinker(file: {path: string; source: string}, linkerPlugin: PluginObj): string { | 
					
						
							|  |  |  |   if (!file.path.endsWith('.js') || !needsLinking(file.path, file.source)) { | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  |     return file.source; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   const result = transformSync(file.source, { | 
					
						
							| 
									
										
										
										
											2020-12-15 22:31:44 +01:00
										 |  |  |     filename: file.path, | 
					
						
							| 
									
										
										
										
											2020-10-30 23:04:09 +01:00
										 |  |  |     plugins: [linkerPlugin], | 
					
						
							|  |  |  |     parserOpts: {sourceType: 'unambiguous'}, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   if (result === null) { | 
					
						
							|  |  |  |     throw fail('Babel transform did not have output'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (result.code == null) { | 
					
						
							|  |  |  |     throw fail('Babel transform result does not have any code'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return result.code; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-22 15:42:33 +01:00
										 |  |  | // Update the function that will do the compiling with this specialised version that
 | 
					
						
							|  |  |  | // runs the prelink and postlink parts of AOT compilation, to check it produces the
 | 
					
						
							|  |  |  | // same result as a normal full AOT compile.
 | 
					
						
							|  |  |  | setCompileFn(linkedCompile); |