gulpfile.js 52 KB

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