| 
									
										
										
										
											2015-04-10 16:45:24 -07:00
										 |  |  | /// <reference path="../../typings/fs-extra/fs-extra.d.ts" />
 | 
					
						
							|  |  |  | /// <reference path="../../typings/node/node.d.ts" />
 | 
					
						
							| 
									
										
										
										
											2015-04-24 11:00:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-10 16:45:24 -07:00
										 |  |  | import fs = require('fs'); | 
					
						
							|  |  |  | import fse = require('fs-extra'); | 
					
						
							|  |  |  | import path = require('path'); | 
					
						
							| 
									
										
										
										
											2015-05-04 08:19:25 -07:00
										 |  |  | import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from '../diffing-broccoli-plugin'; | 
					
						
							| 
									
										
										
										
											2015-04-06 13:30:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-26 01:04:28 -07:00
										 |  |  | let traceur = require('../../../../tools/transpiler'); | 
					
						
							| 
									
										
										
										
											2015-04-24 11:00:15 -07:00
										 |  |  | let xtend = require('xtend'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 08:19:25 -07:00
										 |  |  | class DiffingTraceurCompiler implements DiffingBroccoliPlugin { | 
					
						
							|  |  |  |   constructor(public inputPath: string, public cachePath: string, public options) {} | 
					
						
							| 
									
										
										
										
											2015-04-24 11:00:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 19:24:10 -04:00
										 |  |  |   static includeExtensions = ['.js', '.es6', '.cjs']; | 
					
						
							| 
									
										
										
										
											2015-04-24 11:00:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 08:19:25 -07:00
										 |  |  |   rebuild(treeDiff: DiffResult) { | 
					
						
							|  |  |  |     treeDiff.changedPaths.forEach((changedFilePath) => { | 
					
						
							| 
									
										
										
										
											2015-05-06 19:24:10 -04:00
										 |  |  |       var traceurOpts = xtend({filename: changedFilePath}, this.options.traceurOptions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var fsOpts = {encoding: 'utf-8'}; | 
					
						
							|  |  |  |       var absoluteInputFilePath = path.join(this.inputPath, changedFilePath); | 
					
						
							|  |  |  |       var sourcecode = fs.readFileSync(absoluteInputFilePath, fsOpts); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var result = traceur.compile(traceurOpts, changedFilePath, sourcecode); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // TODO: we should fix the sourceMappingURL written by Traceur instead of overriding
 | 
					
						
							|  |  |  |       // (but we might switch to typescript first)
 | 
					
						
							|  |  |  |       var mapFilepath = changedFilePath.replace(/\.\w+$/, '') + this.options.destSourceMapExtension; | 
					
						
							|  |  |  |       result.js = result.js + '\n//# sourceMappingURL=./' + path.basename(mapFilepath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var destFilepath = changedFilePath.replace(/\.\w+$/, this.options.destExtension); | 
					
						
							|  |  |  |       var destFile = path.join(this.cachePath, destFilepath); | 
					
						
							|  |  |  |       fse.mkdirsSync(path.dirname(destFile)); | 
					
						
							|  |  |  |       fs.writeFileSync(destFile, result.js, fsOpts); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var destMap = path.join(this.cachePath, mapFilepath); | 
					
						
							|  |  |  |       result.sourceMap.file = destFilepath; | 
					
						
							|  |  |  |       fs.writeFileSync(destMap, JSON.stringify(result.sourceMap), fsOpts); | 
					
						
							| 
									
										
										
										
											2015-04-24 11:00:15 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 08:19:25 -07:00
										 |  |  |     treeDiff.removedPaths.forEach((removedFilePath) => { | 
					
						
							| 
									
										
										
										
											2015-05-06 19:24:10 -04:00
										 |  |  |       var destFilepath = removedFilePath.replace(/\.\w+$/, this.options.destExtension); | 
					
						
							|  |  |  |       var absoluteOuputFilePath = path.join(this.cachePath, destFilepath); | 
					
						
							|  |  |  |       fs.unlinkSync(absoluteOuputFilePath); | 
					
						
							| 
									
										
										
										
											2015-04-24 11:00:15 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-04-06 13:30:35 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-05-04 08:19:25 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-04-06 13:30:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 08:19:25 -07:00
										 |  |  | let transpileWithTraceur = wrapDiffingPlugin(DiffingTraceurCompiler); | 
					
						
							|  |  |  | let TRACEUR_RUNTIME_PATH = traceur.RUNTIME_PATH; | 
					
						
							| 
									
										
										
										
											2015-04-24 11:00:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-04 08:19:25 -07:00
										 |  |  | export {transpileWithTraceur as default, TRACEUR_RUNTIME_PATH}; |