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.

263 lines
22 KiB

4 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _lazyResult = _interopRequireDefault(require("./lazy-result"));
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6. /**
  7. * Contains plugins to process CSS. Create one `Processor` instance,
  8. * initialize its plugins, and then use that instance on numerous CSS files.
  9. *
  10. * @example
  11. * const processor = postcss([autoprefixer, precss])
  12. * processor.process(css1).then(result => console.log(result.css))
  13. * processor.process(css2).then(result => console.log(result.css))
  14. */
  15. var Processor =
  16. /*#__PURE__*/
  17. function () {
  18. /**
  19. * @param {Array.<Plugin|pluginFunction>|Processor} plugins PostCSS plugins.
  20. * See {@link Processor#use} for plugin format.
  21. */
  22. function Processor(plugins) {
  23. if (plugins === void 0) {
  24. plugins = [];
  25. }
  26. /**
  27. * Current PostCSS version.
  28. *
  29. * @type {string}
  30. *
  31. * @example
  32. * if (result.processor.version.split('.')[0] !== '6') {
  33. * throw new Error('This plugin works only with PostCSS 6')
  34. * }
  35. */
  36. this.version = '7.0.23';
  37. /**
  38. * Plugins added to this processor.
  39. *
  40. * @type {pluginFunction[]}
  41. *
  42. * @example
  43. * const processor = postcss([autoprefixer, precss])
  44. * processor.plugins.length //=> 2
  45. */
  46. this.plugins = this.normalize(plugins);
  47. }
  48. /**
  49. * Adds a plugin to be used as a CSS processor.
  50. *
  51. * PostCSS plugin can be in 4 formats:
  52. * * A plugin created by {@link postcss.plugin} method.
  53. * * A function. PostCSS will pass the function a @{link Root}
  54. * as the first argument and current {@link Result} instance
  55. * as the second.
  56. * * An object with a `postcss` method. PostCSS will use that method
  57. * as described in #2.
  58. * * Another {@link Processor} instance. PostCSS will copy plugins
  59. * from that instance into this one.
  60. *
  61. * Plugins can also be added by passing them as arguments when creating
  62. * a `postcss` instance (see [`postcss(plugins)`]).
  63. *
  64. * Asynchronous plugins should return a `Promise` instance.
  65. *
  66. * @param {Plugin|pluginFunction|Processor} plugin PostCSS plugin
  67. * or {@link Processor}
  68. * with plugins.
  69. *
  70. * @example
  71. * const processor = postcss()
  72. * .use(autoprefixer)
  73. * .use(precss)
  74. *
  75. * @return {Processes} Current processor to make methods chain.
  76. */
  77. var _proto = Processor.prototype;
  78. _proto.use = function use(plugin) {
  79. this.plugins = this.plugins.concat(this.normalize([plugin]));
  80. return this;
  81. }
  82. /**
  83. * Parses source CSS and returns a {@link LazyResult} Promise proxy.
  84. * Because some plugins can be asynchronous it doesnt make
  85. * any transformations. Transformations will be applied
  86. * in the {@link LazyResult} methods.
  87. *
  88. * @param {string|toString|Result} css String with input CSS or any object
  89. * with a `toString()` method,
  90. * like a Buffer. Optionally, send
  91. * a {@link Result} instance
  92. * and the processor will take
  93. * the {@link Root} from it.
  94. * @param {processOptions} [opts] Options.
  95. *
  96. * @return {LazyResult} Promise proxy.
  97. *
  98. * @example
  99. * processor.process(css, { from: 'a.css', to: 'a.out.css' })
  100. * .then(result => {
  101. * console.log(result.css)
  102. * })
  103. */
  104. ;
  105. _proto.process = function (_process) {
  106. function process(_x) {
  107. return _process.apply(this, arguments);
  108. }
  109. process.toString = function () {
  110. return _process.toString();
  111. };
  112. return process;
  113. }(function (css, opts) {
  114. if (opts === void 0) {
  115. opts = {};
  116. }
  117. if (this.plugins.length === 0 && opts.parser === opts.stringifier) {
  118. if (process.env.NODE_ENV !== 'production') {
  119. if (typeof console !== 'undefined' && console.warn) {
  120. console.warn('You did not set any plugins, parser, or stringifier. ' + 'Right now, PostCSS does nothing. Pick plugins for your case ' + 'on https://www.postcss.parts/ and use them in postcss.config.js.');
  121. }
  122. }
  123. }
  124. return new _lazyResult.default(this, css, opts);
  125. });
  126. _proto.normalize = function normalize(plugins) {
  127. var normalized = [];
  128. for (var _iterator = plugins, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  129. var _ref;
  130. if (_isArray) {
  131. if (_i >= _iterator.length) break;
  132. _ref = _iterator[_i++];
  133. } else {
  134. _i = _iterator.next();
  135. if (_i.done) break;
  136. _ref = _i.value;
  137. }
  138. var i = _ref;
  139. if (i.postcss) i = i.postcss;
  140. if (typeof i === 'object' && Array.isArray(i.plugins)) {
  141. normalized = normalized.concat(i.plugins);
  142. } else if (typeof i === 'function') {
  143. normalized.push(i);
  144. } else if (typeof i === 'object' && (i.parse || i.stringify)) {
  145. if (process.env.NODE_ENV !== 'production') {
  146. throw new Error('PostCSS syntaxes cannot be used as plugins. Instead, please use ' + 'one of the syntax/parser/stringifier options as outlined ' + 'in your PostCSS runner documentation.');
  147. }
  148. } else {
  149. throw new Error(i + ' is not a PostCSS plugin');
  150. }
  151. }
  152. return normalized;
  153. };
  154. return Processor;
  155. }();
  156. var _default = Processor;
  157. /**
  158. * @callback builder
  159. * @param {string} part Part of generated CSS connected to this node.
  160. * @param {Node} node AST node.
  161. * @param {"start"|"end"} [type] Nodes part type.
  162. */
  163. /**
  164. * @callback parser
  165. *
  166. * @param {string|toString} css String with input CSS or any object
  167. * with toString() method, like a Buffer.
  168. * @param {processOptions} [opts] Options with only `from` and `map` keys.
  169. *
  170. * @return {Root} PostCSS AST
  171. */
  172. /**
  173. * @callback stringifier
  174. *
  175. * @param {Node} node Start node for stringifing. Usually {@link Root}.
  176. * @param {builder} builder Function to concatenate CSS from nodes parts
  177. * or generate string and source map.
  178. *
  179. * @return {void}
  180. */
  181. /**
  182. * @typedef {object} syntax
  183. * @property {parser} parse Function to generate AST by string.
  184. * @property {stringifier} stringify Function to generate string by AST.
  185. */
  186. /**
  187. * @typedef {object} toString
  188. * @property {function} toString
  189. */
  190. /**
  191. * @callback pluginFunction
  192. * @param {Root} root Parsed input CSS.
  193. * @param {Result} result Result to set warnings or check other plugins.
  194. */
  195. /**
  196. * @typedef {object} Plugin
  197. * @property {function} postcss PostCSS plugin function.
  198. */
  199. /**
  200. * @typedef {object} processOptions
  201. * @property {string} from The path of the CSS source file.
  202. * You should always set `from`,
  203. * because it is used in source map
  204. * generation and syntax error messages.
  205. * @property {string} to The path where youll put the output
  206. * CSS file. You should always set `to`
  207. * to generate correct source maps.
  208. * @property {parser} parser Function to generate AST by string.
  209. * @property {stringifier} stringifier Class to generate string by AST.
  210. * @property {syntax} syntax Object with `parse` and `stringify`.
  211. * @property {object} map Source map options.
  212. * @property {boolean} map.inline Does source map should
  213. * be embedded in the output
  214. * CSS as a base64-encoded
  215. * comment.
  216. * @property {string|object|false|function} map.prev Source map content
  217. * from a previous
  218. * processing step
  219. * (for example, Sass).
  220. * PostCSS will try to find
  221. * previous map automatically,
  222. * so you could disable it by
  223. * `false` value.
  224. * @property {boolean} map.sourcesContent Does PostCSS should set
  225. * the origin content to map.
  226. * @property {string|false} map.annotation Does PostCSS should set
  227. * annotation comment to map.
  228. * @property {string} map.from Override `from` in maps
  229. * sources`.
  230. */
  231. exports.default = _default;
  232. module.exports = exports.default;
  233. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInByb2Nlc3Nvci5lczYiXSwibmFtZXMiOlsiUHJvY2Vzc29yIiwicGx1Z2lucyIsInZlcnNpb24iLCJub3JtYWxpemUiLCJ1c2UiLCJwbHVnaW4iLCJjb25jYXQiLCJwcm9jZXNzIiwiY3NzIiwib3B0cyIsImxlbmd0aCIsInBhcnNlciIsInN0cmluZ2lmaWVyIiwiZW52IiwiTk9ERV9FTlYiLCJjb25zb2xlIiwid2FybiIsIkxhenlSZXN1bHQiLCJub3JtYWxpemVkIiwiaSIsInBvc3Rjc3MiLCJBcnJheSIsImlzQXJyYXkiLCJwdXNoIiwicGFyc2UiLCJzdHJpbmdpZnkiLCJFcnJvciJdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQTs7OztBQUVBOzs7Ozs7Ozs7SUFTTUEsUzs7O0FBQ0o7Ozs7QUFJQSxxQkFBYUMsT0FBYixFQUEyQjtBQUFBLFFBQWRBLE9BQWM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