gulpfile.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. var gulp = require("gulp");
  2. var uglify = require("gulp-uglify");
  3. var typescript = require("gulp-typescript");
  4. var sourcemaps = require("gulp-sourcemaps");
  5. var srcToVariable = require("gulp-content-to-variable");
  6. var appendSrcToVariable = require("./gulp-appendSrcToVariable");
  7. var addDtsExport = require("./gulp-addDtsExport");
  8. var addModuleExports = require("./gulp-addModuleExports");
  9. var addES6Exports = require("./gulp-addES6Exports");
  10. var babylonModuleExports = require("./gulp-babylonModule");
  11. var babylonES6ModuleExports = require("./gulp-es6ModuleExports");
  12. var dtsModuleSupport = require("./gulp-dtsModuleSupport");
  13. let calculateDependencies = require("./gulp-calculateDependencies");
  14. var merge2 = require("merge2");
  15. var concat = require("gulp-concat");
  16. var rename = require("gulp-rename");
  17. var cleants = require("gulp-clean-ts-extends");
  18. var changedInPlace = require("gulp-changed-in-place");
  19. var runSequence = require("run-sequence");
  20. var replace = require("gulp-replace");
  21. var uncommentShader = require("./gulp-removeShaderComments");
  22. var expect = require("gulp-expect-file");
  23. var optimisejs = require("gulp-optimize-js");
  24. var webserver = require("gulp-webserver");
  25. var path = require("path");
  26. var sass = require("gulp-sass");
  27. var webpack = require("webpack-stream");
  28. var typedoc = require("gulp-typedoc");
  29. var validateTypedoc = require("./gulp-validateTypedoc");
  30. var request = require('request');
  31. var fs = require("fs");
  32. var dtsBundle = require('dts-bundle');
  33. const through = require('through2');
  34. var karmaServer = require('karma').Server;
  35. var config = require("./config.json");
  36. var del = require("del");
  37. var debug = require("gulp-debug");
  38. var includeShadersStream;
  39. var shadersStream;
  40. var workersStream;
  41. var extendsSearchRegex = /var\s__extends[\s\S]+?\}\)\(\);/g;
  42. var decorateSearchRegex = /var\s__decorate[\s\S]+?\};/g;
  43. var referenceSearchRegex = /\/\/\/ <reference.*/g;
  44. /**
  45. * TS configurations shared in the gulp file.
  46. */
  47. var tsConfig = {
  48. noResolve: true,
  49. target: "ES5",
  50. declarationFiles: true,
  51. typescript: require("typescript"),
  52. experimentalDecorators: true,
  53. isolatedModules: false,
  54. noImplicitAny: true,
  55. noImplicitReturns: true,
  56. noImplicitThis: true,
  57. noUnusedLocals: true,
  58. strictNullChecks: true,
  59. strictFunctionTypes: true,
  60. strict: true,
  61. types: [],
  62. lib: [
  63. "dom",
  64. "es2015.promise",
  65. "es5"
  66. ]
  67. };
  68. var tsProject = typescript.createProject(tsConfig);
  69. var externalTsConfig = {
  70. noResolve: false,
  71. target: "ES5",
  72. declarationFiles: true,
  73. typescript: require("typescript"),
  74. experimentalDecorators: true,
  75. isolatedModules: false,
  76. noImplicitAny: true,
  77. noImplicitReturns: true,
  78. noImplicitThis: true,
  79. noUnusedLocals: true,
  80. strictNullChecks: true,
  81. strict: true,
  82. types: [],
  83. lib: [
  84. "dom",
  85. "es2015.promise",
  86. "es5"
  87. ]
  88. };
  89. var minimist = require("minimist");
  90. var commandLineOptions = minimist(process.argv.slice(2), {
  91. boolean: "public"
  92. });
  93. function processDependency(kind, dependency, filesToLoad, firstLevelOnly) {
  94. if (!firstLevelOnly && dependency.dependUpon) {
  95. for (var i = 0; i < dependency.dependUpon.length; i++) {
  96. var dependencyName = dependency.dependUpon[i];
  97. var parent = config.workloads[dependencyName];
  98. processDependency(kind, parent, filesToLoad);
  99. }
  100. }
  101. var content = dependency[kind];
  102. if (!content) {
  103. return;
  104. }
  105. for (var i = 0; i < content.length; i++) {
  106. var file = content[i];
  107. if (filesToLoad.indexOf(file) === -1) {
  108. filesToLoad.push(file);
  109. }
  110. }
  111. }
  112. function determineFilesToProcess(kind) {
  113. var currentConfig = config.build.currentConfig;
  114. var buildConfiguration = config.buildConfigurations[currentConfig];
  115. var filesToLoad = [];
  116. for (var index = 0; index < buildConfiguration.length; index++) {
  117. var dependencyName = buildConfiguration[index];
  118. var dependency = config.workloads[dependencyName];
  119. if (kind === "directFiles" && !dependency) {
  120. filesToLoad.push("../../dist/preview release/" + dependencyName);
  121. }
  122. else if (dependency) {
  123. processDependency(kind, dependency, filesToLoad);
  124. }
  125. }
  126. if (kind === "shaderIncludes") {
  127. for (var index = 0; index < filesToLoad.length; index++) {
  128. filesToLoad[index] = "../../src/Shaders/ShadersInclude/" + filesToLoad[index] + ".fx";
  129. }
  130. } else if (kind === "shaders") {
  131. for (var index = 0; index < filesToLoad.length; index++) {
  132. var name = filesToLoad[index];
  133. filesToLoad[index] = "../../src/Shaders/" + filesToLoad[index] + ".fx";
  134. }
  135. }
  136. return filesToLoad;
  137. }
  138. /*
  139. * Shader Management.
  140. */
  141. function shadersName(filename) {
  142. return path.basename(filename)
  143. .replace(".fragment", "Pixel")
  144. .replace(".vertex", "Vertex")
  145. .replace(".fx", "Shader");
  146. }
  147. function includeShadersName(filename) {
  148. return path.basename(filename).replace(".fx", "");
  149. }
  150. /*
  151. * Main necessary files stream Management.
  152. */
  153. gulp.task("includeShaders", function (cb) {
  154. var filesToProcess = determineFilesToProcess("shaderIncludes");
  155. includeShadersStream = gulp.src(filesToProcess).
  156. pipe(expect.real({ errorOnFailure: true }, filesToProcess)).
  157. pipe(uncommentShader()).
  158. pipe(srcToVariable({
  159. variableName: "BABYLON.Effect.IncludesShadersStore", asMap: true, namingCallback: includeShadersName
  160. }));
  161. cb();
  162. });
  163. gulp.task("shaders", ["includeShaders"], function (cb) {
  164. var filesToProcess = determineFilesToProcess("shaders");
  165. shadersStream = gulp.src(filesToProcess).
  166. pipe(expect.real({ errorOnFailure: true }, filesToProcess)).
  167. pipe(uncommentShader()).
  168. pipe(srcToVariable({
  169. variableName: "BABYLON.Effect.ShadersStore", asMap: true, namingCallback: shadersName
  170. }));
  171. cb();
  172. });
  173. gulp.task("workers", function (cb) {
  174. workersStream = config.workers.map(function (workerDef) {
  175. return gulp.src(workerDef.files).
  176. pipe(expect.real({ errorOnFailure: true }, workerDef.files)).
  177. pipe(uglify()).
  178. pipe(srcToVariable({
  179. variableName: workerDef.variable
  180. }));
  181. });
  182. cb();
  183. });
  184. /**
  185. * Build tasks to concat minify uflify optimise the BJS js in different flavor (workers...).
  186. */
  187. gulp.task("buildWorker", ["workers", "shaders"], function () {
  188. var filesToProcess = determineFilesToProcess("files");
  189. return merge2(
  190. gulp.src(filesToProcess).
  191. pipe(expect.real({ errorOnFailure: true }, filesToProcess)),
  192. shadersStream,
  193. includeShadersStream,
  194. workersStream
  195. )
  196. .pipe(concat(config.build.minWorkerFilename))
  197. .pipe(cleants())
  198. .pipe(replace(extendsSearchRegex, ""))
  199. .pipe(replace(decorateSearchRegex, ""))
  200. .pipe(addModuleExports("BABYLON", {
  201. dependencies: config.build.dependencies
  202. }))
  203. .pipe(uglify())
  204. .pipe(optimisejs())
  205. .pipe(gulp.dest(config.build.outputDirectory));
  206. });
  207. gulp.task("build", ["shaders"], function () {
  208. var filesToProcess = determineFilesToProcess("files");
  209. var directFilesToProcess = determineFilesToProcess("directFiles");
  210. let mergedStreams = merge2(
  211. gulp.src(filesToProcess).
  212. pipe(expect.real({ errorOnFailure: true }, filesToProcess)),
  213. shadersStream,
  214. includeShadersStream,
  215. gulp.src(directFilesToProcess)
  216. )
  217. return merge2(
  218. mergedStreams
  219. .pipe(concat(config.build.filename))
  220. .pipe(cleants())
  221. .pipe(replace(extendsSearchRegex, ""))
  222. .pipe(replace(decorateSearchRegex, ""))
  223. .pipe(addModuleExports("BABYLON", {
  224. dependencies: config.build.dependencies
  225. }))
  226. .pipe(gulp.dest(config.build.outputDirectory))
  227. .pipe(rename(config.build.minFilename))
  228. .pipe(uglify())
  229. .pipe(optimisejs())
  230. .pipe(gulp.dest(config.build.outputDirectory)),
  231. mergedStreams
  232. .pipe(concat("es6.js"))
  233. .pipe(cleants())
  234. .pipe(replace(extendsSearchRegex, ""))
  235. .pipe(replace(decorateSearchRegex, ""))
  236. .pipe(addES6Exports("BABYLON"))
  237. .pipe(gulp.dest(config.build.outputDirectory))
  238. );
  239. });
  240. /*
  241. * Compiles all typescript files and creating a js and a declaration file.
  242. */
  243. gulp.task("typescript-compile", function () {
  244. var tsResult = gulp.src(config.typescript)
  245. .pipe(sourcemaps.init())
  246. .pipe(tsProject());
  247. //If this gulp task is running on travis, file the build!
  248. if (process.env.TRAVIS) {
  249. tsResult.once("error", function () {
  250. tsResult.once("finish", function () {
  251. console.log("Typescript compile failed");
  252. process.exit(1);
  253. });
  254. });
  255. }
  256. return merge2([
  257. tsResult.dts
  258. .pipe(concat(config.build.declarationFilename))
  259. .pipe(addDtsExport("BABYLON", "babylonjs"))
  260. .pipe(gulp.dest(config.build.outputDirectory)),
  261. tsResult.js
  262. .pipe(sourcemaps.write("./",
  263. {
  264. includeContent: false,
  265. sourceRoot: (filePath) => {
  266. return "";
  267. }
  268. }))
  269. .pipe(gulp.dest(config.build.srcOutputDirectory))
  270. ])
  271. });
  272. /**
  273. * Helper methods to build external library (mat, post processes, ...).
  274. */
  275. var buildExternalLibraries = function (settings) {
  276. var tasks = settings.libraries.map(function (library) {
  277. return buildExternalLibrary(library, settings, false);
  278. });
  279. let mergedTasks = merge2(tasks);
  280. if (settings.build.buildAsModule) {
  281. mergedTasks.on("end", function () {
  282. //generate js file list
  283. let files = settings.libraries.filter(function (lib) {
  284. return !lib.doNotIncludeInBundle;
  285. }).map(function (lib) {
  286. return config.build.outputDirectory + settings.build.distOutputDirectory + lib.output;
  287. });
  288. var outputDirectory = config.build.outputDirectory + settings.build.distOutputDirectory;
  289. let srcTask = gulp.src(files)
  290. .pipe(concat(settings.build.outputFilename + ".js"))
  291. .pipe(replace(extendsSearchRegex, ""))
  292. .pipe(replace(decorateSearchRegex, ""))
  293. .pipe(replace(referenceSearchRegex, ""))
  294. .pipe(addModuleExports(settings.build.moduleDeclaration, { subModule: true, extendsRoot: settings.build.extendsRoot }))
  295. .pipe(gulp.dest(outputDirectory))
  296. .pipe(cleants())
  297. .pipe(rename({ extname: ".min.js" }))
  298. .pipe(uglify())
  299. .pipe(optimisejs())
  300. .pipe(gulp.dest(outputDirectory));
  301. let dtsFiles = files.map(function (filename) {
  302. return filename.replace(".js", ".d.ts");
  303. });
  304. let dtsModuleTask = gulp.src(dtsFiles)
  305. .pipe(concat(settings.build.outputFilename + ".module.d.ts"))
  306. .pipe(replace(referenceSearchRegex, ""))
  307. .pipe(addDtsExport(settings.build.moduleDeclaration, settings.build.moduleName, true, settings.build.extendsRoot, settings.build.extraTypesDependencies))
  308. .pipe(gulp.dest(outputDirectory));
  309. let dtsTask = gulp.src(dtsFiles)
  310. .pipe(concat(settings.build.outputFilename + ".d.ts"))
  311. .pipe(replace(referenceSearchRegex, ""))
  312. .pipe(gulp.dest(outputDirectory));
  313. return merge2([srcTask, dtsTask, dtsModuleTask]);
  314. });
  315. }
  316. return mergedTasks;
  317. }
  318. var buildExternalLibrary = function (library, settings, watch) {
  319. var tsProcess = gulp.src(library.files, { base: settings.build.srcOutputDirectory })
  320. .pipe(sourcemaps.init())
  321. .pipe(typescript(externalTsConfig));
  322. var includeShader = gulp.src(library.shadersIncludeFiles || [], { base: settings.build.srcOutputDirectory })
  323. .pipe(uncommentShader())
  324. .pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, library.output + ".include.fx"))
  325. .pipe(gulp.dest(settings.build.srcOutputDirectory));
  326. var shader = gulp.src(library.shaderFiles || [], { base: settings.build.srcOutputDirectory })
  327. .pipe(uncommentShader())
  328. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, library.output + ".fx"))
  329. .pipe(gulp.dest(settings.build.srcOutputDirectory));
  330. var dev = tsProcess.js
  331. .pipe(sourcemaps.write("./", {
  332. includeContent: false,
  333. sourceRoot: (filePath) => {
  334. return "";
  335. }
  336. })).pipe(gulp.dest(settings.build.srcOutputDirectory));
  337. var outputDirectory = config.build.outputDirectory + settings.build.distOutputDirectory;
  338. var css = gulp.src(library.sassFiles || [])
  339. .pipe(sass().on("error", sass.logError))
  340. .pipe(concat(library.output.replace(".js", ".css")))
  341. .pipe(gulp.dest(outputDirectory));
  342. if (watch) {
  343. return merge2([shader, includeShader, dev, css]);
  344. }
  345. else {
  346. /*if (library.bundle) {
  347. // Don't remove extends and decorate functions
  348. var code = merge2([tsProcess.js, shader, includeShader])
  349. .pipe(concat(library.output));
  350. if (library.buildAsModule) {
  351. code = code.pipe(addModuleExports(library.moduleDeclaration, true))
  352. }
  353. code.pipe(gulp.dest(outputDirectory))
  354. .pipe(cleants())
  355. .pipe(rename({ extname: ".min.js" }))
  356. .pipe(uglify())
  357. .pipe(optimisejs())
  358. .pipe(gulp.dest(outputDirectory));
  359. } else {*/
  360. var code = merge2([tsProcess.js, shader, includeShader])
  361. .pipe(concat(library.output))
  362. if (library.buildAsModule) {
  363. code = code.pipe(replace(extendsSearchRegex, ""))
  364. .pipe(replace(decorateSearchRegex, ""))
  365. .pipe(addModuleExports(library.moduleDeclaration, { subModule: true, extendsRoot: library.extendsRoot }))
  366. }
  367. code = code.pipe(gulp.dest(outputDirectory))
  368. .pipe(cleants())
  369. .pipe(rename({ extname: ".min.js" }))
  370. .pipe(uglify())
  371. .pipe(optimisejs())
  372. .pipe(gulp.dest(outputDirectory));
  373. /*}*/
  374. var dts = tsProcess.dts
  375. .pipe(concat(library.output))
  376. .pipe(replace(referenceSearchRegex, ""))
  377. .pipe(rename({ extname: ".d.ts" }))
  378. .pipe(gulp.dest(outputDirectory));
  379. var waitAll;
  380. if (library.buildAsModule) {
  381. var dts2 = tsProcess.dts
  382. .pipe(concat(library.output))
  383. .pipe(replace(referenceSearchRegex, ""))
  384. .pipe(addDtsExport(library.moduleDeclaration, library.moduleName, true, library.extendsRoot, config.build.extraTypesDependencies))
  385. .pipe(rename({ extname: ".module.d.ts" }))
  386. .pipe(gulp.dest(outputDirectory));
  387. waitAll = merge2([dev, code, css, dts, dts2]);
  388. } else {
  389. waitAll = merge2([dev, code, css, dts]);
  390. }
  391. if (library.webpack) {
  392. let sequence = [waitAll];
  393. let wpBuild = webpack(require(library.webpack));
  394. if (settings.build.outputs) {
  395. //shoud dtsBundle create the declaration?
  396. if (settings.build.dtsBundle) {
  397. let event = wpBuild
  398. .pipe(through.obj(function (file, enc, cb) {
  399. // only declaration files
  400. const isdts = /\.d\.ts$/.test(file.path);
  401. if (isdts) this.push(file);
  402. cb();
  403. }))
  404. .pipe(gulp.dest('.'));
  405. // dts-bundle does NOT support (gulp) streams, so files have to be saved and reloaded,
  406. // until I fix it
  407. event.on("end", function () {
  408. // create the file
  409. dtsBundle.bundle(settings.build.dtsBundle);
  410. // prepend the needed reference
  411. fs.readFile(settings.build.dtsBundle.out, function (err, data) {
  412. if (err) throw err;
  413. data = settings.build.dtsBundle.prependText + data.toString();
  414. fs.writeFile(settings.build.dtsBundle.out, data);
  415. });
  416. });
  417. }
  418. let build = wpBuild
  419. .pipe(through.obj(function (file, enc, cb) {
  420. // only pipe js files
  421. const isJs = /\.js$/.test(file.path);
  422. if (isJs) this.push(file);
  423. cb();
  424. }))
  425. .pipe(addModuleExports(library.moduleDeclaration, { subModule: false, extendsRoot: false, externalUsingBabylon: true, noBabylonInit: library.babylonIncluded }));
  426. let unminifiedOutpus = [];
  427. let minifiedOutputs = [];
  428. settings.build.outputs.forEach(out => {
  429. if (out.minified) {
  430. out.destination.forEach(dest => {
  431. minifiedOutputs.push(dest);
  432. });
  433. } else {
  434. out.destination.forEach(dest => {
  435. unminifiedOutpus.push(dest);
  436. });
  437. }
  438. });
  439. function processDestination(dest) {
  440. var outputDirectory = config.build.outputDirectory + dest.outputDirectory;
  441. build = build
  442. .pipe(rename(dest.filename.replace(".js", library.noBundleInName ? '.js' : ".bundle.js")))
  443. .pipe(gulp.dest(outputDirectory));
  444. if (library.babylonIncluded && dest.addBabylonDeclaration) {
  445. // include the babylon declaration
  446. sequence.unshift(gulp.src(config.build.outputDirectory + '/' + config.build.declarationFilename)
  447. .pipe(gulp.dest(outputDirectory)))
  448. }
  449. }
  450. unminifiedOutpus.forEach(dest => {
  451. processDestination(dest);
  452. });
  453. if (minifiedOutputs.length) {
  454. build = build
  455. .pipe(uglify())
  456. .pipe(optimisejs())
  457. }
  458. minifiedOutputs.forEach(dest => {
  459. processDestination(dest);
  460. });
  461. sequence.push(build);
  462. } else {
  463. sequence.push(
  464. wpBuild
  465. .pipe(rename(library.output.replace(".js", library.noBundleInName ? '.js' : ".bundle.js")))
  466. .pipe(addModuleExports(library.moduleDeclaration, { subModule: false, extendsRoot: library.extendsRoot, externalUsingBabylon: true }))
  467. .pipe(uglify())
  468. .pipe(optimisejs())
  469. .pipe(gulp.dest(outputDirectory))
  470. )
  471. }
  472. return merge2(sequence);
  473. }
  474. else {
  475. return waitAll;
  476. }
  477. }
  478. }
  479. /**
  480. * The default task, concat and min the main BJS files.
  481. */
  482. gulp.task("default", function (cb) {
  483. runSequence("typescript-all", "intellisense", "typedoc-all", "tests-unit", "tests-validation-virtualscreen", "tests-validation-browserstack", cb);
  484. });
  485. gulp.task("mainBuild", function (cb) {
  486. runSequence("buildWorker", "build", cb);
  487. });
  488. /**
  489. * Build the releasable files.
  490. */
  491. gulp.task("typescript", function (cb) {
  492. runSequence("typescript-compile", "mainBuild", cb);
  493. });
  494. /**
  495. * Dynamic module creation.
  496. */
  497. config.modules.map(function (module) {
  498. gulp.task(module, function () {
  499. return buildExternalLibraries(config[module]);
  500. });
  501. });
  502. gulp.task("typescript-libraries", config.modules, function () {
  503. });
  504. /**
  505. * Custom build with full path file control; used by profile.html
  506. */
  507. gulp.task("build-custom", function (cb) {
  508. runSequence("typescript-compile", "build", cb);
  509. });
  510. /**
  511. * Do it all.
  512. */
  513. gulp.task("typescript-all", function (cb) {
  514. runSequence("typescript", "typescript-libraries", cb);
  515. });
  516. /**
  517. * Watch ts files from typescript .
  518. */
  519. gulp.task("srcTscWatch", function () {
  520. // Reuse The TSC CLI from gulp to enable -w.
  521. process.argv[2] = "-w";
  522. process.argv[3] = "-p";
  523. process.argv[4] = "../../src/tsconfig.json";
  524. require("./node_modules/typescript/lib/tsc.js");
  525. });
  526. /**
  527. * Watch ts files and fire repective tasks.
  528. */
  529. gulp.task("watch", ["srcTscWatch"], function () {
  530. var interval = 1000;
  531. var tasks = [];
  532. config.modules.map(function (module) {
  533. config[module].libraries.map(function (library) {
  534. tasks.push(gulp.watch(library.files, { interval: interval }, function () {
  535. console.log(library.output);
  536. return buildExternalLibrary(library, config[module], true)
  537. .pipe(debug());
  538. }));
  539. tasks.push(gulp.watch(library.shaderFiles, { interval: interval }, function () {
  540. console.log(library.output);
  541. return buildExternalLibrary(library, config[module], true)
  542. .pipe(debug())
  543. }));
  544. tasks.push(gulp.watch(library.sassFiles, { interval: interval }, function () {
  545. console.log(library.output);
  546. return buildExternalLibrary(library, config[module], true)
  547. .pipe(debug())
  548. }));
  549. });
  550. });
  551. return tasks;
  552. });
  553. gulp.task("intellisense", function () {
  554. gulp.src(config.build.intellisenseSources)
  555. .pipe(concat(config.build.intellisenseFile))
  556. .pipe(replace(/^\s+_.*?;/gm, ""))
  557. .pipe(replace(/^\s+_[\S\s]*?}/gm, ""))
  558. .pipe(replace(/^\s*readonly _/gm, "protected readonly _"))
  559. .pipe(replace(/^\s*static _/gm, "private static _"))
  560. .pipe(replace(/^\s*abstract _/gm, ""))
  561. .pipe(gulp.dest(config.build.playgroundDirectory));
  562. });
  563. /**
  564. * Embedded local dev env management.
  565. */
  566. gulp.task("deployLocalDev", function () {
  567. gulp.src("../../localDev/template/**.*")
  568. .pipe(gulp.dest("../../localDev/src/"));
  569. });
  570. /**
  571. * Embedded webserver for test convenience.
  572. */
  573. gulp.task("webserver", function () {
  574. var options = {
  575. port: 1338,
  576. livereload: false,
  577. };
  578. if (commandLineOptions.public) {
  579. options.host = "0.0.0.0";
  580. }
  581. gulp.src("../../.").pipe(webserver(options));
  582. });
  583. /**
  584. * Combine Webserver and Watch as long as vscode does not handle multi tasks.
  585. */
  586. gulp.task("run", ["watch", "webserver"], function () {
  587. });
  588. /**
  589. * Cleans map and js files from the src folder.
  590. */
  591. gulp.task("clean-JS-MAP", function () {
  592. return del([
  593. "../../src/**/*.js.map", "../../src/**/*.js"
  594. ], { force: true });
  595. });
  596. // this is needed for the modules for the declaration files.
  597. gulp.task("modules-compile", function () {
  598. var tsResult = gulp.src(config.typescript)
  599. .pipe(sourcemaps.init())
  600. .pipe(tsProject());
  601. // If this gulp task is running on travis
  602. if (process.env.TRAVIS) {
  603. tsResult.once("error", function () {
  604. tsResult.once("finish", function () {
  605. console.log("Typescript compile failed");
  606. process.exit(1);
  607. });
  608. });
  609. }
  610. return merge2([
  611. tsResult.dts
  612. .pipe(gulp.dest(config.build.srcOutputDirectory)),
  613. tsResult.js
  614. .pipe(sourcemaps.write("./",
  615. {
  616. includeContent: false,
  617. sourceRoot: (filePath) => {
  618. return "";
  619. }
  620. }))
  621. .pipe(gulp.dest(config.build.srcOutputDirectory))
  622. ]);
  623. });
  624. // this holds the declared objects in each module
  625. let declared = {}
  626. let perFile = {};
  627. let dependencyTree = {};
  628. gulp.task('prepare-for-modules', /*["modules-compile"],*/ function () {
  629. let tasks = [];
  630. Object.keys(config.workloads).forEach((moduleName) => {
  631. let dtsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".d.ts"))
  632. let dtsTask = gulp.src(dtsFiles)
  633. .pipe(dtsModuleSupport(moduleName, false, declared, perFile));
  634. tasks.push(dtsTask);
  635. });
  636. // now calculate internal dependencies in the .ts files!
  637. /*Object.keys(config.workloads).forEach((moduleName) => {
  638. let tsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".ts"))
  639. let depTask = gulp.src(tsFiles)
  640. .pipe(calculateDependencies(moduleName, perFile, dependencyTree));
  641. tasks.push(depTask);
  642. });*/
  643. return merge2(tasks);
  644. });
  645. gulp.task('prepare-dependency-tree', ["prepare-for-modules"], function () {
  646. let tasks = [];
  647. // now calculate internal dependencies in the .ts files!
  648. Object.keys(config.workloads).forEach((moduleName) => {
  649. let tsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".ts"))
  650. let depTask = gulp.src(tsFiles)
  651. .pipe(calculateDependencies(moduleName, perFile, declared, dependencyTree));
  652. tasks.push(depTask);
  653. });
  654. return merge2(tasks);
  655. });
  656. // generate the modules directory, along with commonjs modules and es6 modules
  657. // Note - the generated modules are UNMINIFIED! The user will choose whether they want to minify or not.
  658. gulp.task("modules", ["prepare-dependency-tree"], function () {
  659. let tasks = [];
  660. Object.keys(config.workloads)
  661. .forEach((moduleName) => {
  662. let shadersFiles = [];
  663. processDependency("shaders", config.workloads[moduleName], shadersFiles, true);
  664. for (var index = 0; index < shadersFiles.length; index++) {
  665. shadersFiles[index] = "../../src/Shaders/" + shadersFiles[index] + ".fx";
  666. }
  667. let shaderIncludeFiles = [];
  668. processDependency("shaderIncludes", config.workloads[moduleName], shaderIncludeFiles, true);
  669. for (var index = 0; index < shaderIncludeFiles.length; index++) {
  670. shaderIncludeFiles[index] = "../../src/Shaders/ShadersInclude/" + shaderIncludeFiles[index] + ".fx";
  671. }
  672. let commonJsTask = merge2([
  673. gulp.src(config.workloads[moduleName].files)
  674. .pipe(replace(extendsSearchRegex, ""))
  675. .pipe(replace(decorateSearchRegex, ""))
  676. .pipe(replace(referenceSearchRegex, ""))
  677. .pipe(replace(/var BABYLON;\n/g, ""))
  678. .pipe(babylonModuleExports(moduleName, dependencyTree, false, perFile, shadersFiles.length, shaderIncludeFiles.length))
  679. .pipe(rename(function (path) {
  680. path.basename = path.basename.split(".").pop()
  681. path.extname = ".js"
  682. })),
  683. gulp.src(shadersFiles)
  684. .pipe(expect.real({ errorOnFailure: true }, shadersFiles))
  685. .pipe(uncommentShader())
  686. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", "commonjs"))
  687. .pipe(rename("shaders.js")),
  688. gulp.src(shaderIncludeFiles)
  689. .pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles))
  690. .pipe(uncommentShader())
  691. .pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", "commonjs"))
  692. .pipe(rename("shaderIncludes.js")),
  693. gulp.src(config.workloads[moduleName].files)
  694. .pipe(concat('index.js'))
  695. .pipe(babylonModuleExports(moduleName, dependencyTree, true, perFile))
  696. ]).pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'))
  697. let es6Tasks = merge2([
  698. gulp.src(config.workloads[moduleName].files)
  699. .pipe(replace(extendsSearchRegex, ""))
  700. .pipe(replace(decorateSearchRegex, ""))
  701. .pipe(replace(referenceSearchRegex, ""))
  702. .pipe(replace(/var BABYLON;\n/g, ""))
  703. .pipe(babylonES6ModuleExports(moduleName, dependencyTree, false, perFile, shadersFiles.length, shaderIncludeFiles.length))
  704. .pipe(rename(function (path) {
  705. path.basename = path.basename.split(".").pop()
  706. path.extname = ".js"
  707. })),
  708. gulp.src(shadersFiles)
  709. .pipe(expect.real({ errorOnFailure: true }, shadersFiles))
  710. .pipe(uncommentShader())
  711. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/es6/' + moduleName + ".fx", "es6"))
  712. .pipe(rename("shaders.js")),
  713. gulp.src(shaderIncludeFiles)
  714. .pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles))
  715. .pipe(uncommentShader())
  716. .pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/es6/' + moduleName + ".include.fx", "es6"))
  717. .pipe(rename("shaderIncludes.js")),
  718. gulp.src(config.workloads[moduleName].files)
  719. .pipe(concat('index.js'))
  720. .pipe(babylonES6ModuleExports(moduleName, dependencyTree, true, perFile))
  721. ]).pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/es6/'))
  722. //commonjs js generation task
  723. /*let jsTask = merge2([
  724. gulp.src(config.workloads[moduleName].files),
  725. gulp.src(shadersFiles).
  726. //pipe(expect.real({ errorOnFailure: true }, shadersFiles)).
  727. pipe(uncommentShader()).
  728. pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", true)),
  729. gulp.src(shaderIncludeFiles).
  730. //pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles)).
  731. pipe(uncommentShader()).
  732. pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", true))
  733. ]).pipe(concat('index.js'))
  734. .pipe(replace(extendsSearchRegex, ""))
  735. .pipe(replace(decorateSearchRegex, ""))
  736. .pipe(replace(referenceSearchRegex, ""))
  737. .pipe(babylonModuleExports(moduleName, config.workloads[moduleName].dependUpon))
  738. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));*/
  739. // es6 modules generation task
  740. /*let es6Task = merge2([
  741. gulp.src(config.workloads[moduleName].files),
  742. gulp.src(shadersFiles).
  743. //pipe(expect.real({ errorOnFailure: true }, shadersFiles)).
  744. pipe(uncommentShader()).
  745. pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", true)),
  746. gulp.src(shaderIncludeFiles).
  747. //pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles)).
  748. pipe(uncommentShader()).
  749. pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", true))
  750. ]).pipe(concat('es6.js'))
  751. .pipe(replace(extendsSearchRegex, ""))
  752. .pipe(replace(decorateSearchRegex, ""))
  753. .pipe(replace(referenceSearchRegex, ""))
  754. .pipe(replace(/var BABYLON;/g, ""))
  755. .pipe(babylonES6ModuleExports(moduleName, config.workloads[moduleName].dependUpon))
  756. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));
  757. // dts genration task
  758. let dtsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".d.ts"))
  759. let dtsTask = gulp.src(dtsFiles)
  760. .pipe(concat("index.d.ts"))
  761. .pipe(replace(/declare module BABYLON {/g, `declare module 'babylonjs/${moduleName}' {`))
  762. .pipe(replace(/\ninterface /g, `\nexport interface `))
  763. .pipe(dtsModuleSupport(moduleName, true, declared, perFile, dependencyTree))
  764. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));
  765. */
  766. tasks.push(commonJsTask, es6Tasks);
  767. });
  768. // run da tasks man!
  769. return merge2(tasks);
  770. })
  771. /**
  772. * Generate the TypeDoc JSON output in order to create code metadata.
  773. */
  774. gulp.task("typedoc-generate", function () {
  775. return gulp
  776. .src(["../../dist/preview release/babylon.d.ts"])
  777. .pipe(typedoc({
  778. // TypeScript options (see typescript docs)
  779. mode: "modules",
  780. module: "commonjs",
  781. target: "es5",
  782. includeDeclarations: true,
  783. // Output options (see typedoc docs)
  784. json: config.build.typedocJSON,
  785. // TypeDoc options (see typedoc docs)
  786. ignoreCompilerErrors: true,
  787. readme: "none",
  788. excludeExternals: true,
  789. excludePrivate: true,
  790. excludeProtected: true,
  791. entryPoint: ["\"babylon.d\"", "BABYLON"]
  792. }));
  793. });
  794. /**
  795. * Validate the TypeDoc JSON output against the current baselin to ensure our code is correctly documented.
  796. * (in the newly introduced areas)
  797. */
  798. gulp.task("typedoc-validate", function () {
  799. return gulp.src(config.build.typedocJSON)
  800. .pipe(validateTypedoc(config.build.typedocValidationBaseline, "BABYLON", true, false));
  801. });
  802. /**
  803. * Generate the validation reference to ensure our code is correctly documented.
  804. */
  805. gulp.task("typedoc-generateValidationBaseline", function () {
  806. return gulp.src(config.build.typedocJSON)
  807. .pipe(validateTypedoc(config.build.typedocValidationBaseline, "BABYLON", true, true));
  808. });
  809. /**
  810. * Validate the code comments and style case convention through typedoc and
  811. * generate the new baseline.
  812. */
  813. gulp.task("typedoc-all", function (cb) {
  814. runSequence("typedoc-generate", "typedoc-validate", "typedoc-generateValidationBaseline", cb);
  815. });
  816. /**
  817. * Validate compile the code and check the comments and style case convention through typedoc
  818. */
  819. gulp.task("typedoc-check", function (cb) {
  820. runSequence("typescript-compile", "typedoc-generate", "typedoc-validate", cb);
  821. });
  822. /**
  823. * Launches the KARMA validation tests in chrome in order to debug them.
  824. * (Can only be launch locally.)
  825. */
  826. gulp.task("tests-validation-karma", function (done) {
  827. var kamaServerOptions = {
  828. configFile: __dirname + "/../../tests/validation/karma.conf.js",
  829. singleRun: false
  830. };
  831. var server = new karmaServer(kamaServerOptions, done);
  832. server.start();
  833. });
  834. /**
  835. * Launches the KARMA validation tests in ff or virtual screen ff on travis for a quick analysis during the build.
  836. * (Can only be launch on any branches.)
  837. */
  838. gulp.task("tests-validation-virtualscreen", function (done) {
  839. var kamaServerOptions = {
  840. configFile: __dirname + "/../../tests/validation/karma.conf.js",
  841. singleRun: true,
  842. browsers: ['Firefox']
  843. };
  844. var server = new karmaServer(kamaServerOptions, done);
  845. server.start();
  846. });
  847. /**
  848. * Launches the KARMA validation tests in browser stack for remote and cross devices validation tests.
  849. * (Can only be launch from secure branches.)
  850. */
  851. gulp.task("tests-validation-browserstack", function (done) {
  852. if (!process.env.BROWSER_STACK_USERNAME) {
  853. done();
  854. return;
  855. }
  856. var kamaServerOptions = {
  857. configFile: __dirname + "/../../tests/validation/karma.conf.browserstack.js",
  858. singleRun: true
  859. };
  860. var server = new karmaServer(kamaServerOptions, done);
  861. server.start();
  862. });
  863. /**
  864. * Transpiles typescript unit tests.
  865. */
  866. gulp.task("tests-unit-transpile", function (done) {
  867. var tsProject = typescript.createProject('../../tests/unit/tsconfig.json');
  868. var tsResult = gulp.src("../../tests/unit/**/*.ts", { base: "../../" })
  869. .pipe(tsProject());
  870. tsResult.once("error", function () {
  871. tsResult.once("finish", function () {
  872. console.log("Typescript compile failed");
  873. process.exit(1);
  874. });
  875. });
  876. return tsResult.js.pipe(gulp.dest("../../"));
  877. });
  878. /**
  879. * Launches the KARMA unit tests in phantomJS.
  880. * (Can only be launch on any branches.)
  881. */
  882. gulp.task("tests-unit-debug", ["tests-unit-transpile"], function (done) {
  883. var kamaServerOptions = {
  884. configFile: __dirname + "/../../tests/unit/karma.conf.js",
  885. singleRun: false,
  886. browsers: ['Chrome']
  887. };
  888. var server = new karmaServer(kamaServerOptions, done);
  889. server.start();
  890. });
  891. /**
  892. * Launches the KARMA unit tests in phantomJS.
  893. * (Can only be launch on any branches.)
  894. */
  895. gulp.task("tests-unit", ["tests-unit-transpile"], function (done) {
  896. var kamaServerOptions = {
  897. configFile: __dirname + "/../../tests/unit/karma.conf.js",
  898. singleRun: true
  899. };
  900. var server = new karmaServer(kamaServerOptions, done);
  901. server.start();
  902. });
  903. gulp.task("tests-whatsnew", function (done) {
  904. // Only checks on Travis
  905. if (!process.env.TRAVIS) {
  906. done();
  907. return;
  908. }
  909. // Only checks on Pull Requests
  910. if (process.env.TRAVIS_PULL_REQUEST == "false") {
  911. done();
  912. return;
  913. }
  914. // Do not check deploy
  915. if (process.env.TRAVIS_BRANCH == "preview") {
  916. done();
  917. return;
  918. }
  919. // Compare what's new with the current one in the preview release folder.
  920. const https = require("https");
  921. const url = "https://rawgit.com/BabylonJS/Babylon.js/master/dist/preview%20release/what's%20new.md";
  922. https.get(url, res => {
  923. res.setEncoding("utf8");
  924. let oldData = "";
  925. res.on("data", data => {
  926. oldData += data;
  927. });
  928. res.on("end", () => {
  929. fs.readFile("../../dist/preview release/what's new.md", "utf-8", function (err, newData) {
  930. if (err || oldData != newData) {
  931. done();
  932. return;
  933. }
  934. console.error("What's new file did not change.");
  935. process.exit(1);
  936. });
  937. });
  938. });
  939. });