gulpfile.js 37 KB

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