gulpfile.js 41 KB

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