gulpfile.js 52 KB

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