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