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