|
| 1 | +var gulp = require('gulp'); |
| 2 | +var rename = require('gulp-rename'); |
| 3 | +var watch = require('gulp-watch'); |
| 4 | +var shell = require('gulp-shell'); |
| 5 | +var mergeStreams = require('event-stream').merge; |
| 6 | +var connect = require('gulp-connect'); |
| 7 | +var clean = require('gulp-rimraf'); |
| 8 | +var runSequence = require('run-sequence'); |
| 9 | +var glob = require('glob'); |
| 10 | +var ejs = require('gulp-ejs'); |
| 11 | +var path = require('path'); |
| 12 | + |
| 13 | +// import js2dart and traceur build tasks |
| 14 | +require('./tools/js2dart/gulpfile').install(gulp); |
| 15 | + |
| 16 | +var traceurJsOptions = { |
| 17 | + annotations: true, // parse annotations |
| 18 | + types: true, // parse types |
| 19 | + script: false, // parse as a module |
| 20 | + modules: 'register', |
| 21 | + typeAssertionModule: 'assert', |
| 22 | + typeAssertions: true, |
| 23 | + moduleName: true, |
| 24 | + reload: true |
| 25 | +}; |
| 26 | + |
| 27 | +var traceur = require('./tools/js2dart/gulp-traceur'); |
| 28 | +var js2dart = require('./tools/js2dart/gulp-js2dart'); |
| 29 | + |
| 30 | +// ----------------------- |
| 31 | +// modules |
| 32 | +var sourceTypeConfigs = { |
| 33 | + dart: { |
| 34 | + compiler: function() { |
| 35 | + return js2dart({replace: true}); |
| 36 | + }, |
| 37 | + transpileSrc: ['modules/*/src/**/*.es6d'], |
| 38 | + htmlSrc: ['modules/*/src/**/*.html'], |
| 39 | + copySrc: ['modules/*/src/**/*.dart'], |
| 40 | + outputDir: 'build/dart', |
| 41 | + outputExt: 'dart', |
| 42 | + mimeType: 'application/dart' |
| 43 | + }, |
| 44 | + js: { |
| 45 | + compiler: function() { |
| 46 | + return traceur(traceurJsOptions); |
| 47 | + }, |
| 48 | + transpileSrc: ['modules/*/src/**/*.es*', 'tools/rtts-assert/src/assert.js'], |
| 49 | + htmlSrc: ['modules/*/src/**/*.html'], |
| 50 | + copySrc: ['tools/traceur/bin/traceur-runtime.js'], |
| 51 | + outputDir: 'build/js', |
| 52 | + outputExt: 'js' |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +gulp.task('modules/clean', function() { |
| 57 | + return gulp.src('build', {read: false}) |
| 58 | + .pipe(clean()); |
| 59 | +}); |
| 60 | + |
| 61 | +function removeSrc(path) { |
| 62 | + path.dirname = path.dirname.replace('/src', ''); |
| 63 | +} |
| 64 | + |
| 65 | +function createModuleTask(sourceTypeConfig, isWatch) { |
| 66 | + var start = isWatch ? watch : gulp.src.bind(gulp); |
| 67 | + return function(done) { |
| 68 | + var transpile = start(sourceTypeConfig.transpileSrc) |
| 69 | + .pipe(rename({extname: '.'+sourceTypeConfig.outputExt})) |
| 70 | + .pipe(rename(removeSrc)) |
| 71 | + .pipe(sourceTypeConfig.compiler()) |
| 72 | + .pipe(gulp.dest(sourceTypeConfig.outputDir)); |
| 73 | + var copy = start(sourceTypeConfig.copySrc) |
| 74 | + .pipe(rename(removeSrc)) |
| 75 | + .pipe(gulp.dest(sourceTypeConfig.outputDir)); |
| 76 | + // TODO: provide the list of files to the template |
| 77 | + var html = start(sourceTypeConfig.htmlSrc) |
| 78 | + .pipe(rename(removeSrc)) |
| 79 | + .pipe(ejs({ |
| 80 | + type: sourceTypeConfig.outputExt |
| 81 | + })) |
| 82 | + .pipe(gulp.dest(sourceTypeConfig.outputDir)); |
| 83 | + |
| 84 | + return mergeStreams(transpile, copy, html); |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +gulp.task('modules/build.dart', createModuleTask(sourceTypeConfigs.dart, false)); |
| 89 | +gulp.task('modules/watch.dart', createModuleTask(sourceTypeConfigs.dart, true)); |
| 90 | +gulp.task('modules/build.js', createModuleTask(sourceTypeConfigs.js, false)); |
| 91 | +gulp.task('modules/watch.js', createModuleTask(sourceTypeConfigs.js, true)); |
| 92 | + |
| 93 | +// ------------------ |
| 94 | +// WEB SERVER |
| 95 | +gulp.task('serve', connect.server({ |
| 96 | + root: [__dirname+'/build'], |
| 97 | + port: 8000, |
| 98 | + livereload: false, |
| 99 | + open: false, |
| 100 | + middleware: function() { |
| 101 | + return [function(req, resp, next){ |
| 102 | + if (req.url.match(/\.dart$/)) { |
| 103 | + resp.setHeader("Content-Type", "application/dart"); |
| 104 | + console.log('now', req.url); |
| 105 | + } |
| 106 | + next(); |
| 107 | + }]; |
| 108 | + } |
| 109 | +})); |
| 110 | + |
| 111 | +// -------------- |
| 112 | +// general targets |
| 113 | + |
| 114 | +gulp.task('clean', function(done) { |
| 115 | + return runSequence(['traceur/clean', 'modules/clean'], done); |
| 116 | +}); |
| 117 | + |
| 118 | +gulp.task('build', function(done) { |
| 119 | + // By using runSequence here we are decoupling the cleaning from the rest of the build tasks |
| 120 | + // Otherwise, we have to add clean as a dependency on every task to ensure that it completes |
| 121 | + // before they begin. |
| 122 | + runSequence( |
| 123 | + 'js2dart/build', |
| 124 | + ['modules/build.dart', 'modules/build.js'], |
| 125 | + done |
| 126 | + ); |
| 127 | +}); |
| 128 | + |
| 129 | +gulp.task('watch', function(done) { |
| 130 | + // By using runSequence here we are decoupling the cleaning from the rest of the build tasks |
| 131 | + // Otherwise, we have to add clean as a dependency on every task to ensure that it completes |
| 132 | + // before they begin. |
| 133 | + runSequence( |
| 134 | + 'build', |
| 135 | + ['js2dart/watch', 'modules/watch.dart', 'modules/watch.js'], |
| 136 | + done |
| 137 | + ); |
| 138 | +}); |
0 commit comments