gulpfile.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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 replace = require("gulp-replace");
  20. var uncommentShader = require("./gulp-removeShaderComments");
  21. var expect = require("gulp-expect-file");
  22. var optimisejs = require("gulp-optimize-js");
  23. var webserver = require("gulp-webserver");
  24. var path = require("path");
  25. var sass = require("gulp-sass");
  26. const webpack = require('webpack');
  27. var webpackStream = require("webpack-stream");
  28. var typedoc = require("gulp-typedoc");
  29. var validateTypedoc = require("./gulp-validateTypedoc");
  30. var fs = require("fs");
  31. var dtsBundle = require('dts-bundle');
  32. const through = require('through2');
  33. var karmaServer = require('karma').Server;
  34. //viewer declaration
  35. var processDeclaration = require('./processViewerDeclaration');
  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", gulp.series("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", gulp.series("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", gulp.series("shaders", function build() {
  208. var filesToProcess = determineFilesToProcess("files");
  209. var directFilesToProcess = determineFilesToProcess("directFiles");
  210. let merged = [gulp.src(filesToProcess).
  211. pipe(expect.real({ errorOnFailure: true }, filesToProcess)),
  212. shadersStream,
  213. includeShadersStream];
  214. if (directFilesToProcess.length) {
  215. merged.push(gulp.src(directFilesToProcess));
  216. }
  217. let mergedStreams = merge2(merged);
  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;
  325. if (library.files && library.files.length) {
  326. tsProcess = gulp.src(library.files, { base: settings.build.srcOutputDirectory })
  327. .pipe(sourcemaps.init())
  328. .pipe(typescript(externalTsConfig));
  329. }
  330. let tasks = [];
  331. let shaderTask;
  332. let shadersIndlueTask;
  333. if (library.shadersIncludeFiles && library.shadersIncludeFiles.length) {
  334. shadersIndlueTask = gulp.src(library.shadersIncludeFiles, { base: settings.build.srcOutputDirectory })
  335. .pipe(uncommentShader())
  336. .pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, library.output + ".include.fx"))
  337. .pipe(gulp.dest(settings.build.srcOutputDirectory));
  338. tasks.push(shadersIndlueTask);
  339. }
  340. if (library.shaderFiles && library.shaderFiles.length) {
  341. shaderTask = gulp.src(library.shaderFiles, { base: settings.build.srcOutputDirectory })
  342. .pipe(uncommentShader())
  343. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, library.output + ".fx"))
  344. .pipe(gulp.dest(settings.build.srcOutputDirectory));
  345. tasks.push(shaderTask);
  346. }
  347. var dev;
  348. if (tsProcess) {
  349. dev = tsProcess.js
  350. .pipe(sourcemaps.write("./", {
  351. includeContent: false,
  352. sourceRoot: (filePath) => {
  353. return "";
  354. }
  355. })).pipe(gulp.dest(settings.build.srcOutputDirectory));
  356. tasks.push(dev);
  357. }
  358. var outputDirectory = config.build.outputDirectory + settings.build.distOutputDirectory;
  359. let cssTask;
  360. if (library.sassFiles && library.sassFiles.length) {
  361. cssTask = gulp.src(library.sassFiles)
  362. .pipe(sass().on("error", sass.logError))
  363. .pipe(concat(library.output.replace(".js", ".css")))
  364. .pipe(gulp.dest(outputDirectory));
  365. tasks.push(cssTask);
  366. }
  367. if (watch) {
  368. return merge2(tasks);
  369. }
  370. else {
  371. /*if (library.bundle) {
  372. // Don't remove extends and decorate functions
  373. var code = merge2([tsProcess.js, shader, includeShader])
  374. .pipe(concat(library.output));
  375. if (library.buildAsModule) {
  376. code = code.pipe(addModuleExports(library.moduleDeclaration, true))
  377. }
  378. code.pipe(gulp.dest(outputDirectory))
  379. .pipe(cleants())
  380. .pipe(rename({ extname: ".min.js" }))
  381. .pipe(uglify())
  382. .pipe(optimisejs())
  383. .pipe(gulp.dest(outputDirectory));
  384. } else {*/
  385. let currentTasks = [];
  386. if (tsProcess) {
  387. currentTasks.push(tsProcess.js);
  388. }
  389. if (shaderTask) {
  390. currentTasks.push(shaderTask);
  391. }
  392. if (shadersIndlueTask) {
  393. currentTasks.push(shadersIndlueTask);
  394. }
  395. var code;
  396. if (currentTasks.length) {
  397. code = merge2(currentTasks)
  398. .pipe(concat(library.output));
  399. }
  400. if (library.buildAsModule && code) {
  401. code = code.pipe(replace(extendsSearchRegex, ""))
  402. .pipe(replace(decorateSearchRegex, ""))
  403. .pipe(addDecorateAndExtends())
  404. .pipe(addModuleExports(library.moduleDeclaration, { subModule: true, extendsRoot: library.extendsRoot }))
  405. }
  406. if (code) {
  407. code = code.pipe(gulp.dest(outputDirectory))
  408. .pipe(cleants())
  409. .pipe(rename({ extname: ".min.js" }))
  410. .pipe(uglify())
  411. .pipe(optimisejs())
  412. .pipe(gulp.dest(outputDirectory));
  413. /*}*/
  414. }
  415. var dts;
  416. if (tsProcess) {
  417. dts = tsProcess.dts
  418. .pipe(concat(library.output))
  419. .pipe(replace(referenceSearchRegex, ""))
  420. .pipe(rename({ extname: ".d.ts" }))
  421. .pipe(gulp.dest(outputDirectory));
  422. }
  423. var waitAll;
  424. let waitAllTasks = [];
  425. if (cssTask) {
  426. waitAllTasks.push(cssTask);
  427. }
  428. if (dev) {
  429. waitAllTasks.push(dev);
  430. }
  431. if (code) {
  432. waitAllTasks.push(code);
  433. }
  434. if (dts) {
  435. waitAllTasks.push(dts);
  436. }
  437. if (library.buildAsModule && tsProcess) {
  438. var dts2 = tsProcess.dts
  439. .pipe(concat(library.output))
  440. .pipe(replace(referenceSearchRegex, ""))
  441. .pipe(addDtsExport(library.moduleDeclaration, library.moduleName, true, library.extendsRoot, config.build.extraTypesDependencies))
  442. .pipe(rename({ extname: ".module.d.ts" }))
  443. .pipe(gulp.dest(outputDirectory));
  444. waitAllTasks.push(dts2);
  445. }
  446. if (waitAllTasks.length) {
  447. waitAll = merge2(waitAllTasks);
  448. }
  449. if (library.webpack) {
  450. let sequence = [];
  451. if (waitAll) {
  452. sequence.push(waitAll);
  453. }
  454. if (settings.build.outputs) {
  455. settings.build.outputs.forEach(out => {
  456. let wpConfig = require(library.webpack);
  457. if (!out.minified) {
  458. wpConfig.mode = "development";
  459. }
  460. let wpBuild = webpackStream(wpConfig, webpack);
  461. //shoud dtsBundle create the declaration?
  462. if (settings.build.dtsBundle) {
  463. let event = wpBuild
  464. .pipe(through.obj(function (file, enc, cb) {
  465. // only declaration files
  466. const isdts = /\.d\.ts$/.test(file.path);
  467. if (isdts) this.push(file);
  468. cb();
  469. }))
  470. .pipe(gulp.dest(outputDirectory));
  471. // dts-bundle does NOT support (gulp) streams, so files have to be saved and reloaded,
  472. // until I fix it
  473. event.on("end", function () {
  474. // create the file
  475. dtsBundle.bundle(settings.build.dtsBundle);
  476. // prepend the needed reference
  477. let fileLocation = path.join(path.dirname(settings.build.dtsBundle.main), settings.build.dtsBundle.out);
  478. fs.readFile(fileLocation, function (err, data) {
  479. if (err) throw err;
  480. data = (settings.build.dtsBundle.prependText || "") + '\n' + data.toString();
  481. fs.writeFileSync(fileLocation, data);
  482. if (settings.build.processDeclaration) {
  483. var newData = processDeclaration(data, settings.build.processDeclaration);
  484. fs.writeFileSync(fileLocation.replace('.module', ''), newData);
  485. }
  486. });
  487. });
  488. }
  489. let build = wpBuild
  490. .pipe(through.obj(function (file, enc, cb) {
  491. // only pipe js files
  492. const isJs = /\.js$/.test(file.path);
  493. if (isJs) this.push(file);
  494. cb();
  495. }))
  496. .pipe(addModuleExports(library.moduleDeclaration, { subModule: false, extendsRoot: false, externalUsingBabylon: true, noBabylonInit: library.babylonIncluded }));
  497. function processDestination(dest) {
  498. var outputDirectory = config.build.outputDirectory + dest.outputDirectory;
  499. build = build
  500. .pipe(rename(dest.filename.replace(".js", library.noBundleInName ? '.js' : ".bundle.js")))
  501. .pipe(gulp.dest(outputDirectory));
  502. if (library.babylonIncluded && dest.addBabylonDeclaration) {
  503. // include the babylon declaration
  504. if (dest.addBabylonDeclaration === true) {
  505. dest.addBabylonDeclaration = [config.build.declarationFilename];
  506. }
  507. var decsToAdd = dest.addBabylonDeclaration.map(function (dec) {
  508. return config.build.outputDirectory + '/' + dec;
  509. });
  510. sequence.unshift(gulp.src(decsToAdd)
  511. .pipe(rename(function (path) {
  512. path.dirname = '';
  513. }))
  514. .pipe(gulp.dest(outputDirectory)))
  515. }
  516. }
  517. out.destinations.forEach(dest => {
  518. processDestination(dest);
  519. });
  520. sequence.push(build);
  521. });
  522. } else {
  523. let wpBuild = webpackStream(require(library.webpack), webpack);
  524. let buildEvent = wpBuild
  525. .pipe(gulp.dest(outputDirectory))
  526. //back-compat
  527. .pipe(through.obj(function (file, enc, cb) {
  528. // only js files
  529. const isjs = /\.js$/.test(file.path);
  530. if (isjs) this.push(file);
  531. cb();
  532. }))
  533. .pipe(rename(function (path) {
  534. console.log(path.basename);
  535. //path.extname === ".js"
  536. path.basename = path.basename.replace(".min", "")
  537. })).pipe(gulp.dest(outputDirectory));
  538. sequence.push(
  539. buildEvent
  540. );
  541. if (settings.build.dtsBundle || settings.build.processDeclaration) {
  542. buildEvent.on("end", function () {
  543. if (settings.build.dtsBundle) {
  544. dtsBundle.bundle(settings.build.dtsBundle);
  545. } if (settings.build.processDeclaration) {
  546. let fileLocation = path.join(outputDirectory, settings.build.processDeclaration.filename);
  547. fs.readFile(fileLocation, function (err, data) {
  548. if (err) throw err;
  549. var newData = processDeclaration(data, settings.build.processDeclaration);
  550. fs.writeFileSync(fileLocation.replace('.module', ''), newData);
  551. //legacy module support
  552. fs.writeFileSync(fileLocation, data + "\n" + newData);
  553. });
  554. }
  555. });
  556. }
  557. /*if (settings.build.processDeclaration) {
  558. sequence.push(
  559. wpBuild
  560. .pipe(through.obj(function (file, enc, cb) {
  561. // only js files
  562. const isDts = /\.d.ts$/.test(file.path);
  563. file.contents = new Buffer(processDeclaration(file.contents, settings.build.processDeclaration));
  564. if (isDts) this.push(file);
  565. cb();
  566. }))
  567. .pipe(gulp.dest(outputDirectory))
  568. )
  569. }*/
  570. }
  571. return merge2(sequence);
  572. }
  573. else {
  574. return waitAll || Promise.resolve();
  575. }
  576. }
  577. }
  578. gulp.task("mainBuild", gulp.series("buildWorker", "build"));
  579. /**
  580. * Build the releasable files.
  581. */
  582. gulp.task("typescript", gulp.series("typescript-compile", "mainBuild"));
  583. /**
  584. * Dynamic module creation.
  585. */
  586. config.modules.map(function (module) {
  587. gulp.task(module, function () {
  588. return buildExternalLibraries(config[module]);
  589. });
  590. });
  591. gulp.task("typescript-libraries", gulp.series(config.modules));
  592. /**
  593. * Custom build with full path file control; used by profile.html
  594. */
  595. gulp.task("build-custom", gulp.series("typescript-compile", "build"));
  596. /**
  597. * Watch ts files from typescript .
  598. */
  599. gulp.task("srcTscWatch", function () {
  600. // Reuse The TSC CLI from gulp to enable -w.
  601. process.argv[2] = "-w";
  602. process.argv[3] = "-p";
  603. process.argv[4] = "../../src/tsconfig.json";
  604. require("./node_modules/typescript/lib/tsc.js");
  605. return Promise.resolve();
  606. });
  607. /**
  608. * Watch ts files and fire repective tasks.
  609. */
  610. gulp.task("watch", gulp.series("srcTscWatch", function startWatch() {
  611. var interval = 1000;
  612. var tasks = [];
  613. config.modules.map(function (module) {
  614. config[module].libraries.map(function (library) {
  615. if (library.webpack) {
  616. if (library.noWatch) return;
  617. var outputDirectory = config.build.tempDirectory + config[module].build.distOutputDirectory;
  618. let wpconfig = require(library.webpack);
  619. wpconfig.watch = true;
  620. // dev mode and absolute path sourcemaps for debugging
  621. wpconfig.mode = "development";
  622. wpconfig.output.devtoolModuleFilenameTemplate = "[absolute-resource-path]";
  623. //config.stats = "minimal";
  624. tasks.push(webpackStream(wpconfig, webpack).pipe(gulp.dest(outputDirectory)))
  625. } else {
  626. tasks.push(gulp.watch(library.files, { interval: interval }, function () {
  627. console.log(library.output);
  628. return buildExternalLibrary(library, config[module], true)
  629. .pipe(debug());
  630. }));
  631. tasks.push(gulp.watch(library.shaderFiles, { interval: interval }, function () {
  632. console.log(library.output);
  633. return buildExternalLibrary(library, config[module], true)
  634. .pipe(debug())
  635. }));
  636. tasks.push(gulp.watch(library.sassFiles, { interval: interval }, function () {
  637. console.log(library.output);
  638. return buildExternalLibrary(library, config[module], true)
  639. .pipe(debug())
  640. }));
  641. }
  642. });
  643. });
  644. console.log(tasks.length);
  645. return Promise.resolve();
  646. }));
  647. gulp.task("intellisense", function () {
  648. return gulp.src(config.build.intellisenseSources)
  649. .pipe(concat(config.build.intellisenseFile))
  650. .pipe(replace(/^\s+_.*?;/gm, ""))
  651. .pipe(replace(/^\s+_[\S\s]*?}/gm, ""))
  652. .pipe(replace(/^\s*readonly _/gm, "protected readonly _"))
  653. .pipe(replace(/^\s*static _/gm, "private static _"))
  654. .pipe(replace(/^\s*abstract _/gm, ""))
  655. .pipe(gulp.dest(config.build.playgroundDirectory));
  656. });
  657. /**
  658. * Embedded local dev env management.
  659. */
  660. gulp.task("deployLocalDev", function () {
  661. return gulp.src("../../localDev/template/**.*")
  662. .pipe(gulp.dest("../../localDev/src/"));
  663. });
  664. /**
  665. * Embedded webserver for test convenience.
  666. */
  667. gulp.task("webserver", function () {
  668. var options = {
  669. port: 1338,
  670. livereload: false,
  671. };
  672. if (commandLineOptions.public) {
  673. options.host = "0.0.0.0";
  674. }
  675. return gulp.src("../../.").pipe(webserver(options));
  676. });
  677. /**
  678. * Combine Webserver and Watch as long as vscode does not handle multi tasks.
  679. */
  680. gulp.task("run", gulp.series("watch", "webserver"));
  681. /**
  682. * Cleans map and js files from the src folder.
  683. */
  684. gulp.task("clean-JS-MAP", function () {
  685. return del([
  686. "../../src/**/*.js.map", "../../src/**/*.js"
  687. ], { force: true });
  688. });
  689. gulp.task("netlify-cleanup", function () {
  690. //set by netlify
  691. if (process.env.REPOSITORY_URL) {
  692. return del([
  693. "../../inspector/node_modules/**/*", "../../gui/node_modules/**/*",
  694. "../../Viewer/node_modules/**/*"
  695. ], { force: true });
  696. }
  697. else {
  698. return Promise.resolve();
  699. }
  700. })
  701. // this is needed for the modules for the declaration files.
  702. gulp.task("modules-compile", function () {
  703. var tsResult = gulp.src(config.typescript)
  704. .pipe(sourcemaps.init())
  705. .pipe(tsProject());
  706. // If this gulp task is running on travis
  707. if (process.env.TRAVIS) {
  708. tsResult.once("error", function () {
  709. tsResult.once("finish", function () {
  710. console.log("Typescript compile failed");
  711. process.exit(1);
  712. });
  713. });
  714. }
  715. return merge2([
  716. tsResult.dts
  717. .pipe(gulp.dest(config.build.srcOutputDirectory)),
  718. tsResult.js
  719. .pipe(sourcemaps.write("./",
  720. {
  721. includeContent: false,
  722. sourceRoot: (filePath) => {
  723. return "";
  724. }
  725. }))
  726. .pipe(gulp.dest(config.build.srcOutputDirectory))
  727. ]);
  728. });
  729. // this holds the declared objects in each module
  730. let declared = {}
  731. let perFile = {};
  732. let dependencyTree = {};
  733. gulp.task('prepare-for-modules', /*["modules-compile"],*/ function () {
  734. let tasks = [];
  735. Object.keys(config.workloads).forEach((moduleName) => {
  736. let dtsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".d.ts"))
  737. let dtsTask = gulp.src(dtsFiles)
  738. .pipe(dtsModuleSupport(moduleName, false, declared, perFile));
  739. tasks.push(dtsTask);
  740. });
  741. // now calculate internal dependencies in the .ts files!
  742. /*Object.keys(config.workloads).forEach((moduleName) => {
  743. let tsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".ts"))
  744. let depTask = gulp.src(tsFiles)
  745. .pipe(calculateDependencies(moduleName, perFile, dependencyTree));
  746. tasks.push(depTask);
  747. });*/
  748. return merge2(tasks);
  749. });
  750. gulp.task('prepare-dependency-tree', gulp.series("prepare-for-modules", function () {
  751. let tasks = [];
  752. // now calculate internal dependencies in the .ts files!
  753. Object.keys(config.workloads).forEach((moduleName) => {
  754. let tsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".ts"))
  755. let depTask = gulp.src(tsFiles)
  756. .pipe(calculateDependencies(moduleName, perFile, declared, dependencyTree));
  757. tasks.push(depTask);
  758. });
  759. return merge2(tasks);
  760. }));
  761. // generate the modules directory, along with commonjs modules and es6 modules
  762. // Note - the generated modules are UNMINIFIED! The user will choose whether they want to minify or not.
  763. gulp.task("modules", gulp.series("prepare-dependency-tree", function () {
  764. let tasks = [];
  765. Object.keys(config.workloads)
  766. .forEach((moduleName) => {
  767. let shadersFiles = [];
  768. processDependency("shaders", config.workloads[moduleName], shadersFiles, true);
  769. for (var index = 0; index < shadersFiles.length; index++) {
  770. shadersFiles[index] = "../../src/Shaders/" + shadersFiles[index] + ".fx";
  771. }
  772. let shaderIncludeFiles = [];
  773. processDependency("shaderIncludes", config.workloads[moduleName], shaderIncludeFiles, true);
  774. for (var index = 0; index < shaderIncludeFiles.length; index++) {
  775. shaderIncludeFiles[index] = "../../src/Shaders/ShadersInclude/" + shaderIncludeFiles[index] + ".fx";
  776. }
  777. let commonJsTask = merge2([
  778. gulp.src(config.workloads[moduleName].files)
  779. .pipe(replace(extendsSearchRegex, ""))
  780. .pipe(replace(decorateSearchRegex, ""))
  781. .pipe(replace(referenceSearchRegex, ""))
  782. .pipe(replace(/var BABYLON;\n/g, ""))
  783. .pipe(babylonModuleExports(moduleName, dependencyTree, false, perFile, shadersFiles.length, shaderIncludeFiles.length))
  784. .pipe(rename(function (path) {
  785. path.basename = path.basename.split(".").pop()
  786. path.extname = ".js"
  787. })),
  788. gulp.src(shadersFiles)
  789. .pipe(expect.real({ errorOnFailure: true }, shadersFiles))
  790. .pipe(uncommentShader())
  791. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", "commonjs"))
  792. .pipe(rename("shaders.js")),
  793. gulp.src(shaderIncludeFiles)
  794. .pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles))
  795. .pipe(uncommentShader())
  796. .pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", "commonjs"))
  797. .pipe(rename("shaderIncludes.js")),
  798. gulp.src(config.workloads[moduleName].files)
  799. .pipe(concat('index.js'))
  800. .pipe(babylonModuleExports(moduleName, dependencyTree, true, perFile))
  801. ]).pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'))
  802. let es6Tasks = merge2([
  803. gulp.src(config.workloads[moduleName].files)
  804. .pipe(replace(extendsSearchRegex, ""))
  805. .pipe(replace(decorateSearchRegex, ""))
  806. .pipe(replace(referenceSearchRegex, ""))
  807. .pipe(replace(/var BABYLON;\n/g, ""))
  808. .pipe(babylonES6ModuleExports(moduleName, dependencyTree, false, perFile, shadersFiles.length, shaderIncludeFiles.length))
  809. .pipe(rename(function (path) {
  810. path.basename = path.basename.split(".").pop()
  811. path.extname = ".js"
  812. })),
  813. gulp.src(shadersFiles)
  814. .pipe(expect.real({ errorOnFailure: true }, shadersFiles))
  815. .pipe(uncommentShader())
  816. .pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/es6/' + moduleName + ".fx", "es6"))
  817. .pipe(rename("shaders.js")),
  818. gulp.src(shaderIncludeFiles)
  819. .pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles))
  820. .pipe(uncommentShader())
  821. .pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/es6/' + moduleName + ".include.fx", "es6"))
  822. .pipe(rename("shaderIncludes.js")),
  823. gulp.src(config.workloads[moduleName].files)
  824. .pipe(concat('index.js'))
  825. .pipe(babylonES6ModuleExports(moduleName, dependencyTree, true, perFile))
  826. ]).pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/es6/'))
  827. //commonjs js generation task
  828. /*let jsTask = merge2([
  829. gulp.src(config.workloads[moduleName].files),
  830. gulp.src(shadersFiles).
  831. //pipe(expect.real({ errorOnFailure: true }, shadersFiles)).
  832. pipe(uncommentShader()).
  833. pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", true)),
  834. gulp.src(shaderIncludeFiles).
  835. //pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles)).
  836. pipe(uncommentShader()).
  837. pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", true))
  838. ]).pipe(concat('index.js'))
  839. .pipe(replace(extendsSearchRegex, ""))
  840. .pipe(replace(decorateSearchRegex, ""))
  841. .pipe(replace(referenceSearchRegex, ""))
  842. .pipe(babylonModuleExports(moduleName, config.workloads[moduleName].dependUpon))
  843. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));*/
  844. // es6 modules generation task
  845. /*let es6Task = merge2([
  846. gulp.src(config.workloads[moduleName].files),
  847. gulp.src(shadersFiles).
  848. //pipe(expect.real({ errorOnFailure: true }, shadersFiles)).
  849. pipe(uncommentShader()).
  850. pipe(appendSrcToVariable("BABYLON.Effect.ShadersStore", shadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".fx", true)),
  851. gulp.src(shaderIncludeFiles).
  852. //pipe(expect.real({ errorOnFailure: true }, shaderIncludeFiles)).
  853. pipe(uncommentShader()).
  854. pipe(appendSrcToVariable("BABYLON.Effect.IncludesShadersStore", includeShadersName, config.build.outputDirectory + '/commonjs/' + moduleName + ".include.fx", true))
  855. ]).pipe(concat('es6.js'))
  856. .pipe(replace(extendsSearchRegex, ""))
  857. .pipe(replace(decorateSearchRegex, ""))
  858. .pipe(replace(referenceSearchRegex, ""))
  859. .pipe(replace(/var BABYLON;/g, ""))
  860. .pipe(babylonES6ModuleExports(moduleName, config.workloads[moduleName].dependUpon))
  861. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));
  862. // dts genration task
  863. let dtsFiles = config.workloads[moduleName].files.map(f => f.replace(".js", ".d.ts"))
  864. let dtsTask = gulp.src(dtsFiles)
  865. .pipe(concat("index.d.ts"))
  866. .pipe(replace(/declare module BABYLON {/g, `declare module 'babylonjs/${moduleName}' {`))
  867. .pipe(replace(/\ninterface /g, `\nexport interface `))
  868. .pipe(dtsModuleSupport(moduleName, true, declared, perFile, dependencyTree))
  869. .pipe(gulp.dest(config.build.outputDirectory + '/modules/' + moduleName + '/'));
  870. */
  871. tasks.push(commonJsTask, es6Tasks);
  872. });
  873. // run da tasks man!
  874. return merge2(tasks);
  875. }));
  876. /**
  877. * Generate the TypeDoc JSON output in order to create code metadata.
  878. */
  879. gulp.task("typedoc-generate", function () {
  880. return gulp
  881. .src([
  882. "../../dist/preview release/babylon.d.ts",
  883. "../../dist/preview release/gui/babylon.gui.d.ts",
  884. "../../dist/preview release/loaders/babylon.glTF2FileLoader.d.ts",
  885. "../../dist/preview release/serializers/babylon.glTF2Serializer.d.ts",
  886. "../../dist/preview release/glTF2Interface/babylon.glTF2Interface.d.ts"])
  887. .pipe(typedoc({
  888. // TypeScript options (see typescript docs)
  889. mode: "modules",
  890. module: "commonjs",
  891. target: "es5",
  892. includeDeclarations: true,
  893. // Output options (see typedoc docs)
  894. json: config.build.typedocJSON,
  895. // TypeDoc options (see typedoc docs)
  896. ignoreCompilerErrors: true,
  897. readme: "none",
  898. excludeExternals: true,
  899. excludePrivate: true,
  900. excludeProtected: true,
  901. entryPoint: ["\"babylon.d\"", "BABYLON"]
  902. }));
  903. });
  904. /**
  905. * Validate the TypeDoc JSON output against the current baselin to ensure our code is correctly documented.
  906. * (in the newly introduced areas)
  907. */
  908. gulp.task("typedoc-validate", function () {
  909. return gulp.src(config.build.typedocJSON)
  910. .pipe(validateTypedoc(config.build.typedocValidationBaseline, "BABYLON", true, false));
  911. });
  912. /**
  913. * Generate the validation reference to ensure our code is correctly documented.
  914. */
  915. gulp.task("typedoc-generateValidationBaseline", function () {
  916. return gulp.src(config.build.typedocJSON)
  917. .pipe(validateTypedoc(config.build.typedocValidationBaseline, "BABYLON", true, true));
  918. });
  919. /**
  920. * Validate the code comments and style case convention through typedoc and
  921. * generate the new baseline.
  922. */
  923. gulp.task("typedoc-all", gulp.series("typedoc-generate", "typedoc-validate", "typedoc-generateValidationBaseline"));
  924. /**
  925. * Validate compile the code and check the comments and style case convention through typedoc
  926. */
  927. gulp.task("typedoc-check", gulp.series("typescript-compile", "gui", "loaders", "serializers", "typedoc-generate", "typedoc-validate"));
  928. /**
  929. * Launches the KARMA validation tests in chrome in order to debug them.
  930. * (Can only be launch locally.)
  931. */
  932. gulp.task("tests-validation-karma", function (done) {
  933. var kamaServerOptions = {
  934. configFile: __dirname + "/../../tests/validation/karma.conf.js",
  935. singleRun: false
  936. };
  937. var server = new karmaServer(kamaServerOptions, done);
  938. server.start();
  939. });
  940. /**
  941. * Launches the KARMA validation tests in ff or virtual screen ff on travis for a quick analysis during the build.
  942. * (Can only be launch on any branches.)
  943. */
  944. gulp.task("tests-validation-virtualscreen", function (done) {
  945. var kamaServerOptions = {
  946. configFile: __dirname + "/../../tests/validation/karma.conf.js",
  947. singleRun: true,
  948. browsers: ['Firefox']
  949. };
  950. var server = new karmaServer(kamaServerOptions, done);
  951. server.start();
  952. });
  953. /**
  954. * Launches the KARMA validation tests in browser stack for remote and cross devices validation tests.
  955. * (Can only be launch from secure branches.)
  956. */
  957. gulp.task("tests-validation-browserstack", function (done) {
  958. if (!process.env.BROWSER_STACK_USERNAME) {
  959. done();
  960. return;
  961. }
  962. var kamaServerOptions = {
  963. configFile: __dirname + "/../../tests/validation/karma.conf.browserstack.js",
  964. singleRun: true
  965. };
  966. var server = new karmaServer(kamaServerOptions, done);
  967. server.start();
  968. });
  969. /**
  970. * Transpiles typescript unit tests.
  971. */
  972. gulp.task("tests-unit-transpile", function (done) {
  973. var tsProject = typescript.createProject('../../tests/unit/tsconfig.json');
  974. var tsResult = gulp.src("../../tests/unit/**/*.ts", { base: "../../" })
  975. .pipe(tsProject());
  976. tsResult.once("error", function () {
  977. tsResult.once("finish", function () {
  978. console.log("Typescript compile failed");
  979. process.exit(1);
  980. });
  981. });
  982. return tsResult.js.pipe(gulp.dest("../../"));
  983. });
  984. /**
  985. * Launches the KARMA unit tests in phantomJS.
  986. * (Can only be launch on any branches.)
  987. */
  988. gulp.task("tests-unit-debug", gulp.series("tests-unit-transpile", function (done) {
  989. var kamaServerOptions = {
  990. configFile: __dirname + "/../../tests/unit/karma.conf.js",
  991. singleRun: false,
  992. browsers: ['Chrome']
  993. };
  994. var server = new karmaServer(kamaServerOptions, done);
  995. server.start();
  996. }));
  997. gulp.task("tests-babylon-unit", gulp.series("tests-unit-transpile", function (done) {
  998. var kamaServerOptions = {
  999. configFile: __dirname + "/../../tests/unit/karma.conf.js",
  1000. singleRun: true
  1001. };
  1002. var server = new karmaServer(kamaServerOptions, done);
  1003. server.start();
  1004. }));
  1005. var rmDir = function (dirPath) {
  1006. try { var files = fs.readdirSync(dirPath); }
  1007. catch (e) { return; }
  1008. if (files.length > 0)
  1009. for (var i = 0; i < files.length; i++) {
  1010. var filePath = dirPath + '/' + files[i];
  1011. if (fs.statSync(filePath).isFile())
  1012. fs.unlinkSync(filePath);
  1013. else
  1014. rmDir(filePath);
  1015. }
  1016. fs.rmdirSync(dirPath);
  1017. };
  1018. /**
  1019. * Transpiles viewer typescript unit tests.
  1020. */
  1021. gulp.task("tests-viewer-validation-transpile", function () {
  1022. let wpBuild = webpackStream(require('../../Viewer/webpack.gulp.config.js'), webpack);
  1023. // clean the built directory
  1024. rmDir("../../Viewer/tests/build/");
  1025. return wpBuild
  1026. .pipe(rename(function (path) {
  1027. if (path.extname === '.js') {
  1028. path.basename = "test";
  1029. }
  1030. }))
  1031. .pipe(gulp.dest("../../Viewer/tests/build/"));
  1032. });
  1033. /**
  1034. * Launches the viewer's KARMA validation tests in chrome in order to debug them.
  1035. * (Can only be launch locally.)
  1036. */
  1037. gulp.task("tests-viewer-validation-karma", gulp.series("tests-viewer-validation-transpile", function (done) {
  1038. var kamaServerOptions = {
  1039. configFile: __dirname + "/../../Viewer/tests/validation/karma.conf.js",
  1040. singleRun: false
  1041. };
  1042. var server = new karmaServer(kamaServerOptions, done);
  1043. server.start();
  1044. }));
  1045. /**
  1046. * Launches the KARMA validation tests in ff or virtual screen ff on travis for a quick analysis during the build.
  1047. * (Can only be launch on any branches.)
  1048. */
  1049. gulp.task("tests-viewer-validation-virtualscreen", gulp.series("tests-viewer-validation-transpile", function (done) {
  1050. var kamaServerOptions = {
  1051. configFile: __dirname + "/../../Viewer/tests/validation/karma.conf.js",
  1052. singleRun: true,
  1053. browsers: ['Firefox']
  1054. };
  1055. var server = new karmaServer(kamaServerOptions, done);
  1056. server.start();
  1057. }));
  1058. /**
  1059. * Launches the KARMA validation tests in browser stack for remote and cross devices validation tests.
  1060. * (Can only be launch from secure branches.)
  1061. */
  1062. gulp.task("tests-viewer-validation-browserstack", gulp.series("tests-viewer-validation-transpile", function (done) {
  1063. if (!process.env.BROWSER_STACK_USERNAME) {
  1064. done();
  1065. return;
  1066. }
  1067. var kamaServerOptions = {
  1068. configFile: __dirname + "/../../Viewer/tests/validation/karma.conf.browserstack.js",
  1069. singleRun: true
  1070. };
  1071. var server = new karmaServer(kamaServerOptions, done);
  1072. server.start();
  1073. }));
  1074. /**
  1075. * Transpiles viewer typescript unit tests.
  1076. */
  1077. gulp.task("tests-viewer-transpile", function () {
  1078. let wpBuild = webpackStream(require('../../Viewer/tests/unit/webpack.config.js'), webpack);
  1079. // clean the built directory
  1080. rmDir("../../Viewer/tests/build/");
  1081. return wpBuild
  1082. .pipe(rename(function (path) {
  1083. if (path.extname === '.js') {
  1084. path.basename = "test";
  1085. }
  1086. }))
  1087. .pipe(gulp.dest("../../Viewer/tests/build/"));
  1088. });
  1089. /**
  1090. * Launches the KARMA unit tests in chrome.
  1091. * (Can be launch on any branches.)
  1092. */
  1093. gulp.task("tests-viewer-unit-debug", gulp.series("tests-viewer-transpile", function (done) {
  1094. var kamaServerOptions = {
  1095. configFile: __dirname + "/../../Viewer/tests/karma.conf.js",
  1096. singleRun: false,
  1097. browsers: ['Chrome']
  1098. };
  1099. var server = new karmaServer(kamaServerOptions, done);
  1100. server.start();
  1101. }));
  1102. /**
  1103. * Launches the KARMA unit tests in phantomJS.
  1104. * (Can be launch on any branches.)
  1105. */
  1106. gulp.task("tests-viewer-unit", gulp.series("tests-viewer-transpile", function (done) {
  1107. var kamaServerOptions = {
  1108. configFile: __dirname + "/../../Viewer/tests/karma.conf.js",
  1109. singleRun: true
  1110. };
  1111. var server = new karmaServer(kamaServerOptions, done);
  1112. server.start();
  1113. }));
  1114. /**
  1115. * Launches the KARMA unit tests in phantomJS.
  1116. * (Can only be launch on any branches.)
  1117. */
  1118. gulp.task("tests-unit", gulp.series("tests-babylon-unit", "tests-viewer-unit"));
  1119. gulp.task("tests-modules", function () {
  1120. let testsToRun = require('../../tests/modules/tests.json');
  1121. let sequencePromise = Promise.resolve();
  1122. testsToRun.tests.forEach(test => {
  1123. sequencePromise = sequencePromise.then(() => {
  1124. console.log("Running " + test.name);
  1125. let basePath = '../../tests/modules/' + test.name + '/';
  1126. rmDir("../../tests/modules/build/");
  1127. let compilePromise = Promise.resolve();
  1128. if (test.dependencies) {
  1129. compilePromise = new Promise(function (resolve, reject) {
  1130. let counter = 0;
  1131. let copyTask = gulp.src(test.dependencies.map(dep => config.build.outputDirectory + '/' + dep)).pipe(rename(function (path) {
  1132. path.basename = (counter++) + '';
  1133. })).pipe(gulp.dest("../../tests/modules/build/dependencies/"))
  1134. copyTask.once("finish", resolve);
  1135. })
  1136. }
  1137. // any compilation needed?
  1138. if (test.typescript || test.bundler) {
  1139. //typescript only
  1140. if (test.typescript && !test.bundler) {
  1141. compilePromise = compilePromise.then(() => {
  1142. return new Promise(function (resolve, reject) {
  1143. var tsProject = typescript.createProject(basePath + (test.tsconfig || 'tsconfig.json'));
  1144. var tsResult = gulp.src(basePath + '/src/**/*.ts', { base: basePath })
  1145. .pipe(tsProject());
  1146. let error = false;
  1147. tsResult.once("error", function () {
  1148. error = true;
  1149. });
  1150. let jsPipe = tsResult.js.pipe(gulp.dest("../../tests/modules/"));
  1151. jsPipe.once("finish", function () {
  1152. if (error)
  1153. reject('error compiling test');
  1154. else
  1155. resolve();
  1156. });
  1157. });
  1158. });
  1159. } else {
  1160. if (test.bundler === 'webpack') {
  1161. console.log("webpack");
  1162. compilePromise = compilePromise.then(() => {
  1163. return new Promise(function (resolve, reject) {
  1164. let wpBuild = webpackStream(require(basePath + '/webpack.config.js'), webpack);
  1165. wpBuild = wpBuild
  1166. .pipe(rename(function (path) {
  1167. if (path.extname === '.js') {
  1168. path.basename = "tests-loader";
  1169. }
  1170. }))
  1171. .pipe(gulp.dest("../../tests/modules/build/"));
  1172. wpBuild.once("finish", resolve);
  1173. })
  1174. });
  1175. }
  1176. }
  1177. }
  1178. return compilePromise.then(() => {
  1179. return new Promise(function (resolve, reject) {
  1180. var kamaServerOptions = {
  1181. configFile: __dirname + "/../../tests/modules/karma.conf.js",
  1182. singleRun: true
  1183. };
  1184. var server = new karmaServer(kamaServerOptions, resolve);
  1185. server.start();
  1186. });
  1187. })
  1188. })
  1189. });
  1190. return sequencePromise;
  1191. });
  1192. gulp.task("tests-whatsnew", function (done) {
  1193. // Only checks on Travis
  1194. if (!process.env.TRAVIS) {
  1195. done();
  1196. return;
  1197. }
  1198. // Only checks on Pull Requests
  1199. if (process.env.TRAVIS_PULL_REQUEST == "false") {
  1200. done();
  1201. return;
  1202. }
  1203. // Do not check deploy
  1204. if (process.env.TRAVIS_BRANCH == "preview") {
  1205. done();
  1206. return;
  1207. }
  1208. // Compare what's new with the current one in the preview release folder.
  1209. const https = require("https");
  1210. const url = "https://rawgit.com/BabylonJS/Babylon.js/master/dist/preview%20release/what's%20new.md";
  1211. https.get(url, res => {
  1212. res.setEncoding("utf8");
  1213. let oldData = "";
  1214. res.on("data", data => {
  1215. oldData += data;
  1216. });
  1217. res.on("end", () => {
  1218. fs.readFile("../../dist/preview release/what's new.md", "utf-8", function (err, newData) {
  1219. if (err || oldData != newData) {
  1220. done();
  1221. return;
  1222. }
  1223. console.error("What's new file did not change.");
  1224. process.exit(1);
  1225. });
  1226. });
  1227. });
  1228. });
  1229. /**
  1230. * Do it all.
  1231. */
  1232. gulp.task("typescript-all", gulp.series("typescript", "typescript-libraries", "netlify-cleanup"));
  1233. /**
  1234. * The default task, concat and min the main BJS files.
  1235. */
  1236. gulp.task("default", gulp.series("typescript-all", "intellisense", "typedoc-all", "tests-unit", "tests-validation-virtualscreen", "tests-validation-browserstack"));