gulpfile.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. strict: true,
  62. types: [],
  63. lib: [
  64. "dom",
  65. "es2015.promise",
  66. "es5"
  67. ]
  68. };
  69. var tsProject = typescript.createProject(tsConfig);
  70. var externalTsConfig = {
  71. noResolve: false,
  72. target: "ES5",
  73. declarationFiles: true,
  74. typescript: require("typescript"),
  75. experimentalDecorators: true,
  76. isolatedModules: false,
  77. noImplicitAny: true,
  78. noImplicitReturns: true,
  79. noImplicitThis: true,
  80. noUnusedLocals: true,
  81. strictNullChecks: true,
  82. strict: 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. // create the file
  416. dtsBundle.bundle(settings.build.dtsBundle);
  417. // prepend the needed reference
  418. fs.readFile(settings.build.dtsBundle.out, function (err, data) {
  419. if (err) throw err;
  420. data = settings.build.dtsBundle.prependText + data.toString();
  421. fs.writeFile(settings.build.dtsBundle.out, data);
  422. });
  423. });
  424. }
  425. let build = wpBuild
  426. .pipe(through.obj(function (file, enc, cb) {
  427. // only pipe js files
  428. const isJs = /\.js$/.test(file.path);
  429. if (isJs) this.push(file);
  430. cb();
  431. }))
  432. .pipe(addModuleExports(library.moduleDeclaration, { subModule: false, extendsRoot: false, externalUsingBabylon: true, noBabylonInit: library.babylonIncluded }));
  433. let unminifiedOutpus = [];
  434. let minifiedOutputs = [];
  435. settings.build.outputs.forEach(out => {
  436. if (out.minified) {
  437. out.destination.forEach(dest => {
  438. minifiedOutputs.push(dest);
  439. });
  440. } else {
  441. out.destination.forEach(dest => {
  442. unminifiedOutpus.push(dest);
  443. });
  444. }
  445. });
  446. function processDestination(dest) {
  447. var outputDirectory = config.build.outputDirectory + dest.outputDirectory;
  448. build = build
  449. .pipe(rename(dest.filename.replace(".js", library.noBundleInName ? '.js' : ".bundle.js")))
  450. .pipe(gulp.dest(outputDirectory));
  451. if (library.babylonIncluded && dest.addBabylonDeclaration) {
  452. // include the babylon declaration
  453. sequence.unshift(gulp.src(config.build.outputDirectory + '/' + config.build.declarationFilename)
  454. .pipe(gulp.dest(outputDirectory)))
  455. }
  456. }
  457. unminifiedOutpus.forEach(dest => {
  458. processDestination(dest);
  459. });
  460. if (minifiedOutputs.length) {
  461. build = build
  462. .pipe(uglify())
  463. .pipe(optimisejs())
  464. }
  465. minifiedOutputs.forEach(dest => {
  466. processDestination(dest);
  467. });
  468. sequence.push(build);
  469. } else {
  470. sequence.push(
  471. wpBuild
  472. .pipe(rename(library.output.replace(".js", library.noBundleInName ? '.js' : ".bundle.js")))
  473. .pipe(addModuleExports(library.moduleDeclaration, { subModule: false, extendsRoot: library.extendsRoot, externalUsingBabylon: true }))
  474. .pipe(uglify())
  475. .pipe(optimisejs())
  476. .pipe(gulp.dest(outputDirectory))
  477. )
  478. }
  479. return merge2(sequence);
  480. }
  481. else {
  482. return waitAll;
  483. }
  484. }
  485. }
  486. /**
  487. * The default task, concat and min the main BJS files.
  488. */
  489. gulp.task("default", function (cb) {
  490. runSequence("typescript-all", "intellisense", "typedoc-all", "tests-unit", "tests-validation-virtualscreen", "tests-validation-browserstack", cb);
  491. });
  492. gulp.task("mainBuild", function (cb) {
  493. runSequence("buildWorker", "build", cb);
  494. });
  495. /**
  496. * Build the releasable files.
  497. */
  498. gulp.task("typescript", function (cb) {
  499. runSequence("typescript-compile", "mainBuild", cb);
  500. });
  501. /**
  502. * Dynamic module creation.
  503. */
  504. config.modules.map(function (module) {
  505. gulp.task(module, function () {
  506. return buildExternalLibraries(config[module]);
  507. });
  508. });
  509. gulp.task("typescript-libraries", config.modules, function () {
  510. });
  511. /**
  512. * Custom build with full path file control; used by profile.html
  513. */
  514. gulp.task("build-custom", function (cb) {
  515. runSequence("typescript-compile", "build", cb);
  516. });
  517. /**
  518. * Do it all.
  519. */
  520. gulp.task("typescript-all", function (cb) {
  521. runSequence("typescript", "typescript-libraries", cb);
  522. });
  523. /**
  524. * Watch ts files from typescript .
  525. */
  526. gulp.task("srcTscWatch", function () {
  527. // Reuse The TSC CLI from gulp to enable -w.
  528. process.argv[2] = "-w";
  529. process.argv[3] = "-p";
  530. process.argv[4] = "../../src/tsconfig.json";
  531. require("./node_modules/typescript/lib/tsc.js");
  532. });
  533. /**
  534. * Watch ts files and fire repective tasks.
  535. */
  536. gulp.task("watch", ["srcTscWatch"], function () {
  537. var interval = 1000;
  538. var tasks = [];
  539. config.modules.map(function (module) {
  540. config[module].libraries.map(function (library) {
  541. tasks.push(gulp.watch(library.files, { interval: interval }, function () {
  542. console.log(library.output);
  543. return buildExternalLibrary(library, config[module], true)
  544. .pipe(debug());
  545. }));
  546. tasks.push(gulp.watch(library.shaderFiles, { interval: interval }, function () {
  547. console.log(library.output);
  548. return buildExternalLibrary(library, config[module], true)
  549. .pipe(debug())
  550. }));
  551. tasks.push(gulp.watch(library.sassFiles, { interval: interval }, function () {
  552. console.log(library.output);
  553. return buildExternalLibrary(library, config[module], true)
  554. .pipe(debug())
  555. }));
  556. });
  557. });
  558. return tasks;
  559. });
  560. gulp.task("intellisense", function () {
  561. gulp.src(config.build.intellisenseSources)
  562. .pipe(concat(config.build.intellisenseFile))
  563. .pipe(replace(/^\s+_.*?;/gm, ""))
  564. .pipe(replace(/^\s+_[\S\s]*?}/gm, ""))
  565. .pipe(replace(/^\s*readonly _/gm, "protected readonly _"))
  566. .pipe(replace(/^\s*static _/gm, "private static _"))
  567. .pipe(replace(/^\s*abstract _/gm, ""))
  568. .pipe(gulp.dest(config.build.playgroundDirectory));
  569. });
  570. /**
  571. * Embedded local dev env management.
  572. */
  573. gulp.task("deployLocalDev", function () {
  574. gulp.src("../../localDev/template/**.*")
  575. .pipe(gulp.dest("../../localDev/src/"));
  576. });
  577. /**
  578. * Embedded webserver for test convenience.
  579. */
  580. gulp.task("webserver", function () {
  581. var options = {
  582. port: 1338,
  583. livereload: false,
  584. };
  585. if (commandLineOptions.public) {
  586. options.host = "0.0.0.0";
  587. }
  588. gulp.src("../../.").pipe(webserver(options));
  589. });
  590. /**
  591. * Combine Webserver and Watch as long as vscode does not handle multi tasks.
  592. */
  593. gulp.task("run", ["watch", "webserver"], function () {
  594. });
  595. /**
  596. * Cleans map and js files from the src folder.
  597. */
  598. gulp.task("clean-JS-MAP", function () {
  599. return del([
  600. "../../src/**/*.js.map", "../../src/**/*.js"
  601. ], { force: true });
  602. });
  603. // this is needed for the modules for the declaration files.
  604. gulp.task("modules-compile", function () {
  605. var tsResult = gulp.src(config.typescript)
  606. .pipe(sourcemaps.init())
  607. .pipe(tsProject());
  608. // If this gulp task is running on travis
  609. if (process.env.TRAVIS) {
  610. tsResult.once("error", function () {
  611. tsResult.once("finish", function () {
  612. console.log("Typescript compile failed");
  613. process.exit(1);
  614. });
  615. });
  616. }
  617. return merge2([
  618. tsResult.dts
  619. .pipe(gulp.dest(config.build.srcOutputDirectory)),
  620. tsResult.js
  621. .pipe(sourcemaps.write("./",
  622. {
  623. includeContent: false,
  624. sourceRoot: (filePath) => {
  625. return "";
  626. }
  627. }))
  628. .pipe(gulp.dest(config.build.srcOutputDirectory))
  629. ]);
  630. });
  631. // this holds the declared objects in each module
  632. let declared = {}
  633. let perFile = {};
  634. let dependencyTree = {};
  635. gulp.task('prepare-for-modules', /*["modules-compile"],*/ function () {
  636. let tasks = [];
  637. Object.keys(config.workloads).forEach((moduleName) => {
  638. let dtsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".d.ts"))
  639. let dtsTask = gulp.src(dtsFiles)
  640. .pipe(dtsModuleSupport(moduleName, false, declared, perFile));
  641. tasks.push(dtsTask);
  642. });
  643. // now calculate internal dependencies in the .ts files!
  644. /*Object.keys(config.workloads).forEach((moduleName) => {
  645. let tsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".ts"))
  646. let depTask = gulp.src(tsFiles)
  647. .pipe(calculateDependencies(moduleName, perFile, dependencyTree));
  648. tasks.push(depTask);
  649. });*/
  650. return merge2(tasks);
  651. });
  652. gulp.task('prepare-dependency-tree', ["prepare-for-modules"], function () {
  653. let tasks = [];
  654. // now calculate internal dependencies in the .ts files!
  655. Object.keys(config.workloads).forEach((moduleName) => {
  656. let tsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".ts"))
  657. let depTask = gulp.src(tsFiles)
  658. .pipe(calculateDependencies(moduleName, perFile, declared, dependencyTree));
  659. tasks.push(depTask);
  660. });
  661. return merge2(tasks);
  662. });
  663. // generate the modules directory, along with commonjs modules and es6 modules
  664. // Note - the generated modules are UNMINIFIED! The user will choose whether they want to minify or not.
  665. gulp.task("modules", ["prepare-dependency-tree"], function () {
  666. let tasks = [];
  667. Object.keys(config.workloads)
  668. .forEach((moduleName) => {
  669. let shadersFiles = [];
  670. processDependency("shaders", config.workloads[moduleName], shadersFiles, true);
  671. for (var index = 0; index < shadersFiles.length; index++) {
  672. shadersFiles[index] = "../../src/Shaders/" + shadersFiles[index] + ".fx";
  673. }
  674. let shaderIncludeFiles = [];
  675. processDependency("shaderIncludes", config.workloads[moduleName], shaderIncludeFiles, true);
  676. for (var index = 0; index < shaderIncludeFiles.length; index++) {
  677. shaderIncludeFiles[index] = "../../src/Shaders/ShadersInclude/" + shaderIncludeFiles[index] + ".fx";
  678. }
  679. let commonJsTask = merge2([
  680. gulp.src(config.workloads[moduleName].files)
  681. .pipe(replace(extendsSearchRegex, ""))
  682. .pipe(replace(decorateSearchRegex, ""))
  683. .pipe(replace(referenceSearchRegex, ""))
  684. .pipe(replace(/var BABYLON;\n/g, ""))
  685. .pipe(babylonModuleExports(moduleName, dependencyTree, false, perFile, shadersFiles.length, shaderIncludeFiles.length))
  686. .pipe(rename(function (path) {
  687. path.basename = path.basename.split(".").pop()
  688. path.extname = ".js"
  689. })),
  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", "commonjs"))
  694. .pipe(rename("shaders.js")),
  695. gulp.src(shaderIncludeFiles)
  696. .pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles))
  697. .pipe(uncommentShader())
  698. .pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", "commonjs"))
  699. .pipe(rename("shaderIncludes.js")),
  700. gulp.src(config.workloads[moduleName].files)
  701. .pipe(concat('index.js'))
  702. .pipe(babylonModuleExports(moduleName, dependencyTree, true, perFile))
  703. ]).pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'))
  704. let es6Tasks = merge2([
  705. gulp.src(config.workloads[moduleName].files)
  706. .pipe(replace(extendsSearchRegex, ""))
  707. .pipe(replace(decorateSearchRegex, ""))
  708. .pipe(replace(referenceSearchRegex, ""))
  709. .pipe(replace(/var BABYLON;\n/g, ""))
  710. .pipe(babylonES6ModuleExports(moduleName, dependencyTree, false, perFile, shadersFiles.length, shaderIncludeFiles.length))
  711. .pipe(rename(function (path) {
  712. path.basename = path.basename.split(".").pop()
  713. path.extname = ".js"
  714. })),
  715. gulp.src(shadersFiles)
  716. .pipe(expect.real({ errorOnFailure: true }, shadersFiles))
  717. .pipe(uncommentShader())
  718. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/es6/' + moduleName + ".fx", "es6"))
  719. .pipe(rename("shaders.js")),
  720. gulp.src(shaderIncludeFiles)
  721. .pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles))
  722. .pipe(uncommentShader())
  723. .pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/es6/' + moduleName + ".include.fx", "es6"))
  724. .pipe(rename("shaderIncludes.js")),
  725. gulp.src(config.workloads[moduleName].files)
  726. .pipe(concat('index.js'))
  727. .pipe(babylonES6ModuleExports(moduleName, dependencyTree, true, perFile))
  728. ]).pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/es6/'))
  729. //commonjs js generation task
  730. /*let jsTask = merge2([
  731. gulp.src(config.workloads[moduleName].files),
  732. gulp.src(shadersFiles).
  733. //pipe(expect.real({ errorOnFailure: true }, shadersFiles)).
  734. pipe(uncommentShader()).
  735. pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", true)),
  736. gulp.src(shaderIncludeFiles).
  737. //pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles)).
  738. pipe(uncommentShader()).
  739. pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", true))
  740. ]).pipe(concat('index.js'))
  741. .pipe(replace(extendsSearchRegex, ""))
  742. .pipe(replace(decorateSearchRegex, ""))
  743. .pipe(replace(referenceSearchRegex, ""))
  744. .pipe(babylonModuleExports(moduleName, config.workloads[moduleName].dependUpon))
  745. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));*/
  746. // es6 modules generation task
  747. /*let es6Task = merge2([
  748. gulp.src(config.workloads[moduleName].files),
  749. gulp.src(shadersFiles).
  750. //pipe(expect.real({ errorOnFailure: true }, shadersFiles)).
  751. pipe(uncommentShader()).
  752. pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", true)),
  753. gulp.src(shaderIncludeFiles).
  754. //pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles)).
  755. pipe(uncommentShader()).
  756. pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", true))
  757. ]).pipe(concat('es6.js'))
  758. .pipe(replace(extendsSearchRegex, ""))
  759. .pipe(replace(decorateSearchRegex, ""))
  760. .pipe(replace(referenceSearchRegex, ""))
  761. .pipe(replace(/var BABYLON;/g, ""))
  762. .pipe(babylonES6ModuleExports(moduleName, config.workloads[moduleName].dependUpon))
  763. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));
  764. // dts genration task
  765. let dtsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".d.ts"))
  766. let dtsTask = gulp.src(dtsFiles)
  767. .pipe(concat("index.d.ts"))
  768. .pipe(replace(/declare module BABYLON {/g, `declare module 'babylonjs/${moduleName}' {`))
  769. .pipe(replace(/\ninterface /g, `\nexport interface `))
  770. .pipe(dtsModuleSupport(moduleName, true, declared, perFile, dependencyTree))
  771. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));
  772. */
  773. tasks.push(commonJsTask, es6Tasks);
  774. });
  775. // run da tasks man!
  776. return merge2(tasks);
  777. })
  778. /**
  779. * Generate the TypeDoc JSON output in order to create code metadata.
  780. */
  781. gulp.task("typedoc-generate", function () {
  782. return gulp
  783. .src(["../../dist/preview release/babylon.d.ts"])
  784. .pipe(typedoc({
  785. // TypeScript options (see typescript docs)
  786. mode: "modules",
  787. module: "commonjs",
  788. target: "es5",
  789. includeDeclarations: true,
  790. // Output options (see typedoc docs)
  791. json: config.build.typedocJSON,
  792. // TypeDoc options (see typedoc docs)
  793. ignoreCompilerErrors: true,
  794. readme: "none",
  795. excludeExternals: true,
  796. excludePrivate: true,
  797. excludeProtected: true,
  798. entryPoint: ["\"babylon.d\"", "BABYLON"]
  799. }));
  800. });
  801. /**
  802. * Validate the TypeDoc JSON output against the current baselin to ensure our code is correctly documented.
  803. * (in the newly introduced areas)
  804. */
  805. gulp.task("typedoc-validate", function () {
  806. return gulp.src(config.build.typedocJSON)
  807. .pipe(validateTypedoc(config.build.typedocValidationBaseline, "BABYLON", true, false));
  808. });
  809. /**
  810. * Generate the validation reference to ensure our code is correctly documented.
  811. */
  812. gulp.task("typedoc-generateValidationBaseline", function () {
  813. return gulp.src(config.build.typedocJSON)
  814. .pipe(validateTypedoc(config.build.typedocValidationBaseline, "BABYLON", true, true));
  815. });
  816. /**
  817. * Validate the code comments and style case convention through typedoc and
  818. * generate the new baseline.
  819. */
  820. gulp.task("typedoc-all", function (cb) {
  821. runSequence("typedoc-generate", "typedoc-validate", "typedoc-generateValidationBaseline", cb);
  822. });
  823. /**
  824. * Validate compile the code and check the comments and style case convention through typedoc
  825. */
  826. gulp.task("typedoc-check", function (cb) {
  827. runSequence("typescript-compile", "typedoc-generate", "typedoc-validate", cb);
  828. });
  829. /**
  830. * Launches the KARMA validation tests in chrome in order to debug them.
  831. * (Can only be launch locally.)
  832. */
  833. gulp.task("tests-validation-karma", function (done) {
  834. var kamaServerOptions = {
  835. configFile: __dirname + "/../../tests/validation/karma.conf.js",
  836. singleRun: false
  837. };
  838. var server = new karmaServer(kamaServerOptions, done);
  839. server.start();
  840. });
  841. /**
  842. * Launches the KARMA validation tests in ff or virtual screen ff on travis for a quick analysis during the build.
  843. * (Can only be launch on any branches.)
  844. */
  845. gulp.task("tests-validation-virtualscreen", function (done) {
  846. var kamaServerOptions = {
  847. configFile: __dirname + "/../../tests/validation/karma.conf.js",
  848. singleRun: true,
  849. browsers: ['Firefox']
  850. };
  851. var server = new karmaServer(kamaServerOptions, done);
  852. server.start();
  853. });
  854. /**
  855. * Launches the KARMA validation tests in browser stack for remote and cross devices validation tests.
  856. * (Can only be launch from secure branches.)
  857. */
  858. gulp.task("tests-validation-browserstack", function (done) {
  859. if (!process.env.BROWSER_STACK_USERNAME) {
  860. done();
  861. return;
  862. }
  863. var kamaServerOptions = {
  864. configFile: __dirname + "/../../tests/validation/karma.conf.browserstack.js",
  865. singleRun: true
  866. };
  867. var server = new karmaServer(kamaServerOptions, done);
  868. server.start();
  869. });
  870. /**
  871. * Transpiles typescript unit tests.
  872. */
  873. gulp.task("tests-unit-transpile", function (done) {
  874. var tsProject = typescript.createProject('../../tests/unit/tsconfig.json');
  875. var tsResult = gulp.src("../../tests/unit/**/*.ts", { base: "../../" })
  876. .pipe(tsProject());
  877. tsResult.once("error", function () {
  878. tsResult.once("finish", function () {
  879. console.log("Typescript compile failed");
  880. process.exit(1);
  881. });
  882. });
  883. return tsResult.js.pipe(gulp.dest("../../"));
  884. });
  885. /**
  886. * Launches the KARMA unit tests in phantomJS.
  887. * (Can only be launch on any branches.)
  888. */
  889. gulp.task("tests-unit-debug", ["tests-unit-transpile"], function (done) {
  890. var kamaServerOptions = {
  891. configFile: __dirname + "/../../tests/unit/karma.conf.js",
  892. singleRun: false,
  893. browsers: ['Chrome']
  894. };
  895. var server = new karmaServer(kamaServerOptions, done);
  896. server.start();
  897. });
  898. /**
  899. * Launches the KARMA unit tests in phantomJS.
  900. * (Can only be launch on any branches.)
  901. */
  902. gulp.task("tests-unit", ["tests-unit-transpile"], function (done) {
  903. var kamaServerOptions = {
  904. configFile: __dirname + "/../../tests/unit/karma.conf.js",
  905. singleRun: true
  906. };
  907. var server = new karmaServer(kamaServerOptions, done);
  908. server.start();
  909. });
  910. gulp.task("tests-whatsnew", function (done) {
  911. // Only checks on Travis
  912. if (!process.env.TRAVIS) {
  913. done();
  914. return;
  915. }
  916. // Only checks on Pull Requests
  917. if (process.env.TRAVIS_PULL_REQUEST == "false") {
  918. done();
  919. return;
  920. }
  921. // Do not check deploy
  922. if (process.env.TRAVIS_BRANCH == "preview") {
  923. done();
  924. return;
  925. }
  926. // Compare what's new with the current one in the preview release folder.
  927. const https = require("https");
  928. const url = "https://rawgit.com/BabylonJS/Babylon.js/master/dist/preview%20release/what's%20new.md";
  929. https.get(url, res => {
  930. res.setEncoding("utf8");
  931. let oldData = "";
  932. res.on("data", data => {
  933. oldData += data;
  934. });
  935. res.on("end", () => {
  936. fs.readFile("../../dist/preview release/what's new.md", "utf-8", function (err, newData) {
  937. if (err || oldData != newData) {
  938. done();
  939. return;
  940. }
  941. console.error("What's new file did not change.");
  942. process.exit(1);
  943. });
  944. });
  945. });
  946. });