Skip to content

Commit a3decad

Browse files
committed
feat(build): Use broccoli for ts2dart transpilation.
1 parent 7b790a3 commit a3decad

7 files changed

Lines changed: 121 additions & 10 deletions

File tree

Brocfile-dart.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Brocfile to transpile sources from TypeScript to Dart using ts2dart.
2+
var Funnel = require('broccoli-funnel');
3+
var stew = require('broccoli-stew');
4+
var ts2dart = require('./dist/broccoli/broccoli-ts2dart');
5+
var path = require('path');
6+
7+
// Transpile everything in 'modules'...
8+
var modulesTree = new Funnel('modules', {
9+
include: ['**/*.js', '**/*.ts', '**/*.dart'], // .dart file available means don't translate.
10+
exclude: ['rtts_assert/**/*'], // ... except for the rtts_asserts (don't apply to Dart).
11+
destDir: '/', // Remove the 'modules' prefix.
12+
});
13+
14+
// Transpile to dart.
15+
var dartTree = ts2dart.transpile(modulesTree);
16+
17+
// Move around files to match Dart's layout expectations.
18+
dartTree = stew.rename(dartTree, function(relativePath) {
19+
// If a file matches the `pattern`, insert the given `insertion` as the second path part.
20+
var replacements = [
21+
{pattern: /^benchmarks\/test\//, insertion: ''},
22+
{pattern: /^benchmarks\//, insertion: 'web'},
23+
{pattern: /^benchmarks_external\/test\//, insertion: ''},
24+
{pattern: /^benchmarks_external\//, insertion: 'web'},
25+
{pattern: /^example.?\//, insertion: 'web/'},
26+
{pattern: /^example.?\/test\//, insertion: ''},
27+
{pattern: /^[^\/]*\/test\//, insertion: ''},
28+
{pattern: /^./, insertion: 'lib'}, // catch all.
29+
];
30+
31+
for (var i = 0; i < replacements.length; i++) {
32+
var repl = replacements[i];
33+
if (relativePath.match(repl.pattern)) {
34+
var parts = relativePath.split('/');
35+
parts.splice(1, 0, repl.insertion);
36+
return path.join.apply(path, parts);
37+
}
38+
}
39+
throw new Error('Failed to match any path', relativePath);
40+
});
41+
42+
// Move the tree under the 'dart' folder.
43+
module.exports = stew.mv(dartTree, 'dart');

gulpfile.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ var runServerDartTests = require('./tools/build/run_server_dart_tests');
2929
var sourcemaps = require('gulp-sourcemaps');
3030
var transformCJSTests = require('./tools/build/transformCJSTests');
3131
var tsc = require('gulp-typescript');
32-
var ts2dart = require('gulp-ts2dart');
3332
var util = require('./tools/build/util');
3433
var bundler = require('./tools/build/bundle');
3534
var replace = require('gulp-replace');
@@ -373,11 +372,8 @@ gulp.task('build/transformCJSTests', function() {
373372
.pipe(gulp.dest(CONFIG.dest.js.cjs + '/angular2/test/'));
374373
});
375374

376-
gulp.task('build/transpile.dart', function() {
377-
return gulp.src(CONFIG.transpile.src.dart)
378-
.pipe(ts2dart.transpile())
379-
.pipe(util.insertSrcFolder(gulpPlugins, CONFIG.srcFolderInsertion.dart))
380-
.pipe(gulp.dest(CONFIG.dest.dart));
375+
gulp.task('build/transpile.dart', ['build.broccoli.tools'], function() {
376+
return broccoliBuild(require('./Brocfile-dart.js'), 'dart');
381377
});
382378

383379
// ------------
@@ -720,7 +716,8 @@ gulp.task('test.transpiler.unittest', function() {
720716
// Builds all Dart packages, but does not compile them
721717
gulp.task('build/packages.dart', function(done) {
722718
runSequence(
723-
['build/transpile.dart', 'build/html.dart', 'build/copy.dart', 'build/multicopy.dart'],
719+
'build/transpile.dart', // Creates the folder structure needed by subsequent tasks.
720+
['build/html.dart', 'build/copy.dart', 'build/multicopy.dart'],
724721
'build/format.dart',
725722
'build/pubspec.dart',
726723
done

modules/angular2/globals.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Globals are provided by lang.dart in Dart.
2+
// This file exists to prevent global.ts from being transpiled.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
"gulp-sourcemaps": "1.3.*",
7373
"gulp-template": "^3.0.0",
7474
"gulp-traceur": "0.17.*",
75-
"gulp-ts2dart": "^1.0.6",
7675
"gulp-typescript": "ivogabe/gulp-typescript#3422fbff06532ccc57368f3b4c8801de8f72ef27",
7776
"gulp-webserver": "^0.8.7",
7877
"js-yaml": "^3.2.7",
@@ -97,7 +96,7 @@
9796
"ternary-stream": "^1.2.3",
9897
"through2": "^0.6.1",
9998
"typescript": "alexeagle/TypeScript#93dbbe2a2d0b42cefd02ac949e4bc8ab6b5b5823",
100-
"ts2dart": "^0.2.0",
99+
"ts2dart": "^0.3.0",
101100
"vinyl": "^0.4.6",
102101
"walk-sync": "^0.1.3",
103102
"xtend": "^4.0.0",

tools/broccoli/broccoli-ts2dart.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// <reference path="./broccoli-writer.d.ts" />
2+
/// <reference path="../typings/node/node.d.ts" />
3+
/// <reference path="../typings/fs-extra/fs-extra.d.ts" />
4+
5+
import Writer = require('broccoli-writer');
6+
import fs = require('fs');
7+
import fse = require('fs-extra');
8+
import path = require('path');
9+
import ts2dart = require('ts2dart');
10+
11+
type Set = {
12+
[s:string]: boolean
13+
};
14+
15+
class TypeScriptToDartTranspiler extends Writer {
16+
constructor(private inputTree, private includePattern = /\.(js|ts)$/) { super(); }
17+
18+
write(readTree, destDir): Promise<void> {
19+
return readTree(this.inputTree).then(dir => this.transpile(dir, destDir));
20+
}
21+
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+
}
37+
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+
}
49+
});
50+
return res;
51+
}
52+
}
53+
54+
export function transpile(inputTree) {
55+
return new TypeScriptToDartTranspiler(inputTree);
56+
}

tools/broccoli/broccoli-writer.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
declare class Writer {
5-
write(readTree: (tree) => Promise<string>, destDir: string);
5+
write(readTree: (tree) => Promise<string>, destDir: string): Promise<any>;
66
}
77

88
export = Writer;

tools/broccoli/ts2dart.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// TODO(martinprobst): This is a hand-written declarations file. Replace with an automatically
2+
// generated one when TypeScript has a strategy to distribute TS source via npm.
3+
4+
export interface TranspilerOptions {
5+
failFast?: boolean;
6+
generateLibraryName?: boolean;
7+
generateSourceMap?: boolean;
8+
basePath?: string;
9+
}
10+
11+
export class Transpiler{
12+
constructor(options: TranspilerOptions);
13+
transpile(fileNames: string[], outdir?: string);
14+
}

0 commit comments

Comments
 (0)