gulpfile.js 41 KB

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