gulpfile.js 52 KB

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