Skip to content

Commit 3969009

Browse files
committed
build(brocolli): convert brocolli-ts2dart to use TreeDiffer
Closes angular#1720 Closes angular#1733
1 parent ecb0680 commit 3969009

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

tools/broccoli/broccoli-ts2dart.ts

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,52 @@
11
/// <reference path="./broccoli-writer.d.ts" />
22
/// <reference path="../typings/node/node.d.ts" />
33
/// <reference path="../typings/fs-extra/fs-extra.d.ts" />
4+
/// <reference path="./ts2dart.d.ts" />
45

5-
import Writer = require('broccoli-writer');
66
import fs = require('fs');
77
import fse = require('fs-extra');
88
import path = require('path');
99
import ts2dart = require('ts2dart');
10+
import {wrapDiffingPlugin, DiffingBroccoliPlugin, DiffResult} from './diffing-broccoli-plugin';
1011

11-
type Set = {
12-
[s: string]: boolean
13-
};
12+
class TSToDartTranspiler implements DiffingBroccoliPlugin {
13+
static includeExtensions = ['.js', '.ts'];
1414

15-
class TypeScriptToDartTranspiler extends Writer {
16-
constructor(private inputTree, private includePattern = /\.(js|ts)$/) { super(); }
15+
private basePath: string;
16+
private transpiler: ts2dart.Transpiler;
1717

18-
write(readTree, destDir): Promise<void> {
19-
return readTree(this.inputTree).then(dir => this.transpile(dir, destDir));
18+
constructor(public inputPath: string, public cachePath: string, public options) {
19+
options.basePath = inputPath;
20+
this.transpiler = new ts2dart.Transpiler(options);
2021
}
2122

22-
private transpile(inputDir: string, destDir: string) {
23-
var files = this.listRecursive(inputDir);
24-
var toTranspile = [];
25-
for (var f in files) {
26-
// If it's not matching, don't translate.
27-
if (!f.match(this.includePattern)) continue;
28-
var dartVariant = f.replace(this.includePattern, '.dart');
29-
// A .dart file of the same name takes precedence over transpiled code.
30-
if (files.hasOwnProperty(dartVariant)) continue;
31-
toTranspile.push(f);
32-
}
33-
var transpiler = new ts2dart.Transpiler(
34-
{generateLibraryName: true, generateSourceMap: false, basePath: inputDir});
35-
transpiler.transpile(toTranspile, destDir);
36-
}
23+
rebuild(treeDiff: DiffResult) {
24+
let toEmit = [];
25+
let getDartFilePath = (path: string) => path.replace(/((\.js)|(\.ts))$/i, '.dart');
26+
treeDiff.changedPaths.forEach((changedPath) => {
27+
let inputFilePath = path.resolve(this.inputPath, changedPath);
28+
29+
// Ignore files which don't need to be transpiled to Dart
30+
let dartInputFilePath = getDartFilePath(inputFilePath);
31+
if (fs.existsSync(dartInputFilePath)) return;
32+
33+
// Prepare to rebuild
34+
toEmit.push(path.resolve(this.inputPath, changedPath));
35+
});
3736

38-
private listRecursive(root: string, res: Set = {}): Set {
39-
var paths = fs.readdirSync(root);
40-
paths.forEach((p) => {
41-
p = path.join(root, p);
42-
var stat = fs.statSync(p);
43-
if (stat.isDirectory()) {
44-
this.listRecursive(p, res);
45-
} else {
46-
// Collect *all* files so we can check .dart files that already exist and exclude them.
47-
res[p] = true;
48-
}
37+
treeDiff.removedPaths.forEach((removedPath) => {
38+
let absolutePath = path.resolve(this.inputPath, removedPath);
39+
40+
// Ignore files which don't need to be transpiled to Dart
41+
let dartInputFilePath = getDartFilePath(absolutePath);
42+
if (fs.existsSync(dartInputFilePath)) return;
43+
44+
let dartOutputFilePath = getDartFilePath(removedPath);
45+
fs.unlinkSync(path.join(this.cachePath, dartOutputFilePath));
4946
});
50-
return res;
47+
48+
this.transpiler.transpile(toEmit, this.cachePath);
5149
}
5250
}
5351

54-
export function transpile(inputTree) {
55-
return new TypeScriptToDartTranspiler(inputTree);
56-
}
52+
export default wrapDiffingPlugin(TSToDartTranspiler);

tools/broccoli/trees/dart_tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var path = require('path');
1010
var renderLodashTemplate = require('broccoli-lodash');
1111
var replace = require('broccoli-replace');
1212
var stew = require('broccoli-stew');
13-
var ts2dart = require('../broccoli-ts2dart');
13+
import ts2dart from '../broccoli-ts2dart';
1414

1515
/**
1616
* A funnel starting at modules, including the given filters, and moving into the root.
@@ -44,7 +44,7 @@ function stripModulePrefix(relativePath: string): string {
4444
function getSourceTree() {
4545
// Transpile everything in 'modules' except for rtts_assertions.
4646
var tsInputTree = modulesFunnel(['**/*.js', '**/*.ts', '**/*.dart'], ['rtts_assert/**/*']);
47-
var transpiled = ts2dart.transpile(tsInputTree);
47+
var transpiled = ts2dart(tsInputTree, {generateLibraryName: true, generateSourceMap: false});
4848
// Native sources, dart only examples, etc.
4949
var dartSrcs = modulesFunnel(['**/*.dart', '**/*.ng_meta.json', '**/css/**']);
5050
return mergeTrees([transpiled, dartSrcs]);

0 commit comments

Comments
 (0)