gulpfile.js 39 KB

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