gulpfile.js 53 KB

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