build - refactor

This commit is contained in:
Tobias Bosch 2014-09-18 14:56:38 -07:00
parent 6a3abf2366
commit afa7616464
15 changed files with 284 additions and 0 deletions

4
.gitignore vendored
View File

@ -2,6 +2,10 @@
build/
packages/
.buildlog
node_modules
packages
.DS_STORE
# Or the files created by dart2js.
*.dart.js

9
.gitmodules vendored Normal file
View File

@ -0,0 +1,9 @@
[submodule "tools/js2dart"]
path = tools/js2dart
url = git@github.com:angular/js2dart.git
[submodule "tools/rtts-assert"]
path = tools/rtts-assert
url = git@github.com:angular/assert.git
[submodule "tools/traceur"]
path = tools/traceur
url = git@github.com:google/traceur-compiler.git

9
TODO.md Normal file
View File

@ -0,0 +1,9 @@
## Setup
- use package.json's out of the individual projects
- auto start Chromium when start serving
- auto refresh Chromium when s/t changed
- transform index.html:
-> use a template with flags for dart/js and a variable with all files
-> remove all <script> tags, just leave main.dart!
-> auto create it as well!

138
gulpfile.js Normal file
View File

@ -0,0 +1,138 @@
var gulp = require('gulp');
var rename = require('gulp-rename');
var watch = require('gulp-watch');
var shell = require('gulp-shell');
var mergeStreams = require('event-stream').merge;
var connect = require('gulp-connect');
var clean = require('gulp-rimraf');
var runSequence = require('run-sequence');
var glob = require('glob');
var ejs = require('gulp-ejs');
var path = require('path');
// import js2dart and traceur build tasks
require('./tools/js2dart/gulpfile').install(gulp);
var traceurJsOptions = {
annotations: true, // parse annotations
types: true, // parse types
script: false, // parse as a module
modules: 'register',
typeAssertionModule: 'assert',
typeAssertions: true,
moduleName: true,
reload: true
};
var traceur = require('./tools/js2dart/gulp-traceur');
var js2dart = require('./tools/js2dart/gulp-js2dart');
// -----------------------
// modules
var sourceTypeConfigs = {
dart: {
compiler: function() {
return js2dart({replace: true});
},
transpileSrc: ['modules/*/src/**/*.es6d'],
htmlSrc: ['modules/*/src/**/*.html'],
copySrc: ['modules/*/src/**/*.dart'],
outputDir: 'build/dart',
outputExt: 'dart',
mimeType: 'application/dart'
},
js: {
compiler: function() {
return traceur(traceurJsOptions);
},
transpileSrc: ['modules/*/src/**/*.es*', 'tools/rtts-assert/src/assert.js'],
htmlSrc: ['modules/*/src/**/*.html'],
copySrc: ['tools/traceur/bin/traceur-runtime.js'],
outputDir: 'build/js',
outputExt: 'js'
}
};
gulp.task('modules/clean', function() {
return gulp.src('build', {read: false})
.pipe(clean());
});
function removeSrc(path) {
path.dirname = path.dirname.replace('/src', '');
}
function createModuleTask(sourceTypeConfig, isWatch) {
var start = isWatch ? watch : gulp.src.bind(gulp);
return function(done) {
var transpile = start(sourceTypeConfig.transpileSrc)
.pipe(rename({extname: '.'+sourceTypeConfig.outputExt}))
.pipe(rename(removeSrc))
.pipe(sourceTypeConfig.compiler())
.pipe(gulp.dest(sourceTypeConfig.outputDir));
var copy = start(sourceTypeConfig.copySrc)
.pipe(rename(removeSrc))
.pipe(gulp.dest(sourceTypeConfig.outputDir));
// TODO: provide the list of files to the template
var html = start(sourceTypeConfig.htmlSrc)
.pipe(rename(removeSrc))
.pipe(ejs({
type: sourceTypeConfig.outputExt
}))
.pipe(gulp.dest(sourceTypeConfig.outputDir));
return mergeStreams(transpile, copy, html);
};
}
gulp.task('modules/build.dart', createModuleTask(sourceTypeConfigs.dart, false));
gulp.task('modules/watch.dart', createModuleTask(sourceTypeConfigs.dart, true));
gulp.task('modules/build.js', createModuleTask(sourceTypeConfigs.js, false));
gulp.task('modules/watch.js', createModuleTask(sourceTypeConfigs.js, true));
// ------------------
// WEB SERVER
gulp.task('serve', connect.server({
root: [__dirname+'/build'],
port: 8000,
livereload: false,
open: false,
middleware: function() {
return [function(req, resp, next){
if (req.url.match(/\.dart$/)) {
resp.setHeader("Content-Type", "application/dart");
console.log('now', req.url);
}
next();
}];
}
}));
// --------------
// general targets
gulp.task('clean', function(done) {
return runSequence(['traceur/clean', 'modules/clean'], done);
});
gulp.task('build', function(done) {
// By using runSequence here we are decoupling the cleaning from the rest of the build tasks
// Otherwise, we have to add clean as a dependency on every task to ensure that it completes
// before they begin.
runSequence(
'js2dart/build',
['modules/build.dart', 'modules/build.js'],
done
);
});
gulp.task('watch', function(done) {
// By using runSequence here we are decoupling the cleaning from the rest of the build tasks
// Otherwise, we have to add clean as a dependency on every task to ensure that it completes
// before they begin.
runSequence(
'build',
['js2dart/watch', 'modules/watch.dart', 'modules/watch.js'],
done
);
});

View File

@ -0,0 +1,17 @@
import {DOM} from './dom';
export class App {
constructor() {
this.input = null;
this.list = null;
}
run() {
this.input = DOM.query('input');
this.list = DOM.query('ul');
DOM.on(this.input, 'change', (evt) => this.add(evt));
}
add(evt) {
var html = DOM.getInnerHTML(this.list);
html += '<li>'+this.input.value+'</li>';
DOM.setInnerHTML(this.list, html);
}
}

View File

@ -0,0 +1,18 @@
library dom;
import 'dart:html';
class DOM {
static query(selector) {
return document.query(selector);
}
static on(element, event, callback) {
element.addEventListener(event, callback);
}
static getInnerHTML(el) {
return el.innerHtml;
}
static setInnerHTML(el, value) {
el.innerHtml = value;
}
}

View File

@ -0,0 +1,14 @@
export class DOM {
static query(selector) {
return document.querySelector(selector);
}
static on(el, evt, listener) {
el.addEventListener(evt, listener, false);
}
static getInnerHTML(el) {
return el.innerHTML;
}
static setInnerHTML(el, value) {
el.innerHTML = value;
}
}

View File

@ -0,0 +1,20 @@
<!doctype html>
<html>
<body>
<input type="test">
<ul></ul>
<% if(type === 'dart') { %>
<script src="main.dart" type="application/dart"></script>
<% } else { %>
<script src="../../traceur-runtime.js" type="text/javascript"></script>
<script src="../../assert.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
<script src="dom.js" type="text/javascript"></script>
<script>
new (System.get("examples/todo/app").App)().run();
</script>
<% } %>
</body>
</html>

View File

@ -0,0 +1,5 @@
import 'app.dart' show App;
main() {
new App().run();
}

25
package.json Normal file
View File

@ -0,0 +1,25 @@
{
"name": "angular",
"version": "0.0.0",
"description": "Angular",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"postinstall": "./postinstall.sh"
},
"author": "Tobias Bosch <tbosch@google.com>",
"license": "MIT",
"dependencies": {
"gulp": "^3.8.8",
"gulp-rename": "^1.2.0",
"gulp-shell": "^0.2.9",
"gulp-watch": "^1.0.3",
"q": "^1.0.1",
"through2": "^0.6.1",
"event-stream": "^3.1.5",
"gulp-connect": "~1.0.5",
"gulp-rimraf": "^0.1.0",
"run-sequence": "^0.3.6",
"glob": "^4.0.6",
"gulp-ejs": "^0.3.1"
}
}

12
postinstall.sh Executable file
View File

@ -0,0 +1,12 @@
#! /bin/sh
rm node_modules/traceur
ln -s ../tools/traceur node_modules/traceur
rm node_modules/js2dart
ln -s ../tools/j2dart node_modules/js2dart
cd tools/traceur
npm install
cd ../js2dart
npm install

10
pubspec.yaml Normal file
View File

@ -0,0 +1,10 @@
name: angular
version: 0.0.0
authors:
- Vojta Jina <vojta.jina@gmail.com>
description: Compile JavaScript to Dart so that you can compile it back to JavaScript and run.
environment:
sdk: '>=1.4.0'
dependencies:
dev_dependencies:
unittest: '>=0.10.1 <0.12.0'

1
tools/js2dart Submodule

@ -0,0 +1 @@
Subproject commit 4e1ebfdefda333354bbda71e172daa5db4808616

1
tools/rtts-assert Submodule

@ -0,0 +1 @@
Subproject commit 6caafd2561ece8cbfdabbe7357b50ce9192db18c

1
tools/traceur Submodule

@ -0,0 +1 @@
Subproject commit 573ac4091b79167532ef929993e82fda96325718