gulpfile.js 54 KB

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