gulpfile.js 43 KB

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