gulpTasks-localRun.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Import Dependencies.
  2. var gulp = require("gulp");
  3. var connect = require("gulp-connect");
  4. var minimist = require("minimist");
  5. var fs = require('fs');
  6. var path = require('path');
  7. // Read the full config.
  8. var config = require("../../Config/config.json");
  9. // Comand line parsing.
  10. var commandLineOptions = minimist(process.argv.slice(2), {
  11. boolean: ["public"]
  12. });
  13. // Skip known extensions.
  14. var skipExtensions = [".js", ".glb", ".gltf", ".bin", ".html", ".gif", ".jpg", ".jpeg", ".png", ".dds", ".babylon", "ktx", ".map"];
  15. /**
  16. * Embedded webserver for test convenience.
  17. */
  18. gulp.task("webserver", function () {
  19. var rootRelativePath = "../../";
  20. var options = {
  21. root: rootRelativePath,
  22. port: 1338,
  23. livereload: false,
  24. middleware: function (connect, opt) {
  25. return [function (req, res, next) {
  26. const baseUrl = (req.url.indexOf('dist') !== -1 || req.url.indexOf('Tools') !== -1 || req.url.indexOf('temp/') !== -1);
  27. let referer = req.headers['referer'];
  28. if (!baseUrl && referer) {
  29. referer = referer.toLowerCase();
  30. if (referer.indexOf('/playground/') !== -1 && req.url.indexOf('/Playground/') === -1) {
  31. req.url = "/Playground/" + req.url;
  32. res.writeHead(301, {
  33. 'Location': req.url
  34. });
  35. return res.end();
  36. }
  37. if (referer.indexOf('/localdev/') !== -1) {
  38. if (!fs.existsSync(rootRelativePath + req.originalUrl)) {
  39. req.url = "/Playground/" + req.url.replace(/localDev/ig, "");
  40. }
  41. }
  42. }
  43. const pgMath = req.url.match(/\/Playground\/pg\/(.*)/);
  44. if (pgMath) {
  45. const isAFile = req.url.indexOf('.') !== -1;
  46. const withRevision = req.url.match(/\/pg\/(.*)\/revision\/(\d*)/);
  47. if (withRevision) {
  48. revision = withRevision[2];
  49. if (isAFile) {
  50. req.url = req.url.replace(/\/pg\/(.*)\/revision\//gi, "/")
  51. } else {
  52. req.url = req.url.replace(/\/pg\/(.*)\/revision\/(\d*)/gi, "/?pg=$1&revision=$2")
  53. }
  54. } else {
  55. if (isAFile) {
  56. req.url = req.url.replace(/\/pg\//gi, "/")
  57. } else {
  58. req.url = req.url.replace(/\/pg\/(.*)/gi, "/?pg=$1")
  59. }
  60. }
  61. }
  62. var extension = path.extname(decodeURIComponent(req.originalUrl));
  63. if (req.originalUrl.indexOf(config.build.localDevES6FolderName) > -1 && skipExtensions.indexOf(extension) === -1) {
  64. // Append .js for es6 modules.
  65. if (!fs.existsSync(rootRelativePath + req.originalUrl)) {
  66. req.url += ".js";
  67. }
  68. }
  69. next();
  70. }]
  71. }
  72. };
  73. if (commandLineOptions.public) {
  74. options.host = "0.0.0.0";
  75. }
  76. if (commandLineOptions.ssl) {
  77. options.https = true;
  78. }
  79. connect.server(options);
  80. });