build(broccoli): add DiffingBroccoliPlugin and refactor existing plugins to use it
tree-differ: - export both TreeDiffer and DiffResult interface diffing-broccoli-plugin: - factory class for wrapping DiffingBroccoliPlugins and turning them into BroccoliTrees broccoli-dest-copy: - refactor into DiffingBroccoliPlugin broccoli-traceur: - refactor into DiffingBroccoliPlugin
This commit is contained in:
@ -1,66 +1,38 @@
|
||||
/// <reference path="../broccoli.d.ts" />
|
||||
/// <reference path="../broccoli-writer.d.ts" />
|
||||
/// <reference path="../../typings/fs-extra/fs-extra.d.ts" />
|
||||
/// <reference path="../../typings/node/node.d.ts" />
|
||||
|
||||
import fs = require('fs');
|
||||
import fse = require('fs-extra');
|
||||
import path = require('path');
|
||||
import TreeDiffer = require('../tree-differ');
|
||||
import Writer = require('broccoli-writer');
|
||||
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from '../diffing-broccoli-plugin';
|
||||
|
||||
let traceur = require('../../../../tools/transpiler');
|
||||
let symlinkOrCopy = require('symlink-or-copy');
|
||||
let xtend = require('xtend');
|
||||
|
||||
|
||||
export = traceurCompiler;
|
||||
|
||||
function traceurCompiler(inputTree, destExtension, destSourceMapExtension, options) {
|
||||
return new TraceurCompiler(inputTree, destExtension, destSourceMapExtension, options);
|
||||
}
|
||||
|
||||
traceurCompiler['RUNTIME_PATH'] = traceur.RUNTIME_PATH;
|
||||
class DiffingTraceurCompiler implements DiffingBroccoliPlugin {
|
||||
constructor(public inputPath: string, public cachePath: string, public options) {}
|
||||
|
||||
|
||||
class TraceurCompiler implements BroccoliTree {
|
||||
treeDiffer: TreeDiffer;
|
||||
initialized = false;
|
||||
|
||||
// props monkey-patched by broccoli builder:
|
||||
inputPath = null;
|
||||
cachePath = null;
|
||||
outputPath = null;
|
||||
|
||||
|
||||
constructor(public inputTree: BroccoliTree, private destExtension: string,
|
||||
private destSourceMapExtension: string, private options: {[key: string]: any}) {}
|
||||
|
||||
|
||||
rebuild() {
|
||||
let firstRun = !this.initialized;
|
||||
this.init();
|
||||
|
||||
let diffResult = this.treeDiffer.diffTree();
|
||||
diffResult.log(!firstRun);
|
||||
|
||||
diffResult.changedPaths.forEach((changedFilePath) => {
|
||||
rebuild(treeDiff: DiffResult) {
|
||||
treeDiff.changedPaths.forEach((changedFilePath) => {
|
||||
var extension = path.extname(changedFilePath).toLowerCase();
|
||||
if (extension === '.js' || extension === '.es6' || extension === '.cjs') {
|
||||
var options = xtend({filename: changedFilePath}, this.options);
|
||||
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(options, changedFilePath, sourcecode);
|
||||
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.destSourceMapExtension;
|
||||
var mapFilepath =
|
||||
changedFilePath.replace(/\.\w+$/, '') + this.options.destSourceMapExtension;
|
||||
result.js = result.js + '\n//# sourceMappingURL=./' + path.basename(mapFilepath);
|
||||
|
||||
var destFilepath = changedFilePath.replace(/\.\w+$/, this.destExtension);
|
||||
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);
|
||||
@ -71,25 +43,15 @@ class TraceurCompiler implements BroccoliTree {
|
||||
}
|
||||
});
|
||||
|
||||
diffResult.removedPaths.forEach((removedFilePath) => {
|
||||
var destFilepath = removedFilePath.replace(/\.\w+$/, this.destExtension);
|
||||
treeDiff.removedPaths.forEach((removedFilePath) => {
|
||||
var destFilepath = removedFilePath.replace(/\.\w+$/, this.options.destExtension);
|
||||
var absoluteOuputFilePath = path.join(this.cachePath, destFilepath);
|
||||
fs.unlinkSync(absoluteOuputFilePath);
|
||||
});
|
||||
|
||||
// just symlink the cache and output tree
|
||||
fs.rmdirSync(this.outputPath);
|
||||
symlinkOrCopy.sync(this.cachePath, this.outputPath);
|
||||
}
|
||||
|
||||
|
||||
private init() {
|
||||
if (!this.initialized) {
|
||||
this.initialized = true;
|
||||
this.treeDiffer = new TreeDiffer(this.inputPath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cleanup() {}
|
||||
}
|
||||
|
||||
let transpileWithTraceur = wrapDiffingPlugin(DiffingTraceurCompiler);
|
||||
let TRACEUR_RUNTIME_PATH = traceur.RUNTIME_PATH;
|
||||
|
||||
export {transpileWithTraceur as default, TRACEUR_RUNTIME_PATH};
|
||||
|
Reference in New Issue
Block a user