gulpfile.js 52 KB

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