gulpTasks-localRun.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. if (!baseUrl && req.headers['referer'] && req.headers['referer'].indexOf('/Playground/') && req.url.indexOf('/Playground/') === -1) {
  28. req.url = "/Playground/" + req.url;
  29. res.writeHead(301, {
  30. 'Location': req.url
  31. });
  32. return res.end();
  33. }
  34. const pgMath = req.url.match(/\/Playground\/pg\/(.*)/);
  35. if (pgMath) {
  36. const isAFile = req.url.indexOf('.') !== -1;
  37. const withRevision = req.url.match(/\/pg\/(.*)\/revision\/(\d*)/);
  38. if (withRevision) {
  39. revision = withRevision[2];
  40. if (isAFile) {
  41. req.url = req.url.replace(/\/pg\/(.*)\/revision\//gi, "/")
  42. } else {
  43. req.url = req.url.replace(/\/pg\/(.*)\/revision\/(\d*)/gi, "/?pg=$1&revision=$2")
  44. }
  45. } else {
  46. if (isAFile) {
  47. req.url = req.url.replace(/\/pg\//gi, "/")
  48. } else {
  49. req.url = req.url.replace(/\/pg\/(.*)/gi, "/?pg=$1")
  50. }
  51. }
  52. }
  53. var extension = path.extname(decodeURIComponent(req.originalUrl));
  54. if (req.originalUrl.indexOf(config.build.localDevES6FolderName) > -1 && skipExtensions.indexOf(extension) === -1) {
  55. // Append .js for es6 modules.
  56. if (!fs.existsSync(rootRelativePath + req.originalUrl)) {
  57. req.url += ".js";
  58. }
  59. }
  60. next();
  61. }]
  62. }
  63. };
  64. if (commandLineOptions.public) {
  65. options.host = "0.0.0.0";
  66. }
  67. if (commandLineOptions.ssl) {
  68. options.https = true;
  69. }
  70. connect.server(options);
  71. });