gulpfile.js 37 KB

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