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.

285 lines
19 KiB

4 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _declaration = _interopRequireDefault(require("./declaration"));
  5. var _processor = _interopRequireDefault(require("./processor"));
  6. var _stringify = _interopRequireDefault(require("./stringify"));
  7. var _comment = _interopRequireDefault(require("./comment"));
  8. var _atRule = _interopRequireDefault(require("./at-rule"));
  9. var _vendor = _interopRequireDefault(require("./vendor"));
  10. var _parse = _interopRequireDefault(require("./parse"));
  11. var _list = _interopRequireDefault(require("./list"));
  12. var _rule = _interopRequireDefault(require("./rule"));
  13. var _root = _interopRequireDefault(require("./root"));
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. /**
  16. * Create a new {@link Processor} instance that will apply `plugins`
  17. * as CSS processors.
  18. *
  19. * @param {Array.<Plugin|pluginFunction>|Processor} plugins PostCSS plugins.
  20. * See {@link Processor#use} for plugin format.
  21. *
  22. * @return {Processor} Processor to process multiple CSS.
  23. *
  24. * @example
  25. * import postcss from 'postcss'
  26. *
  27. * postcss(plugins).process(css, { from, to }).then(result => {
  28. * console.log(result.css)
  29. * })
  30. *
  31. * @namespace postcss
  32. */
  33. function postcss() {
  34. for (var _len = arguments.length, plugins = new Array(_len), _key = 0; _key < _len; _key++) {
  35. plugins[_key] = arguments[_key];
  36. }
  37. if (plugins.length === 1 && Array.isArray(plugins[0])) {
  38. plugins = plugins[0];
  39. }
  40. return new _processor.default(plugins);
  41. }
  42. /**
  43. * Creates a PostCSS plugin with a standard API.
  44. *
  45. * The newly-wrapped function will provide both the name and PostCSS
  46. * version of the plugin.
  47. *
  48. * ```js
  49. * const processor = postcss([replace])
  50. * processor.plugins[0].postcssPlugin //=> 'postcss-replace'
  51. * processor.plugins[0].postcssVersion //=> '6.0.0'
  52. * ```
  53. *
  54. * The plugin function receives 2 arguments: {@link Root}
  55. * and {@link Result} instance. The function should mutate the provided
  56. * `Root` node. Alternatively, you can create a new `Root` node
  57. * and override the `result.root` property.
  58. *
  59. * ```js
  60. * const cleaner = postcss.plugin('postcss-cleaner', () => {
  61. * return (root, result) => {
  62. * result.root = postcss.root()
  63. * }
  64. * })
  65. * ```
  66. *
  67. * As a convenience, plugins also expose a `process` method so that you can use
  68. * them as standalone tools.
  69. *
  70. * ```js
  71. * cleaner.process(css, processOpts, pluginOpts)
  72. * // This is equivalent to:
  73. * postcss([ cleaner(pluginOpts) ]).process(css, processOpts)
  74. * ```
  75. *
  76. * Asynchronous plugins should return a `Promise` instance.
  77. *
  78. * ```js
  79. * postcss.plugin('postcss-import', () => {
  80. * return (root, result) => {
  81. * return new Promise( (resolve, reject) => {
  82. * fs.readFile('base.css', (base) => {
  83. * root.prepend(base)
  84. * resolve()
  85. * })
  86. * })
  87. * }
  88. * })
  89. * ```
  90. *
  91. * Add warnings using the {@link Node#warn} method.
  92. * Send data to other plugins using the {@link Result#messages} array.
  93. *
  94. * ```js
  95. * postcss.plugin('postcss-caniuse-test', () => {
  96. * return (root, result) => {
  97. * root.walkDecls(decl => {
  98. * if (!caniuse.support(decl.prop)) {
  99. * decl.warn(result, 'Some browsers do not support ' + decl.prop)
  100. * }
  101. * })
  102. * }
  103. * })
  104. * ```
  105. *
  106. * @param {string} name PostCSS plugin name. Same as in `name`
  107. * property in `package.json`. It will be saved
  108. * in `plugin.postcssPlugin` property.
  109. * @param {function} initializer Will receive plugin options
  110. * and should return {@link pluginFunction}
  111. *
  112. * @return {Plugin} PostCSS plugin.
  113. */
  114. postcss.plugin = function plugin(name, initializer) {
  115. function creator() {
  116. var transformer = initializer.apply(void 0, arguments);
  117. transformer.postcssPlugin = name;
  118. transformer.postcssVersion = new _processor.default().version;
  119. return transformer;
  120. }
  121. var cache;
  122. Object.defineProperty(creator, 'postcss', {
  123. get: function get() {
  124. if (!cache) cache = creator();
  125. return cache;
  126. }
  127. });
  128. creator.process = function (css, processOpts, pluginOpts) {
  129. return postcss([creator(pluginOpts)]).process(css, processOpts);
  130. };
  131. return creator;
  132. };
  133. /**
  134. * Default function to convert a node tree into a CSS string.
  135. *
  136. * @param {Node} node Start node for stringifing. Usually {@link Root}.
  137. * @param {builder} builder Function to concatenate CSS from nodes parts
  138. * or generate string and source map.
  139. *
  140. * @return {void}
  141. *
  142. * @function
  143. */
  144. postcss.stringify = _stringify.default;
  145. /**
  146. * Parses source css and returns a new {@link Root} node,
  147. * which contains the source CSS nodes.
  148. *
  149. * @param {string|toString} css String with input CSS or any object
  150. * with toString() method, like a Buffer
  151. * @param {processOptions} [opts] Options with only `from` and `map` keys.
  152. *
  153. * @return {Root} PostCSS AST.
  154. *
  155. * @example
  156. * // Simple CSS concatenation with source map support
  157. * const root1 = postcss.parse(css1, { from: file1 })
  158. * const root2 = postcss.parse(css2, { from: file2 })
  159. * root1.append(root2).toResult().css
  160. *
  161. * @function
  162. */
  163. postcss.parse = _parse.default;
  164. /**
  165. * Contains the {@link vendor} module.
  166. *
  167. * @type {vendor}
  168. *
  169. * @example
  170. * postcss.vendor.unprefixed('-moz-tab') //=> ['tab']
  171. */
  172. postcss.vendor = _vendor.default;
  173. /**
  174. * Contains the {@link list} module.
  175. *
  176. * @member {list}
  177. *
  178. * @example
  179. * postcss.list.space('5px calc(10% + 5px)') //=> ['5px', 'calc(10% + 5px)']
  180. */
  181. postcss.list = _list.default;
  182. /**
  183. * Creates a new {@link Comment} node.
  184. *
  185. * @param {object} [defaults] Properties for the new node.
  186. *
  187. * @return {Comment} New comment node
  188. *
  189. * @example
  190. * postcss.comment({ text: 'test' })
  191. */
  192. postcss.comment = function (defaults) {
  193. return new _comment.default(defaults);
  194. };
  195. /**
  196. * Creates a new {@link AtRule} node.
  197. *
  198. * @param {object} [defaults] Properties for the new node.
  199. *
  200. * @return {AtRule} new at-rule node
  201. *
  202. * @example
  203. * postcss.atRule({ name: 'charset' }).toString() //=> "@charset"
  204. */
  205. postcss.atRule = function (defaults) {
  206. return new _atRule.default(defaults);
  207. };
  208. /**
  209. * Creates a new {@link Declaration} node.
  210. *
  211. * @param {object} [defaults] Properties for the new node.
  212. *
  213. * @return {Declaration} new declaration node
  214. *
  215. * @example
  216. * postcss.decl({ prop: 'color', value: 'red' }).toString() //=> "color: red"
  217. */
  218. postcss.decl = function (defaults) {
  219. return new _declaration.default(defaults);
  220. };
  221. /**
  222. * Creates a new {@link Rule} node.
  223. *
  224. * @param {object} [defaults] Properties for the new node.
  225. *
  226. * @return {Rule} new rule node
  227. *
  228. * @example
  229. * postcss.rule({ selector: 'a' }).toString() //=> "a {\n}"
  230. */
  231. postcss.rule = function (defaults) {
  232. return new _rule.default(defaults);
  233. };
  234. /**
  235. * Creates a new {@link Root} node.
  236. *
  237. * @param {object} [defaults] Properties for the new node.
  238. *
  239. * @return {Root} new root node.
  240. *
  241. * @example
  242. * postcss.root({ after: '\n' }).toString() //=> "\n"
  243. */
  244. postcss.root = function (defaults) {
  245. return new _root.default(defaults);
  246. };
  247. var _default = postcss;
  248. exports.default = _default;
  249. module.exports = exports.default;
  250. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInBvc3Rjc3MuZXM2Il0sIm5hbWVzIjpbInBvc3Rjc3MiLCJwbHVnaW5zIiwibGVuZ3RoIiwiQXJyYXkiLCJpc0FycmF5IiwiUHJvY2Vzc29yIiwicGx1Z2luIiwibmFtZSIsImluaXRpYWxpemVyIiwiY3JlYXRvciIsInRyYW5zZm9ybWVyIiwicG9zdGNzc1BsdWdpbiIsInBvc3Rjc3NWZXJzaW9uIiwidmVyc2lvbiIsImNhY2hlIiwiT2JqZWN0IiwiZGVmaW5lUHJvcGVydHkiLCJnZXQiLCJwcm9jZXNzIiwiY3NzIiwicHJvY2Vzc09wdHMiLCJwbHVnaW5PcHRzIiwic3RyaW5naWZ5IiwicGFyc2UiLCJ2ZW5kb3IiLCJsaXN0IiwiY29tbWVudCIsImRlZmF1bHRzIiwiQ29tbWVudCIsImF0UnVs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