contexts.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. var contexts = {};
  23. exports.default = contexts;
  24. var Constants = __importStar(require("./constants"));
  25. var copyFromOriginal = function copyFromOriginal(original, destination, propertiesToCopy) {
  26. if (!original) {
  27. return;
  28. }
  29. for (var i = 0; i < propertiesToCopy.length; i++) {
  30. if (original.hasOwnProperty(propertiesToCopy[i])) {
  31. destination[propertiesToCopy[i]] = original[propertiesToCopy[i]];
  32. }
  33. }
  34. };
  35. /*
  36. parse is used whilst parsing
  37. */
  38. var parseCopyProperties = [
  39. // options
  40. 'paths',
  41. 'rewriteUrls',
  42. 'rootpath',
  43. 'strictImports',
  44. 'insecure',
  45. 'dumpLineNumbers',
  46. 'compress',
  47. 'syncImport',
  48. 'chunkInput',
  49. 'mime',
  50. 'useFileCache',
  51. // context
  52. 'processImports',
  53. // Used by the import manager to stop multiple import visitors being created.
  54. 'pluginManager' // Used as the plugin manager for the session
  55. ];
  56. contexts.Parse = function (options) {
  57. copyFromOriginal(options, this, parseCopyProperties);
  58. if (typeof this.paths === 'string') {
  59. this.paths = [this.paths];
  60. }
  61. };
  62. var evalCopyProperties = [
  63. 'paths',
  64. 'compress',
  65. 'math',
  66. 'strictUnits',
  67. 'sourceMap',
  68. 'importMultiple',
  69. 'urlArgs',
  70. 'javascriptEnabled',
  71. 'pluginManager',
  72. 'importantScope',
  73. 'rewriteUrls' // option - whether to adjust URL's to be relative
  74. ];
  75. function isPathRelative(path) {
  76. return !/^(?:[a-z-]+:|\/|#)/i.test(path);
  77. }
  78. function isPathLocalRelative(path) {
  79. return path.charAt(0) === '.';
  80. }
  81. contexts.Eval = /** @class */ (function () {
  82. function Eval(options, frames) {
  83. copyFromOriginal(options, this, evalCopyProperties);
  84. if (typeof this.paths === 'string') {
  85. this.paths = [this.paths];
  86. }
  87. this.frames = frames || [];
  88. this.importantScope = this.importantScope || [];
  89. this.inCalc = false;
  90. this.mathOn = true;
  91. }
  92. Eval.prototype.enterCalc = function () {
  93. if (!this.calcStack) {
  94. this.calcStack = [];
  95. }
  96. this.calcStack.push(true);
  97. this.inCalc = true;
  98. };
  99. Eval.prototype.exitCalc = function () {
  100. this.calcStack.pop();
  101. if (!this.calcStack.length) {
  102. this.inCalc = false;
  103. }
  104. };
  105. Eval.prototype.inParenthesis = function () {
  106. if (!this.parensStack) {
  107. this.parensStack = [];
  108. }
  109. this.parensStack.push(true);
  110. };
  111. ;
  112. Eval.prototype.outOfParenthesis = function () {
  113. this.parensStack.pop();
  114. };
  115. ;
  116. Eval.prototype.isMathOn = function (op) {
  117. if (!this.mathOn) {
  118. return false;
  119. }
  120. if (op === '/' && this.math !== Constants.Math.ALWAYS && (!this.parensStack || !this.parensStack.length)) {
  121. return false;
  122. }
  123. if (this.math > Constants.Math.PARENS_DIVISION) {
  124. return this.parensStack && this.parensStack.length;
  125. }
  126. return true;
  127. };
  128. Eval.prototype.pathRequiresRewrite = function (path) {
  129. var isRelative = this.rewriteUrls === Constants.RewriteUrls.LOCAL ? isPathLocalRelative : isPathRelative;
  130. return isRelative(path);
  131. };
  132. Eval.prototype.rewritePath = function (path, rootpath) {
  133. var newPath;
  134. rootpath = rootpath || '';
  135. newPath = this.normalizePath(rootpath + path);
  136. // If a path was explicit relative and the rootpath was not an absolute path
  137. // we must ensure that the new path is also explicit relative.
  138. if (isPathLocalRelative(path) &&
  139. isPathRelative(rootpath) &&
  140. isPathLocalRelative(newPath) === false) {
  141. newPath = "./" + newPath;
  142. }
  143. return newPath;
  144. };
  145. Eval.prototype.normalizePath = function (path) {
  146. var segments = path.split('/').reverse();
  147. var segment;
  148. path = [];
  149. while (segments.length !== 0) {
  150. segment = segments.pop();
  151. switch (segment) {
  152. case '.':
  153. break;
  154. case '..':
  155. if ((path.length === 0) || (path[path.length - 1] === '..')) {
  156. path.push(segment);
  157. }
  158. else {
  159. path.pop();
  160. }
  161. break;
  162. default:
  163. path.push(segment);
  164. break;
  165. }
  166. }
  167. return path.join('/');
  168. };
  169. return Eval;
  170. }());
  171. //# sourceMappingURL=contexts.js.map