gulpTasks-localRun.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. let referer = req.headers['referer'];
  27. const baseUrl = ((req.url.indexOf('dist/preview') !== -1) || req.url.indexOf('Tools') !== -1 || req.url.indexOf('temp/') !== -1);
  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 && referer.indexOf(req.originalUrl) === -1) {
  38. if (!fs.existsSync(rootRelativePath + req.originalUrl)) {
  39. req.url = "/Playground/" + req.url.replace(/localDev/ig, "");
  40. }
  41. }
  42. if (referer.indexOf('/localdevwebgpu/') !== -1 && referer.indexOf(req.originalUrl) === -1) {
  43. if (!fs.existsSync(rootRelativePath + req.originalUrl)) {
  44. req.url = "/Playground/" + req.url.replace(/localdevwebgpu/ig, "");
  45. }
  46. }
  47. }
  48. const pgMath = req.url.match(/\/Playground\/pg\/(.*)/);
  49. if (pgMath) {
  50. const isAFile = req.url.indexOf('.') !== -1;
  51. const withRevision = req.url.match(/\/pg\/(.*)\/revision\/(\d*)/);
  52. if (withRevision) {
  53. revision = withRevision[2];
  54. if (isAFile) {
  55. req.url = req.url.replace(/\/pg\/(.*)\/revision\//gi, "/")
  56. } else {
  57. req.url = req.url.replace(/\/pg\/(.*)\/revision\/(\d*)/gi, "/?pg=$1&revision=$2")
  58. }
  59. } else {
  60. if (isAFile) {
  61. req.url = req.url.replace(/\/pg\//gi, "/")
  62. } else {
  63. req.url = req.url.replace(/\/pg\/(.*)/gi, "/?pg=$1")
  64. }
  65. }
  66. }
  67. var extension = path.extname(decodeURIComponent(req.originalUrl));
  68. if (req.originalUrl.indexOf(config.build.localDevES6FolderName) > -1 && skipExtensions.indexOf(extension) === -1) {
  69. // Append .js for es6 modules.
  70. if (!fs.existsSync(rootRelativePath + req.originalUrl)) {
  71. req.url += ".js";
  72. }
  73. }
  74. next();
  75. }]
  76. }
  77. };
  78. if (commandLineOptions.public) {
  79. options.host = "0.0.0.0";
  80. }
  81. if (commandLineOptions.ssl) {
  82. options.https = true;
  83. }
  84. connect.server(options);
  85. });