gulpfile.js 42 KB

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