gulpfile.js 43 KB

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