gulpfile.js 54 KB

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