gulpfile.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  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 shadersIndlueTask;
  401. if (library.shadersIncludeFiles && library.shadersIncludeFiles.length) {
  402. shadersIndlueTask = 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(shadersIndlueTask);
  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. /*let cssTask;
  428. if (library.sassFiles && library.sassFiles.length) {
  429. cssTask = gulp.src(library.sassFiles)
  430. .pipe(sass().on("error", sass.logError))
  431. .pipe(concat(library.output.replace(".js", ".css")))
  432. .pipe(gulp.dest(outputDirectory));
  433. tasks.push(cssTask);
  434. }*/
  435. if (watch) {
  436. return merge2(tasks);
  437. }
  438. else {
  439. /*if (library.bundle) {
  440. // Don't remove extends and decorate functions
  441. var code = merge2([tsProcess.js, shader, includeShader])
  442. .pipe(concat(library.output));
  443. if (library.buildAsModule) {
  444. code = code.pipe(addModuleExports(library.moduleDeclaration, true))
  445. }
  446. code.pipe(gulp.dest(outputDirectory))
  447. .pipe(cleants())
  448. .pipe(rename({ extname: ".min.js" }))
  449. .pipe(uglify())
  450. .pipe(optimisejs())
  451. .pipe(gulp.dest(outputDirectory));
  452. } else {*/
  453. let currentTasks = [];
  454. if (tsProcess) {
  455. currentTasks.push(tsProcess.js);
  456. }
  457. if (shaderTask) {
  458. currentTasks.push(shaderTask);
  459. }
  460. if (shadersIndlueTask) {
  461. currentTasks.push(shadersIndlueTask);
  462. }
  463. var code;
  464. if (currentTasks.length) {
  465. code = merge2(currentTasks)
  466. .pipe(concat(library.output));
  467. }
  468. if (library.buildAsModule && code) {
  469. code = code.pipe(replace(extendsSearchRegex, ""))
  470. .pipe(replace(decorateSearchRegex, ""))
  471. .pipe(addDecorateAndExtends())
  472. .pipe(addModuleExports(library.moduleDeclaration, { subModule: true, extendsRoot: library.extendsRoot }))
  473. }
  474. if (code) {
  475. code = code.pipe(gulp.dest(outputDirectory))
  476. .pipe(cleants())
  477. .pipe(rename({ extname: ".min.js" }))
  478. .pipe(uglify())
  479. .pipe(optimisejs())
  480. .pipe(gulp.dest(outputDirectory));
  481. /*}*/
  482. }
  483. var dts;
  484. if (tsProcess) {
  485. dts = tsProcess.dts
  486. .pipe(concat(library.output))
  487. .pipe(replace(referenceSearchRegex, ""))
  488. .pipe(rename({ extname: ".d.ts" }))
  489. .pipe(gulp.dest(outputDirectory));
  490. }
  491. var waitAll;
  492. let waitAllTasks = [];
  493. /*if (cssTask) {
  494. waitAllTasks.push(cssTask);
  495. }*/
  496. if (dev) {
  497. waitAllTasks.push(dev);
  498. }
  499. if (code) {
  500. waitAllTasks.push(code);
  501. }
  502. if (dts) {
  503. waitAllTasks.push(dts);
  504. }
  505. if (library.buildAsModule && tsProcess) {
  506. var dts2 = tsProcess.dts
  507. .pipe(concat(library.output))
  508. .pipe(replace(referenceSearchRegex, ""))
  509. .pipe(addDtsExport(library.moduleDeclaration, library.moduleName, true, library.extendsRoot, config.build.extraTypesDependencies))
  510. .pipe(rename({ extname: ".module.d.ts" }))
  511. .pipe(gulp.dest(outputDirectory));
  512. waitAllTasks.push(dts2);
  513. }
  514. if (waitAllTasks.length) {
  515. waitAll = merge2(waitAllTasks);
  516. }
  517. if (library.webpack) {
  518. let sequence = [];
  519. if (waitAll) {
  520. sequence.push(waitAll);
  521. }
  522. if (settings.build.outputs) {
  523. settings.build.outputs.forEach(out => {
  524. let wpConfig = require(library.webpack);
  525. if (!out.minified) {
  526. wpConfig.mode = "development";
  527. }
  528. let wpBuild = webpackStream(wpConfig, require("webpack"));
  529. //shoud dtsBundle create the declaration?
  530. if (settings.build.dtsBundle) {
  531. let event = wpBuild
  532. .pipe(through.obj(function(file, enc, cb) {
  533. // only declaration files
  534. const isdts = /\.d\.ts$/.test(file.path);
  535. if (isdts) this.push(file);
  536. cb();
  537. }))
  538. .pipe(gulp.dest(outputDirectory));
  539. // dts-bundle does NOT support (gulp) streams, so files have to be saved and reloaded,
  540. // until I fix it
  541. event.on("end", function() {
  542. // create the file
  543. dtsBundle.bundle(settings.build.dtsBundle);
  544. // prepend the needed reference
  545. let fileLocation = path.join(path.dirname(settings.build.dtsBundle.main), settings.build.dtsBundle.out);
  546. fs.readFile(fileLocation, function(err, data) {
  547. if (err) throw err;
  548. data = (settings.build.dtsBundle.prependText || "") + '\n' + data.toString();
  549. fs.writeFileSync(fileLocation, data);
  550. if (settings.build.processDeclaration) {
  551. var newData = processDeclaration(data, settings.build.processDeclaration);
  552. fs.writeFileSync(fileLocation.replace('.module', ''), newData);
  553. }
  554. });
  555. });
  556. }
  557. let build = wpBuild
  558. .pipe(through.obj(function(file, enc, cb) {
  559. // only pipe js files
  560. const isJs = /\.js$/.test(file.path);
  561. if (isJs) this.push(file);
  562. cb();
  563. }))
  564. .pipe(addModuleExports(library.moduleDeclaration, { subModule: false, extendsRoot: false, externalUsingBabylon: true, noBabylonInit: library.babylonIncluded }));
  565. function processDestination(dest) {
  566. var outputDirectory = config.build.outputDirectory + dest.outputDirectory;
  567. build = build
  568. .pipe(rename(dest.filename.replace(".js", library.noBundleInName ? '.js' : ".bundle.js")))
  569. .pipe(gulp.dest(outputDirectory));
  570. if (library.babylonIncluded && dest.addBabylonDeclaration) {
  571. // include the babylon declaration
  572. if (dest.addBabylonDeclaration === true) {
  573. dest.addBabylonDeclaration = [config.build.declarationFilename];
  574. }
  575. var decsToAdd = dest.addBabylonDeclaration.map(function(dec) {
  576. return config.build.outputDirectory + '/' + dec;
  577. });
  578. sequence.unshift(gulp.src(decsToAdd)
  579. .pipe(rename(function(path) {
  580. path.dirname = '';
  581. }))
  582. .pipe(gulp.dest(outputDirectory)))
  583. }
  584. }
  585. out.destinations.forEach(dest => {
  586. processDestination(dest);
  587. });
  588. sequence.push(build);
  589. });
  590. } else {
  591. var wpConfig = require(library.webpack);
  592. let wpBuild = webpackStream(wpConfig, webpack);
  593. let buildEvent = wpBuild
  594. .pipe(gulp.dest(outputDirectory))
  595. //back-compat
  596. .pipe(through.obj(function(file, enc, cb) {
  597. // only js files
  598. const isjs = /\.js$/.test(file.path);
  599. if (isjs) this.push(file);
  600. cb();
  601. }))
  602. .pipe(rename(function(path) {
  603. console.log(path.basename);
  604. //path.extname === ".js"
  605. path.basename = path.basename.replace(".min", "")
  606. })).pipe(gulp.dest(outputDirectory));
  607. sequence.push(
  608. buildEvent
  609. );
  610. if (settings.build.dtsBundle || settings.build.processDeclaration) {
  611. buildEvent.on("end", function() {
  612. if (settings.build.dtsBundle) {
  613. dtsBundle.bundle(settings.build.dtsBundle);
  614. } if (settings.build.processDeclaration) {
  615. let fileLocation = path.join(outputDirectory, settings.build.processDeclaration.filename);
  616. fs.readFile(fileLocation, function(err, data) {
  617. if (err) throw err;
  618. // For Raanan, litteral import hack TO BETTER INTEGRATE
  619. data = data + "";
  620. data = data.replace('import "../sass/main.scss";', "");
  621. var newData = processDeclaration(data, settings.build.processDeclaration);
  622. fs.writeFileSync(fileLocation.replace('.module', ''), newData);
  623. //legacy module support
  624. fs.writeFileSync(fileLocation, data + "\n" + newData);
  625. });
  626. }
  627. });
  628. }
  629. /*if (settings.build.processDeclaration) {
  630. sequence.push(
  631. wpBuild
  632. .pipe(through.obj(function (file, enc, cb) {
  633. // only js files
  634. const isDts = /\.d.ts$/.test(file.path);
  635. file.contents = new Buffer(processDeclaration(file.contents, settings.build.processDeclaration));
  636. if (isDts) this.push(file);
  637. cb();
  638. }))
  639. .pipe(gulp.dest(outputDirectory))
  640. )
  641. }*/
  642. }
  643. return merge2(sequence);
  644. }
  645. else {
  646. return waitAll || Promise.resolve();
  647. }
  648. }
  649. }
  650. gulp.task("mainBuild", gulp.series("buildWorker", "build"));
  651. /**
  652. * Build the releasable files.
  653. */
  654. gulp.task("typescript", gulp.series("typescript-compile", "mainBuild"));
  655. /**
  656. * Dynamic module creation.
  657. */
  658. config.modules.map(function(module) {
  659. gulp.task(module, function() {
  660. return buildExternalLibraries(config[module]);
  661. });
  662. });
  663. gulp.task("typescript-libraries", gulp.series(config.modules));
  664. /**
  665. * Custom build with full path file control; used by profile.html
  666. */
  667. gulp.task("build-custom", gulp.series("typescript-compile", "build"));
  668. /**
  669. * Watch ts files from typescript .
  670. */
  671. gulp.task("srcTscWatch", function() {
  672. // Reuse The TSC CLI from gulp to enable -w.
  673. process.argv[2] = "-w";
  674. process.argv[3] = "-p";
  675. process.argv[4] = "../../src/tsconfig.json";
  676. require("./node_modules/typescript/lib/tsc.js");
  677. return Promise.resolve();
  678. });
  679. /**
  680. * Watch ts files and fire repective tasks.
  681. */
  682. gulp.task("watch", gulp.series("srcTscWatch", function startWatch() {
  683. var interval = 1000;
  684. var tasks = [];
  685. config.modules.map(function(module) {
  686. config[module].libraries.map(function(library) {
  687. if (library.webpack) {
  688. if (library.noWatch) return;
  689. var outputDirectory = config.build.tempDirectory + config[module].build.distOutputDirectory;
  690. let wpconfig = require(library.webpack);
  691. wpconfig.watch = true;
  692. // dev mode and absolute path sourcemaps for debugging
  693. wpconfig.mode = "development";
  694. wpconfig.output.devtoolModuleFilenameTemplate = "[absolute-resource-path]";
  695. //config.stats = "minimal";
  696. tasks.push(webpackStream(wpconfig, webpack).pipe(gulp.dest(outputDirectory)))
  697. } else {
  698. tasks.push(gulp.watch(library.files, { interval: interval }, function() {
  699. console.log(library.output);
  700. return buildExternalLibrary(library, config[module], true)
  701. .pipe(debug());
  702. }));
  703. tasks.push(gulp.watch(library.shaderFiles, { interval: interval }, function() {
  704. console.log(library.output);
  705. return buildExternalLibrary(library, config[module], true)
  706. .pipe(debug())
  707. }));
  708. tasks.push(gulp.watch(library.sassFiles, { interval: interval }, function() {
  709. console.log(library.output);
  710. return buildExternalLibrary(library, config[module], true)
  711. .pipe(debug())
  712. }));
  713. }
  714. });
  715. });
  716. console.log(tasks.length);
  717. return Promise.resolve();
  718. }));
  719. gulp.task("intellisense", function() {
  720. return gulp.src(config.build.intellisenseSources)
  721. .pipe(concat(config.build.intellisenseFile))
  722. .pipe(replace(/^\s+_.*?;/gm, ""))
  723. .pipe(replace(/^\s+_[\S\s]*?}/gm, ""))
  724. .pipe(replace(/^\s*readonly _/gm, "protected readonly _"))
  725. .pipe(replace(/^\s*static _/gm, "private static _"))
  726. .pipe(replace(/^\s*abstract _/gm, ""))
  727. .pipe(gulp.dest(config.build.playgroundDirectory));
  728. });
  729. /**
  730. * Embedded local dev env management.
  731. */
  732. gulp.task("deployLocalDev", function() {
  733. return gulp.src("../../localDev/template/**.*")
  734. .pipe(gulp.dest("../../localDev/src/"));
  735. });
  736. /**
  737. * Embedded webserver for test convenience.
  738. */
  739. gulp.task("webserver", function() {
  740. var options = {
  741. port: 1338,
  742. livereload: false,
  743. };
  744. if (commandLineOptions.public) {
  745. options.host = "0.0.0.0";
  746. }
  747. return gulp.src("../../.").pipe(webserver(options));
  748. });
  749. /**
  750. * Combine Webserver and Watch as long as vscode does not handle multi tasks.
  751. */
  752. gulp.task("run", gulp.series("watch", "webserver"));
  753. /**
  754. * Cleans map and js files from the src folder.
  755. */
  756. gulp.task("clean-JS-MAP", function() {
  757. return del([
  758. "../../src/**/*.js.map", "../../src/**/*.js"
  759. ], { force: true });
  760. });
  761. gulp.task("netlify-cleanup", function() {
  762. //set by netlify
  763. if (process.env.REPOSITORY_URL) {
  764. return del([
  765. "../../inspector/node_modules/**/*", "../../gui/node_modules/**/*",
  766. "../../Viewer/node_modules/**/*"
  767. ], { force: true });
  768. }
  769. else {
  770. return Promise.resolve();
  771. }
  772. })
  773. // this is needed for the modules for the declaration files.
  774. gulp.task("modules-compile", function() {
  775. var tsResult = gulp.src(config.typescript)
  776. .pipe(sourcemaps.init())
  777. .pipe(tsProject());
  778. // If this gulp task is running on travis
  779. if (process.env.TRAVIS) {
  780. tsResult.once("error", function() {
  781. tsResult.once("finish", function() {
  782. console.log("Typescript compile failed");
  783. process.exit(1);
  784. });
  785. });
  786. }
  787. return merge2([
  788. tsResult.dts
  789. .pipe(gulp.dest(config.build.srcOutputDirectory)),
  790. tsResult.js
  791. .pipe(sourcemaps.write("./",
  792. {
  793. includeContent: false,
  794. sourceRoot: (filePath) => {
  795. return "";
  796. }
  797. }))
  798. .pipe(gulp.dest(config.build.srcOutputDirectory))
  799. ]);
  800. });
  801. // this holds the declared objects in each module
  802. let declared = {}
  803. let perFile = {};
  804. let dependencyTree = {};
  805. gulp.task('prepare-for-modules', /*["modules-compile"],*/ function() {
  806. let tasks = [];
  807. Object.keys(config.workloads).forEach((moduleName) => {
  808. let dtsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".d.ts"))
  809. let dtsTask = gulp.src(dtsFiles)
  810. .pipe(dtsModuleSupport(moduleName, false, declared, perFile));
  811. tasks.push(dtsTask);
  812. });
  813. // now calculate internal dependencies in the .ts files!
  814. /*Object.keys(config.workloads).forEach((moduleName) => {
  815. let tsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".ts"))
  816. let depTask = gulp.src(tsFiles)
  817. .pipe(calculateDependencies(moduleName, perFile, dependencyTree));
  818. tasks.push(depTask);
  819. });*/
  820. return merge2(tasks);
  821. });
  822. gulp.task('prepare-dependency-tree', gulp.series("prepare-for-modules", function() {
  823. let tasks = [];
  824. // now calculate internal dependencies in the .ts files!
  825. Object.keys(config.workloads).forEach((moduleName) => {
  826. let tsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".ts"))
  827. let depTask = gulp.src(tsFiles)
  828. .pipe(calculateDependencies(moduleName, perFile, declared, dependencyTree));
  829. tasks.push(depTask);
  830. });
  831. return merge2(tasks);
  832. }));
  833. // generate the modules directory, along with commonjs modules and es6 modules
  834. // Note - the generated modules are UNMINIFIED! The user will choose whether they want to minify or not.
  835. gulp.task("modules", gulp.series("prepare-dependency-tree", function() {
  836. let tasks = [];
  837. Object.keys(config.workloads)
  838. .forEach((moduleName) => {
  839. let shadersFiles = [];
  840. processDependency("shaders", config.workloads[moduleName], shadersFiles, true);
  841. for (var index = 0; index < shadersFiles.length; index++) {
  842. shadersFiles[index] = "../../src/Shaders/" + shadersFiles[index] + ".fx";
  843. }
  844. let shaderIncludeFiles = [];
  845. processDependency("shaderIncludes", config.workloads[moduleName], shaderIncludeFiles, true);
  846. for (var index = 0; index < shaderIncludeFiles.length; index++) {
  847. shaderIncludeFiles[index] = "../../src/Shaders/ShadersInclude/" + shaderIncludeFiles[index] + ".fx";
  848. }
  849. let commonJsTask = merge2([
  850. gulp.src(config.workloads[moduleName].files)
  851. .pipe(replace(extendsSearchRegex, ""))
  852. .pipe(replace(decorateSearchRegex, ""))
  853. .pipe(replace(referenceSearchRegex, ""))
  854. .pipe(replace(/var BABYLON;\n/g, ""))
  855. .pipe(babylonModuleExports(moduleName, dependencyTree, false, perFile, shadersFiles.length, shaderIncludeFiles.length))
  856. .pipe(rename(function(path) {
  857. path.basename = path.basename.split(".").pop()
  858. path.extname = ".js"
  859. })),
  860. gulp.src(shadersFiles)
  861. .pipe(expect.real({ errorOnFailure: true }, shadersFiles))
  862. .pipe(uncommentShader())
  863. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", "commonjs"))
  864. .pipe(rename("shaders.js")),
  865. gulp.src(shaderIncludeFiles)
  866. .pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles))
  867. .pipe(uncommentShader())
  868. .pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", "commonjs"))
  869. .pipe(rename("shaderIncludes.js")),
  870. gulp.src(config.workloads[moduleName].files)
  871. .pipe(concat('index.js'))
  872. .pipe(babylonModuleExports(moduleName, dependencyTree, true, perFile))
  873. ]).pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'))
  874. let es6Tasks = merge2([
  875. gulp.src(config.workloads[moduleName].files)
  876. .pipe(replace(extendsSearchRegex, ""))
  877. .pipe(replace(decorateSearchRegex, ""))
  878. .pipe(replace(referenceSearchRegex, ""))
  879. .pipe(replace(/var BABYLON;\n/g, ""))
  880. .pipe(babylonES6ModuleExports(moduleName, dependencyTree, false, perFile, shadersFiles.length, shaderIncludeFiles.length))
  881. .pipe(rename(function(path) {
  882. path.basename = path.basename.split(".").pop()
  883. path.extname = ".js"
  884. })),
  885. gulp.src(shadersFiles)
  886. .pipe(expect.real({ errorOnFailure: true }, shadersFiles))
  887. .pipe(uncommentShader())
  888. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/es6/' + moduleName + ".fx", "es6"))
  889. .pipe(rename("shaders.js")),
  890. gulp.src(shaderIncludeFiles)
  891. .pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles))
  892. .pipe(uncommentShader())
  893. .pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/es6/' + moduleName + ".include.fx", "es6"))
  894. .pipe(rename("shaderIncludes.js")),
  895. gulp.src(config.workloads[moduleName].files)
  896. .pipe(concat('index.js'))
  897. .pipe(babylonES6ModuleExports(moduleName, dependencyTree, true, perFile))
  898. ]).pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/es6/'))
  899. //commonjs js generation task
  900. /*let jsTask = merge2([
  901. gulp.src(config.workloads[moduleName].files),
  902. gulp.src(shadersFiles).
  903. //pipe(expect.real({ errorOnFailure: true }, shadersFiles)).
  904. pipe(uncommentShader()).
  905. pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", true)),
  906. gulp.src(shaderIncludeFiles).
  907. //pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles)).
  908. pipe(uncommentShader()).
  909. pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", true))
  910. ]).pipe(concat('index.js'))
  911. .pipe(replace(extendsSearchRegex, ""))
  912. .pipe(replace(decorateSearchRegex, ""))
  913. .pipe(replace(referenceSearchRegex, ""))
  914. .pipe(babylonModuleExports(moduleName, config.workloads[moduleName].dependUpon))
  915. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));*/
  916. // es6 modules generation task
  917. /*let es6Task = merge2([
  918. gulp.src(config.workloads[moduleName].files),
  919. gulp.src(shadersFiles).
  920. //pipe(expect.real({ errorOnFailure: true }, shadersFiles)).
  921. pipe(uncommentShader()).
  922. pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", true)),
  923. gulp.src(shaderIncludeFiles).
  924. //pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles)).
  925. pipe(uncommentShader()).
  926. pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", true))
  927. ]).pipe(concat('es6.js'))
  928. .pipe(replace(extendsSearchRegex, ""))
  929. .pipe(replace(decorateSearchRegex, ""))
  930. .pipe(replace(referenceSearchRegex, ""))
  931. .pipe(replace(/var BABYLON;/g, ""))
  932. .pipe(babylonES6ModuleExports(moduleName, config.workloads[moduleName].dependUpon))
  933. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));
  934. // dts genration task
  935. let dtsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".d.ts"))
  936. let dtsTask = gulp.src(dtsFiles)
  937. .pipe(concat("index.d.ts"))
  938. .pipe(replace(/declare module BABYLON {/g, `declare module 'babylonjs/${moduleName}' {`))
  939. .pipe(replace(/\ninterface /g, `\nexport interface `))
  940. .pipe(dtsModuleSupport(moduleName, true, declared, perFile, dependencyTree))
  941. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));
  942. */
  943. tasks.push(commonJsTask, es6Tasks);
  944. });
  945. // run da tasks man!
  946. return merge2(tasks);
  947. }));
  948. /**
  949. * Generate the TypeDoc JSON output in order to create code metadata.
  950. */
  951. gulp.task("typedoc-generate", function() {
  952. return gulp
  953. .src([
  954. "../../dist/preview release/babylon.d.ts",
  955. "../../dist/preview release/gui/babylon.gui.d.ts",
  956. "../../dist/preview release/loaders/babylon.glTF2FileLoader.d.ts",
  957. "../../dist/preview release/serializers/babylon.glTF2Serializer.d.ts",
  958. "../../dist/preview release/glTF2Interface/babylon.glTF2Interface.d.ts"])
  959. .pipe(typedoc({
  960. // TypeScript options (see typescript docs)
  961. mode: "modules",
  962. module: "commonjs",
  963. target: "es5",
  964. includeDeclarations: true,
  965. // Output options (see typedoc docs)
  966. json: config.build.typedocJSON,
  967. // TypeDoc options (see typedoc docs)
  968. ignoreCompilerErrors: true,
  969. readme: "none",
  970. excludeExternals: true,
  971. excludePrivate: true,
  972. excludeProtected: true,
  973. entryPoint: ["\"babylon.d\"", "BABYLON"]
  974. }));
  975. });
  976. /**
  977. * Validate the TypeDoc JSON output against the current baselin to ensure our code is correctly documented.
  978. * (in the newly introduced areas)
  979. */
  980. gulp.task("typedoc-validate", function() {
  981. return gulp.src(config.build.typedocJSON)
  982. .pipe(validateTypedoc(config.build.typedocValidationBaseline, "BABYLON", true, false));
  983. });
  984. /**
  985. * Generate the validation reference to ensure our code is correctly documented.
  986. */
  987. gulp.task("typedoc-generateValidationBaseline", function() {
  988. return gulp.src(config.build.typedocJSON)
  989. .pipe(validateTypedoc(config.build.typedocValidationBaseline, "BABYLON", true, true));
  990. });
  991. /**
  992. * Validate the code comments and style case convention through typedoc and
  993. * generate the new baseline.
  994. */
  995. gulp.task("typedoc-all", gulp.series("typedoc-generate", "typedoc-validate", "typedoc-generateValidationBaseline"));
  996. /**
  997. * Validate compile the code and check the comments and style case convention through typedoc
  998. */
  999. gulp.task("typedoc-check", gulp.series("typescript-compile", "gui", "loaders", "serializers", "typedoc-generate", "typedoc-validate"));
  1000. /**
  1001. * Launches the KARMA validation tests in chrome in order to debug them.
  1002. * (Can only be launch locally.)
  1003. */
  1004. gulp.task("tests-validation-karma", function(done) {
  1005. var kamaServerOptions = {
  1006. configFile: __dirname + "/../../tests/validation/karma.conf.js",
  1007. singleRun: false
  1008. };
  1009. var server = new karmaServer(kamaServerOptions, done);
  1010. server.start();
  1011. });
  1012. /**
  1013. * Launches the KARMA validation tests in ff or virtual screen ff on travis for a quick analysis during the build.
  1014. * (Can only be launch on any branches.)
  1015. */
  1016. gulp.task("tests-validation-virtualscreen", function(done) {
  1017. var kamaServerOptions = {
  1018. configFile: __dirname + "/../../tests/validation/karma.conf.js",
  1019. singleRun: true,
  1020. browsers: ['Firefox']
  1021. };
  1022. var server = new karmaServer(kamaServerOptions, done);
  1023. server.start();
  1024. });
  1025. /**
  1026. * Launches the KARMA validation tests in browser stack for remote and cross devices validation tests.
  1027. * (Can only be launch from secure branches.)
  1028. */
  1029. gulp.task("tests-validation-browserstack", function(done) {
  1030. if (!process.env.BROWSER_STACK_USERNAME) {
  1031. done();
  1032. return;
  1033. }
  1034. var kamaServerOptions = {
  1035. configFile: __dirname + "/../../tests/validation/karma.conf.browserstack.js",
  1036. singleRun: true
  1037. };
  1038. var server = new karmaServer(kamaServerOptions, done);
  1039. server.start();
  1040. });
  1041. /**
  1042. * Transpiles typescript unit tests.
  1043. */
  1044. gulp.task("tests-unit-transpile", function(done) {
  1045. var tsProject = typescript.createProject('../../tests/unit/tsconfig.json');
  1046. var tsResult = gulp.src("../../tests/unit/**/*.ts", { base: "../../" })
  1047. .pipe(tsProject());
  1048. tsResult.once("error", function() {
  1049. tsResult.once("finish", function() {
  1050. console.log("Typescript compile failed");
  1051. process.exit(1);
  1052. });
  1053. });
  1054. return tsResult.js.pipe(gulp.dest("../../"));
  1055. });
  1056. /**
  1057. * Launches the KARMA unit tests in phantomJS.
  1058. * (Can only be launch on any branches.)
  1059. */
  1060. gulp.task("tests-unit-debug", gulp.series("tests-unit-transpile", function(done) {
  1061. var kamaServerOptions = {
  1062. configFile: __dirname + "/../../tests/unit/karma.conf.js",
  1063. singleRun: false,
  1064. browsers: ['Chrome']
  1065. };
  1066. var server = new karmaServer(kamaServerOptions, done);
  1067. server.start();
  1068. }));
  1069. gulp.task("tests-babylon-unit", gulp.series("tests-unit-transpile", function(done) {
  1070. var kamaServerOptions = {
  1071. configFile: __dirname + "/../../tests/unit/karma.conf.js",
  1072. singleRun: true
  1073. };
  1074. var server = new karmaServer(kamaServerOptions, done);
  1075. server.start();
  1076. }));
  1077. var rmDir = function(dirPath) {
  1078. try { var files = fs.readdirSync(dirPath); }
  1079. catch (e) { return; }
  1080. if (files.length > 0)
  1081. for (var i = 0; i < files.length; i++) {
  1082. var filePath = dirPath + '/' + files[i];
  1083. if (fs.statSync(filePath).isFile())
  1084. fs.unlinkSync(filePath);
  1085. else
  1086. rmDir(filePath);
  1087. }
  1088. fs.rmdirSync(dirPath);
  1089. };
  1090. /**
  1091. * Transpiles viewer typescript unit tests.
  1092. */
  1093. gulp.task("tests-viewer-validation-transpile", function() {
  1094. let wpBuild = webpackStream(require('../../Viewer/webpack.gulp.config.js'), webpack);
  1095. // clean the built directory
  1096. rmDir("../../Viewer/tests/build/");
  1097. return wpBuild
  1098. .pipe(rename(function(path) {
  1099. if (path.extname === '.js') {
  1100. path.basename = "test";
  1101. }
  1102. }))
  1103. .pipe(gulp.dest("../../Viewer/tests/build/"));
  1104. });
  1105. /**
  1106. * Launches the viewer's KARMA validation tests in chrome in order to debug them.
  1107. * (Can only be launch locally.)
  1108. */
  1109. gulp.task("tests-viewer-validation-karma", gulp.series("tests-viewer-validation-transpile", function(done) {
  1110. var kamaServerOptions = {
  1111. configFile: __dirname + "/../../Viewer/tests/validation/karma.conf.js",
  1112. singleRun: false
  1113. };
  1114. var server = new karmaServer(kamaServerOptions, done);
  1115. server.start();
  1116. }));
  1117. /**
  1118. * Launches the KARMA validation tests in ff or virtual screen ff on travis for a quick analysis during the build.
  1119. * (Can only be launch on any branches.)
  1120. */
  1121. gulp.task("tests-viewer-validation-virtualscreen", gulp.series("tests-viewer-validation-transpile", function(done) {
  1122. var kamaServerOptions = {
  1123. configFile: __dirname + "/../../Viewer/tests/validation/karma.conf.js",
  1124. singleRun: true,
  1125. browsers: ['Firefox']
  1126. };
  1127. var server = new karmaServer(kamaServerOptions, done);
  1128. server.start();
  1129. }));
  1130. /**
  1131. * Launches the KARMA validation tests in browser stack for remote and cross devices validation tests.
  1132. * (Can only be launch from secure branches.)
  1133. */
  1134. gulp.task("tests-viewer-validation-browserstack", gulp.series("tests-viewer-validation-transpile", function(done) {
  1135. if (!process.env.BROWSER_STACK_USERNAME) {
  1136. done();
  1137. return;
  1138. }
  1139. var kamaServerOptions = {
  1140. configFile: __dirname + "/../../Viewer/tests/validation/karma.conf.browserstack.js",
  1141. singleRun: true
  1142. };
  1143. var server = new karmaServer(kamaServerOptions, done);
  1144. server.start();
  1145. }));
  1146. /**
  1147. * Transpiles viewer typescript unit tests.
  1148. */
  1149. gulp.task("tests-viewer-transpile", function() {
  1150. let wpBuild = webpackStream(require('../../Viewer/tests/unit/webpack.config.js'), webpack);
  1151. // clean the built directory
  1152. rmDir("../../Viewer/tests/build/");
  1153. return wpBuild
  1154. .pipe(rename(function(path) {
  1155. if (path.extname === '.js') {
  1156. path.basename = "test";
  1157. }
  1158. }))
  1159. .pipe(gulp.dest("../../Viewer/tests/build/"));
  1160. });
  1161. /**
  1162. * Launches the KARMA unit tests in chrome.
  1163. * (Can be launch on any branches.)
  1164. */
  1165. gulp.task("tests-viewer-unit-debug", gulp.series("tests-viewer-transpile", function(done) {
  1166. var kamaServerOptions = {
  1167. configFile: __dirname + "/../../Viewer/tests/karma.conf.js",
  1168. singleRun: false,
  1169. browsers: ['Chrome']
  1170. };
  1171. var server = new karmaServer(kamaServerOptions, done);
  1172. server.start();
  1173. }));
  1174. /**
  1175. * Launches the KARMA unit tests in phantomJS.
  1176. * (Can be launch on any branches.)
  1177. */
  1178. gulp.task("tests-viewer-unit", gulp.series("tests-viewer-transpile", function(done) {
  1179. var kamaServerOptions = {
  1180. configFile: __dirname + "/../../Viewer/tests/karma.conf.js",
  1181. singleRun: true
  1182. };
  1183. var server = new karmaServer(kamaServerOptions, done);
  1184. server.start();
  1185. }));
  1186. /**
  1187. * Launches the KARMA unit tests in phantomJS.
  1188. * (Can only be launch on any branches.)
  1189. */
  1190. gulp.task("tests-unit", gulp.series("tests-babylon-unit", "tests-viewer-unit"));
  1191. gulp.task("tests-modules", function() {
  1192. let testsToRun = require('../../tests/modules/tests.json');
  1193. let sequencePromise = Promise.resolve();
  1194. testsToRun.tests.forEach(test => {
  1195. sequencePromise = sequencePromise.then(() => {
  1196. console.log("Running " + test.name);
  1197. let basePath = '../../tests/modules/' + test.name + '/';
  1198. rmDir("../../tests/modules/build/");
  1199. let compilePromise = Promise.resolve();
  1200. if (test.dependencies) {
  1201. compilePromise = new Promise(function(resolve, reject) {
  1202. let counter = 0;
  1203. let copyTask = gulp.src(test.dependencies.map(dep => config.build.outputDirectory + '/' + dep)).pipe(rename(function(path) {
  1204. path.basename = (counter++) + '';
  1205. })).pipe(gulp.dest("../../tests/modules/build/dependencies/"))
  1206. copyTask.once("finish", resolve);
  1207. })
  1208. }
  1209. // any compilation needed?
  1210. if (test.typescript || test.bundler) {
  1211. //typescript only
  1212. if (test.typescript && !test.bundler) {
  1213. compilePromise = compilePromise.then(() => {
  1214. return new Promise(function(resolve, reject) {
  1215. var tsProject = typescript.createProject(basePath + (test.tsconfig || 'tsconfig.json'));
  1216. var tsResult = gulp.src(basePath + '/src/**/*.ts', { base: basePath })
  1217. .pipe(tsProject());
  1218. let error = false;
  1219. tsResult.once("error", function() {
  1220. error = true;
  1221. });
  1222. let jsPipe = tsResult.js.pipe(gulp.dest("../../tests/modules/"));
  1223. jsPipe.once("finish", function() {
  1224. if (error)
  1225. reject('error compiling test');
  1226. else
  1227. resolve();
  1228. });
  1229. });
  1230. });
  1231. } else {
  1232. if (test.bundler === 'webpack') {
  1233. console.log("webpack");
  1234. compilePromise = compilePromise.then(() => {
  1235. return new Promise(function(resolve, reject) {
  1236. let wpBuild = webpackStream(require(basePath + '/webpack.config.js'), webpack);
  1237. wpBuild = wpBuild
  1238. .pipe(rename(function(path) {
  1239. if (path.extname === '.js') {
  1240. path.basename = "tests-loader";
  1241. }
  1242. }))
  1243. .pipe(gulp.dest("../../tests/modules/build/"));
  1244. wpBuild.once("finish", resolve);
  1245. })
  1246. });
  1247. }
  1248. }
  1249. }
  1250. return compilePromise.then(() => {
  1251. return new Promise(function(resolve, reject) {
  1252. var kamaServerOptions = {
  1253. configFile: __dirname + "/../../tests/modules/karma.conf.js",
  1254. singleRun: true
  1255. };
  1256. var server = new karmaServer(kamaServerOptions, resolve);
  1257. server.start();
  1258. });
  1259. })
  1260. })
  1261. });
  1262. return sequencePromise;
  1263. });
  1264. gulp.task("tests-whatsnew", function(done) {
  1265. // Only checks on Travis
  1266. if (!process.env.TRAVIS) {
  1267. done();
  1268. return;
  1269. }
  1270. // Only checks on Pull Requests
  1271. if (process.env.TRAVIS_PULL_REQUEST == "false") {
  1272. done();
  1273. return;
  1274. }
  1275. // Do not check deploy
  1276. if (process.env.TRAVIS_BRANCH == "preview") {
  1277. done();
  1278. return;
  1279. }
  1280. // Compare what's new with the current one in the preview release folder.
  1281. const https = require("https");
  1282. const url = "https://rawgit.com/BabylonJS/Babylon.js/master/dist/preview%20release/what's%20new.md";
  1283. https.get(url, res => {
  1284. res.setEncoding("utf8");
  1285. let oldData = "";
  1286. res.on("data", data => {
  1287. oldData += data;
  1288. });
  1289. res.on("end", () => {
  1290. fs.readFile("../../dist/preview release/what's new.md", "utf-8", function(err, newData) {
  1291. if (err || oldData != newData) {
  1292. done();
  1293. return;
  1294. }
  1295. console.error("What's new file did not change.");
  1296. process.exit(1);
  1297. });
  1298. });
  1299. });
  1300. });
  1301. /**
  1302. * Do it all.
  1303. */
  1304. gulp.task("typescript-all", gulp.series("typescript", "typescript-libraries", "netlify-cleanup"));
  1305. /**
  1306. * The default task, concat and min the main BJS files.
  1307. */
  1308. gulp.task("default", gulp.series("tsLint", "typescript-all", "intellisense", "typedoc-all", "tests-unit", "tests-modules", "tests-validation-virtualscreen", "tests-validation-browserstack"));