var gulp = require("gulp");
var gulpSync = require("gulp-sync")(gulp);
var path = require("path");


var wonderPackage = require("wonder-package");

var bundleDTS = wonderPackage.bundleDTS;
var compileTs = wonderPackage.compileTs;
var package = wonderPackage.package;
var format = wonderPackage.format;


var config = require("./build/gulp_task/common/config");




var tsFilePaths = config.tsFilePaths;
var tsFileDir = config.tsFileDir;
var distPath = config.distPath;
var tsconfigFile = config.tsconfigFile;
var indexFileDir = config.indexFileDir;
var glslFilePaths = config.glslFilePaths;




var createShaderChunk = require("./build/gulp_task/createInnerFile/ShaderChunk/create").createShaderChunk;


require("./build/gulp_task/clean/clean");
require("./build/gulp_task/test/test");


var generateIndex = require("wonder-tool-generate_es2015_index").generate;
var ts = require("typescript");

gulp.task("generateIndex", function(done) {
    var rootDir = path.join(process.cwd(), "src"),
        destDir = "./src/";

    generateIndex("/", rootDir, ["*.ts", "**/*.ts"], destDir, {
        target: ts.ScriptTarget.ES5,
        module: ts.ModuleKind.System
    }, {
        exclude: ["System.ts", ".d.ts", "Utils.ts"]
    });

    done();
});


gulp.task("compileTsES2015", function(done) {
    compileTs.compileTsES2015(path.join(process.cwd(), tsconfigFile), {
        sourceDir: tsFileDir,
        cwd:"/",
        targetDir:path.join(distPath, "./es2015/")
    }, done);
});

gulp.task("compileTsCommonjs", function(done) {
    compileTs.compileTsCommonjs(path.join(process.cwd(), tsconfigFile), {
        sourceDir: tsFileDir,
        cwd:"/",
        targetDir:path.join(distPath, "./commonjs/")
    }, done);
});

gulp.task("generateDTS", function(done) {
    var indexDTSPath = path.join(indexFileDir, "index.d.ts");

    bundleDTS.generateES2015DTS(indexDTSPath, "wonder.js/dist/es2015", path.join(distPath, "wd.es2015.d.ts"));
    bundleDTS.generateCommonjsDTS(indexDTSPath, "wonder.js/dist/commonjs", path.join(distPath, "wd.commonjs.d.ts"));

    done();
});

gulp.task("rollup", function(done) {
    package.rollup(path.join(process.cwd(), "./rollup.config.js"), done);
});

gulp.task("formatTs", function(done) {
    format.formatTs(tsFilePaths, "/", done);
});


gulp.task("createShaderChunk", function() {
    return createShaderChunk(glslFilePaths);
});



gulp.task("build", gulpSync.sync(["clean", "createShaderChunk", "generateIndex", "compileTsES2015", "compileTsCommonjs", "generateDTS", "rollup", "formatTs"]));



gulp.task("watch", function(){
    var totalPaths = tsFilePaths.concat(glslFilePaths);

    // gulp.watch(totalPaths, gulpSync.sync(["createShaderChunk", "compileTsES2015", "rollup"]));
    // gulp.watch(totalPaths, gulpSync.sync(["compileTsES2015", "rollup"]));
    gulp.watch(totalPaths, gulpSync.sync(["generateIndex", "compileTsES2015"]));
});

gulp.task("watchForTest", function(){
    var totalPaths = tsFilePaths.concat(glslFilePaths);

    gulp.watch(totalPaths, gulpSync.sync(["generateIndex", "compileTsES2015", "rollup"]));
});

@@ -2071,124 +2071,8 @@
 );%0A%0A
-gulp.task(%22rollup%22, function(done) %7B%0A    package.rollup(path.join(process.cwd(), %22./rollup.config.js%22), done);%0A%7D);%0A%0A
 gulp
@@ -2102,24 +2102,24 @@
 ion(done) %7B%0A
+
     format.f
@@ -2249,24 +2249,368 @@
 hs);%0A%7D);%0A%0A%0A%0A
+gulp.task(%22rollupRenderWorker%22, function(done) %7B%0A    package.rollup(path.join(process.cwd(), %22./rollup.config.js%22), done);%0A%7D);%0A%0Agulp.task(%22rollupNoWorker%22, function(done) %7B%0A    package.rollup(path.join(process.cwd(), %22./rollup.config.renderWorker.js%22), done);%0A%7D);%0A%0Agulp.task(%22rollup%22, gulpSync.sync(%5B%22rollupNoWorker%22, %22rollupRenderWorker%22%5D));%0A%0A
 gulp.task(%22b
@@ -3201,24 +3201,24 @@
 ilePaths);%0A%0A
-
     gulp.wat
@@ -3292,16 +3292,438 @@
 rollup%22%5D));%0A%7D);%0A
+%0Agulp.task(%22watchForTestNoWorker%22, function()%7B%0A    var totalPaths = tsFilePaths.concat(glslFilePaths);%0A%0A    gulp.watch(totalPaths, gulpSync.sync(%5B%22generateIndex%22, %22compileTsES2015%22, %22rollupNoWorker%22%5D));%0A%7D);%0A%0Agulp.task(%22watchForTestRenderWorker%22, function()%7B%0A    var totalPaths = tsFilePaths.concat(glslFilePaths);%0A%0A    gulp.watch(totalPaths, gulpSync.sync(%5B%22generateIndex%22, %22compileTsES2015%22, %22rollupRenderWorker%22%5D));%0A%7D);%0A

add "rollupRenderWorker", "rollupNoWorker" gulp tasks