You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
14 KiB

4 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _warning = _interopRequireDefault(require("./warning"));
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  7. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  8. /**
  9. * Provides the result of the PostCSS transformations.
  10. *
  11. * A Result instance is returned by {@link LazyResult#then}
  12. * or {@link Root#toResult} methods.
  13. *
  14. * @example
  15. * postcss([autoprefixer]).process(css).then(result => {
  16. * console.log(result.css)
  17. * })
  18. *
  19. * @example
  20. * const result2 = postcss.parse(css).toResult()
  21. */
  22. var Result =
  23. /*#__PURE__*/
  24. function () {
  25. /**
  26. * @param {Processor} processor Processor used for this transformation.
  27. * @param {Root} root Root node after all transformations.
  28. * @param {processOptions} opts Options from the {@link Processor#process}
  29. * or {@link Root#toResult}.
  30. */
  31. function Result(processor, root, opts) {
  32. /**
  33. * The Processor instance used for this transformation.
  34. *
  35. * @type {Processor}
  36. *
  37. * @example
  38. * for (const plugin of result.processor.plugins) {
  39. * if (plugin.postcssPlugin === 'postcss-bad') {
  40. * throw 'postcss-good is incompatible with postcss-bad'
  41. * }
  42. * })
  43. */
  44. this.processor = processor;
  45. /**
  46. * Contains messages from plugins (e.g., warnings or custom messages).
  47. * Each message should have type and plugin properties.
  48. *
  49. * @type {Message[]}
  50. *
  51. * @example
  52. * postcss.plugin('postcss-min-browser', () => {
  53. * return (root, result) => {
  54. * const browsers = detectMinBrowsersByCanIUse(root)
  55. * result.messages.push({
  56. * type: 'min-browser',
  57. * plugin: 'postcss-min-browser',
  58. * browsers
  59. * })
  60. * }
  61. * })
  62. */
  63. this.messages = [];
  64. /**
  65. * Root node after all transformations.
  66. *
  67. * @type {Root}
  68. *
  69. * @example
  70. * root.toResult().root === root
  71. */
  72. this.root = root;
  73. /**
  74. * Options from the {@link Processor#process} or {@link Root#toResult} call
  75. * that produced this Result instance.
  76. *
  77. * @type {processOptions}
  78. *
  79. * @example
  80. * root.toResult(opts).opts === opts
  81. */
  82. this.opts = opts;
  83. /**
  84. * A CSS string representing of {@link Result#root}.
  85. *
  86. * @type {string}
  87. *
  88. * @example
  89. * postcss.parse('a{}').toResult().css //=> "a{}"
  90. */
  91. this.css = undefined;
  92. /**
  93. * An instance of `SourceMapGenerator` class from the `source-map` library,
  94. * representing changes to the {@link Result#root} instance.
  95. *
  96. * @type {SourceMapGenerator}
  97. *
  98. * @example
  99. * result.map.toJSON() //=> { version: 3, file: 'a.css', … }
  100. *
  101. * @example
  102. * if (result.map) {
  103. * fs.writeFileSync(result.opts.to + '.map', result.map.toString())
  104. * }
  105. */
  106. this.map = undefined;
  107. }
  108. /**
  109. * Returns for @{link Result#css} content.
  110. *
  111. * @example
  112. * result + '' === result.css
  113. *
  114. * @return {string} String representing of {@link Result#root}.
  115. */
  116. var _proto = Result.prototype;
  117. _proto.toString = function toString() {
  118. return this.css;
  119. }
  120. /**
  121. * Creates an instance of {@link Warning} and adds it
  122. * to {@link Result#messages}.
  123. *
  124. * @param {string} text Warning message.
  125. * @param {Object} [opts] Warning options.
  126. * @param {Node} opts.node CSS node that caused the warning.
  127. * @param {string} opts.word Word in CSS source that caused the warning.
  128. * @param {number} opts.index Index in CSS node string that caused
  129. * the warning.
  130. * @param {string} opts.plugin Name of the plugin that created
  131. * this warning. {@link Result#warn} fills
  132. * this property automatically.
  133. *
  134. * @return {Warning} Created warning.
  135. */
  136. ;
  137. _proto.warn = function warn(text, opts) {
  138. if (opts === void 0) {
  139. opts = {};
  140. }
  141. if (!opts.plugin) {
  142. if (this.lastPlugin && this.lastPlugin.postcssPlugin) {
  143. opts.plugin = this.lastPlugin.postcssPlugin;
  144. }
  145. }
  146. var warning = new _warning.default(text, opts);
  147. this.messages.push(warning);
  148. return warning;
  149. }
  150. /**
  151. * Returns warnings from plugins. Filters {@link Warning} instances
  152. * from {@link Result#messages}.
  153. *
  154. * @example
  155. * result.warnings().forEach(warn => {
  156. * console.warn(warn.toString())
  157. * })
  158. *
  159. * @return {Warning[]} Warnings from plugins.
  160. */
  161. ;
  162. _proto.warnings = function warnings() {
  163. return this.messages.filter(function (i) {
  164. return i.type === 'warning';
  165. });
  166. }
  167. /**
  168. * An alias for the {@link Result#css} property.
  169. * Use it with syntaxes that generate non-CSS output.
  170. *
  171. * @type {string}
  172. *
  173. * @example
  174. * result.css === result.content
  175. */
  176. ;
  177. _createClass(Result, [{
  178. key: "content",
  179. get: function get() {
  180. return this.css;
  181. }
  182. }]);
  183. return Result;
  184. }();
  185. var _default = Result;
  186. /**
  187. * @typedef {object} Message
  188. * @property {string} type Message type.
  189. * @property {string} plugin Source PostCSS plugin name.
  190. */
  191. exports.default = _default;
  192. module.exports = exports.default;
  193. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJlc3VsdC5lczYiXSwibmFtZXMiOlsiUmVzdWx0IiwicHJvY2Vzc29yIiwicm9vdCIsIm9wdHMiLCJtZXNzYWdlcyIsImNzcyIsInVuZGVmaW5lZCIsIm1hcCIsInRvU3RyaW5nIiwid2FybiIsInRleHQiLCJwbHVnaW4iLCJsYXN0UGx1Z2luIiwicG9zdGNzc1BsdWdpbiIsIndhcm5pbmciLCJXYXJuaW5nIiwicHVzaCIsIndhcm5pbmdzIiwiZmlsdGVyIiwiaSIsInR5cGUiXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUE7Ozs7Ozs7O0FBRUE7Ozs7Ozs7Ozs7Ozs7O0lBY01BLE07OztBQUNKOzs7Ozs7QUF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