gulpfile.js 54 KB

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