gulpfile.js 39 KB

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