gulp-remapPaths.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. var through = require('through2');
  3. var path = require('path');
  4. var fs = require('fs');
  5. var PluginError = require('plugin-error');
  6. function modifyPath(str, filePath, opts) {
  7. opts = opts || {};
  8. str += "";
  9. // Start process by extracting all lines.
  10. let lines = str.split('\n');
  11. // Let's go line by line and replace the imports sources by their resolved locations
  12. for (let index = 0; index < lines.length; index++) {
  13. let line = lines[index];
  14. // Replace Static Readonly declaration for UMD TS Version compat
  15. var regexVar = /(.*)import .*"(.*)";/g;
  16. var match = regexVar.exec(line);
  17. if (match) {
  18. // Extract the typescript node based import location
  19. const location = match[2];
  20. let newLocation = location;
  21. // Adds file relative path
  22. const lastSlash = location.lastIndexOf("/");
  23. let pathToTest = location;
  24. if (lastSlash > -1) {
  25. pathToTest = location.slice(lastSlash + 1);
  26. }
  27. if (pathToTest.match(/^[A-Z].*/g)) {
  28. if (!pathToTest.startsWith("I") || pathToTest === "Instrumentation" || pathToTest === "Inputs") {
  29. newLocation += "/index";
  30. }
  31. }
  32. if (!location.startsWith(".")) {
  33. const rel = path.relative(filePath, opts.basePath);
  34. const count = (rel.match(/..\\/g) || []).length;
  35. if (count === 0) {
  36. newLocation = "./" + newLocation;
  37. }
  38. else {
  39. for (let i = 0; i < count; i++) {
  40. newLocation = "../" + newLocation;
  41. }
  42. }
  43. }
  44. // Replace the location by the new one
  45. line = line.replace('"' + location + '"', '"' + newLocation + '"');
  46. }
  47. regexVar = /export \* from "(.*)";/g;
  48. match = regexVar.exec(line);
  49. if (match) {
  50. // Extract the typescript node based import location
  51. const location = match[1];
  52. let newLocation = location;
  53. if (location.match(/\.\/[A-Z].*/g)) {
  54. if (location[2] !== "I" || location === "./Instrumentation" || location === "./Inputs") {
  55. newLocation += "/index";
  56. }
  57. }
  58. // Replace the location by the new one
  59. line = line.replace('"' + location + '"', '"' + newLocation + '"');
  60. }
  61. lines[index] = line;
  62. }
  63. // Recreate the file.
  64. str = lines.join('\n');
  65. return str;
  66. }
  67. function main(options, func) {
  68. return through.obj(function (file, enc, cb) {
  69. if (file.isNull()) {
  70. cb(null, file);
  71. return;
  72. }
  73. if (file.isStream()) {
  74. cb(new PluginError("Modify Import Paths", "Streaming not supported."));
  75. }
  76. file.contents = Buffer.from(func(file.contents.toString(), file.path, options));
  77. this.push(file);
  78. return cb();
  79. });
  80. }
  81. function gulpModifyPath(options) {
  82. return main(options, modifyPath);
  83. }
  84. module.exports = gulpModifyPath;