gulpfile.js 54 KB

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