gulpfile.js 37 KB

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