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.

444 lines
35 KiB

4 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _mapGenerator = _interopRequireDefault(require("./map-generator"));
  5. var _stringify2 = _interopRequireDefault(require("./stringify"));
  6. var _warnOnce = _interopRequireDefault(require("./warn-once"));
  7. var _result = _interopRequireDefault(require("./result"));
  8. var _parse = _interopRequireDefault(require("./parse"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. 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); } }
  11. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  12. function isPromise(obj) {
  13. return typeof obj === 'object' && typeof obj.then === 'function';
  14. }
  15. /**
  16. * A Promise proxy for the result of PostCSS transformations.
  17. *
  18. * A `LazyResult` instance is returned by {@link Processor#process}.
  19. *
  20. * @example
  21. * const lazy = postcss([autoprefixer]).process(css)
  22. */
  23. var LazyResult =
  24. /*#__PURE__*/
  25. function () {
  26. function LazyResult(processor, css, opts) {
  27. this.stringified = false;
  28. this.processed = false;
  29. var root;
  30. if (typeof css === 'object' && css !== null && css.type === 'root') {
  31. root = css;
  32. } else if (css instanceof LazyResult || css instanceof _result.default) {
  33. root = css.root;
  34. if (css.map) {
  35. if (typeof opts.map === 'undefined') opts.map = {};
  36. if (!opts.map.inline) opts.map.inline = false;
  37. opts.map.prev = css.map;
  38. }
  39. } else {
  40. var parser = _parse.default;
  41. if (opts.syntax) parser = opts.syntax.parse;
  42. if (opts.parser) parser = opts.parser;
  43. if (parser.parse) parser = parser.parse;
  44. try {
  45. root = parser(css, opts);
  46. } catch (error) {
  47. this.error = error;
  48. }
  49. }
  50. this.result = new _result.default(processor, root, opts);
  51. }
  52. /**
  53. * Returns a {@link Processor} instance, which will be used
  54. * for CSS transformations.
  55. *
  56. * @type {Processor}
  57. */
  58. var _proto = LazyResult.prototype;
  59. /**
  60. * Processes input CSS through synchronous plugins
  61. * and calls {@link Result#warnings()}.
  62. *
  63. * @return {Warning[]} Warnings from plugins.
  64. */
  65. _proto.warnings = function warnings() {
  66. return this.sync().warnings();
  67. }
  68. /**
  69. * Alias for the {@link LazyResult#css} property.
  70. *
  71. * @example
  72. * lazy + '' === lazy.css
  73. *
  74. * @return {string} Output CSS.
  75. */
  76. ;
  77. _proto.toString = function toString() {
  78. return this.css;
  79. }
  80. /**
  81. * Processes input CSS through synchronous and asynchronous plugins
  82. * and calls `onFulfilled` with a Result instance. If a plugin throws
  83. * an error, the `onRejected` callback will be executed.
  84. *
  85. * It implements standard Promise API.
  86. *
  87. * @param {onFulfilled} onFulfilled Callback will be executed
  88. * when all plugins will finish work.
  89. * @param {onRejected} onRejected Callback will be executed on any error.
  90. *
  91. * @return {Promise} Promise API to make queue.
  92. *
  93. * @example
  94. * postcss([autoprefixer]).process(css, { from: cssPath }).then(result => {
  95. * console.log(result.css)
  96. * })
  97. */
  98. ;
  99. _proto.then = function then(onFulfilled, onRejected) {
  100. if (process.env.NODE_ENV !== 'production') {
  101. if (!('from' in this.opts)) {
  102. (0, _warnOnce.default)('Without `from` option PostCSS could generate wrong source map ' + 'and will not find Browserslist config. Set it to CSS file path ' + 'or to `undefined` to prevent this warning.');
  103. }
  104. }
  105. return this.async().then(onFulfilled, onRejected);
  106. }
  107. /**
  108. * Processes input CSS through synchronous and asynchronous plugins
  109. * and calls onRejected for each error thrown in any plugin.
  110. *
  111. * It implements standard Promise API.
  112. *
  113. * @param {onRejected} onRejected Callback will be executed on any error.
  114. *
  115. * @return {Promise} Promise API to make queue.
  116. *
  117. * @example
  118. * postcss([autoprefixer]).process(css).then(result => {
  119. * console.log(result.css)
  120. * }).catch(error => {
  121. * console.error(error)
  122. * })
  123. */
  124. ;
  125. _proto.catch = function _catch(onRejected) {
  126. return this.async().catch(onRejected);
  127. }
  128. /**
  129. * Processes input CSS through synchronous and asynchronous plugins
  130. * and calls onFinally on any error or when all plugins will finish work.
  131. *
  132. * It implements standard Promise API.
  133. *
  134. * @param {onFinally} onFinally Callback will be executed on any error or
  135. * when all plugins will finish work.
  136. *
  137. * @return {Promise} Promise API to make queue.
  138. *
  139. * @example
  140. * postcss([autoprefixer]).process(css).finally(() => {
  141. * console.log('processing ended')
  142. * })
  143. */
  144. ;
  145. _proto.finally = function _finally(onFinally) {
  146. return this.async().then(onFinally, onFinally);
  147. };
  148. _proto.handleError = function handleError(error, plugin) {
  149. try {
  150. this.error = error;
  151. if (error.name === 'CssSyntaxError' && !error.plugin) {
  152. error.plugin = plugin.postcssPlugin;
  153. error.setMessage();
  154. } else if (plugin.postcssVersion) {
  155. if (process.env.NODE_ENV !== 'production') {
  156. var pluginName = plugin.postcssPlugin;
  157. var pluginVer = plugin.postcssVersion;
  158. var runtimeVer = this.result.processor.version;
  159. var a = pluginVer.split('.');
  160. var b = runtimeVer.split('.');
  161. if (a[0] !== b[0] || parseInt(a[1]) > parseInt(b[1])) {
  162. console.error('Unknown error from PostCSS plugin. Your current PostCSS ' + 'version is ' + runtimeVer + ', but ' + pluginName + ' uses ' + pluginVer + '. Perhaps this is the source of the error below.');
  163. }
  164. }
  165. }
  166. } catch (err) {
  167. if (console && console.error) console.error(err);
  168. }
  169. };
  170. _proto.asyncTick = function asyncTick(resolve, reject) {
  171. var _this = this;
  172. if (this.plugin >= this.processor.plugins.length) {
  173. this.processed = true;
  174. return resolve();
  175. }
  176. try {
  177. var plugin = this.processor.plugins[this.plugin];
  178. var promise = this.run(plugin);
  179. this.plugin += 1;
  180. if (isPromise(promise)) {
  181. promise.then(function () {
  182. _this.asyncTick(resolve, reject);
  183. }).catch(function (error) {
  184. _this.handleError(error, plugin);
  185. _this.processed = true;
  186. reject(error);
  187. });
  188. } else {
  189. this.asyncTick(resolve, reject);
  190. }
  191. } catch (error) {
  192. this.processed = true;
  193. reject(error);
  194. }
  195. };
  196. _proto.async = function async() {
  197. var _this2 = this;
  198. if (this.processed) {
  199. return new Promise(function (resolve, reject) {
  200. if (_this2.error) {
  201. reject(_this2.error);
  202. } else {
  203. resolve(_this2.stringify());
  204. }
  205. });
  206. }
  207. if (this.processing) {
  208. return this.processing;
  209. }
  210. this.processing = new Promise(function (resolve, reject) {
  211. if (_this2.error) return reject(_this2.error);
  212. _this2.plugin = 0;
  213. _this2.asyncTick(resolve, reject);
  214. }).then(function () {
  215. _this2.processed = true;
  216. return _this2.stringify();
  217. });
  218. return this.processing;
  219. };
  220. _proto.sync = function sync() {
  221. if (this.processed) return this.result;
  222. this.processed = true;
  223. if (this.processing) {
  224. throw new Error('Use process(css).then(cb) to work with async plugins');
  225. }
  226. if (this.error) throw this.error;
  227. for (var _iterator = this.result.processor.plugins, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  228. var _ref;
  229. if (_isArray) {
  230. if (_i >= _iterator.length) break;
  231. _ref = _iterator[_i++];
  232. } else {
  233. _i = _iterator.next();
  234. if (_i.done) break;
  235. _ref = _i.value;
  236. }
  237. var plugin = _ref;
  238. var promise = this.run(plugin);
  239. if (isPromise(promise)) {
  240. throw new Error('Use process(css).then(cb) to work with async plugins');
  241. }
  242. }
  243. return this.result;
  244. };
  245. _proto.run = function run(plugin) {
  246. this.result.lastPlugin = plugin;
  247. try {
  248. return plugin(this.result.root, this.result);
  249. } catch (error) {
  250. this.handleError(error, plugin);
  251. throw error;
  252. }
  253. };
  254. _proto.stringify = function stringify() {
  255. if (this.stringified) return this.result;
  256. this.stringified = true;
  257. this.sync();
  258. var opts = this.result.opts;
  259. var str = _stringify2.default;
  260. if (opts.syntax) str = opts.syntax.stringify;
  261. if (opts.stringifier) str = opts.stringifier;
  262. if (str.stringify) str = str.stringify;
  263. var map = new _mapGenerator.default(str, this.result.root, this.result.opts);
  264. var data = map.generate();
  265. this.result.css = data[0];
  266. this.result.map = data[1];
  267. return this.result;
  268. };
  269. _createClass(LazyResult, [{
  270. key: "processor",
  271. get: function get() {
  272. return this.result.processor;
  273. }
  274. /**
  275. * Options from the {@link Processor#process} call.
  276. *
  277. * @type {processOptions}
  278. */
  279. }, {
  280. key: "opts",
  281. get: function get() {
  282. return this.result.opts;
  283. }
  284. /**
  285. * Processes input CSS through synchronous plugins, converts `Root`
  286. * to a CSS string and returns {@link Result#css}.
  287. *
  288. * This property will only work with synchronous plugins.
  289. * If the processor contains any asynchronous plugins
  290. * it will throw an error. This is why this method is only
  291. * for debug purpose, you should always use {@link LazyResult#then}.
  292. *
  293. * @type {string}
  294. * @see Result#css
  295. */
  296. }, {
  297. key: "css",
  298. get: function get() {
  299. return this.stringify().css;
  300. }
  301. /**
  302. * An alias for the `css` property. Use it with syntaxes
  303. * that generate non-CSS output.
  304. *
  305. * This property will only work with synchronous plugins.
  306. * If the processor contains any asynchronous plugins
  307. * it will throw an error. This is why this method is only
  308. * for debug purpose, you should always use {@link LazyResult#then}.
  309. *
  310. * @type {string}
  311. * @see Result#content
  312. */
  313. }, {
  314. key: "content",
  315. get: function get() {
  316. return this.stringify().content;
  317. }
  318. /**
  319. * Processes input CSS through synchronous plugins
  320. * and returns {@link Result#map}.
  321. *
  322. * This property will only work with synchronous plugins.
  323. * If the processor contains any asynchronous plugins
  324. * it will throw an error. This is why this method is only
  325. * for debug purpose, you should always use {@link LazyResult#then}.
  326. *
  327. * @type {SourceMapGenerator}
  328. * @see Result#map
  329. */
  330. }, {
  331. key: "map",
  332. get: function get() {
  333. return this.stringify().map;
  334. }
  335. /**
  336. * Processes input CSS through synchronous plugins
  337. * and returns {@link Result#root}.
  338. *
  339. * This property will only work with synchronous plugins. If the processor
  340. * contains any asynchronous plugins it will throw an error.
  341. *
  342. * This is why this method is only for debug purpose,
  343. * you should always use {@link LazyResult#then}.
  344. *
  345. * @type {Root}
  346. * @see Result#root
  347. */
  348. }, {
  349. key: "root",
  350. get: function get() {
  351. return this.sync().root;
  352. }
  353. /**
  354. * Processes input CSS through synchronous plugins
  355. * and returns {@link Result#messages}.
  356. *
  357. * This property will only work with synchronous plugins. If the processor
  358. * contains any asynchronous plugins it will throw an error.
  359. *
  360. * This is why this method is only for debug purpose,
  361. * you should always use {@link LazyResult#then}.
  362. *
  363. * @type {Message[]}
  364. * @see Result#messages
  365. */
  366. }, {
  367. key: "messages",
  368. get: function get() {
  369. return this.sync().messages;
  370. }
  371. }]);
  372. return LazyResult;
  373. }();
  374. var _default = LazyResult;
  375. /**
  376. * @callback onFulfilled
  377. * @param {Result} result
  378. */
  379. /**
  380. * @callback onRejected
  381. * @param {Error} error
  382. */
  383. exports.default = _default;
  384. module.exports = exports.default;
  385. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImxhenktcmVzdWx0LmVzNiJdLCJuYW1lcyI6WyJpc1Byb21pc2UiLCJvYmoiLCJ0aGVuIiwiTGF6eVJlc3VsdCIsInByb2Nlc3NvciIsImNzcyIsIm9wdHMiLCJzdHJpbmdpZmllZCIsInByb2Nlc3NlZCIsInJvb3QiLCJ0eXBlIiwiUmVzdWx0IiwibWFwIiwiaW5saW5lIiwicHJldiIsInBhcnNlciIsInBhcnNlIiwic3ludGF4IiwiZXJyb3IiLCJyZXN1bHQiLCJ3YXJuaW5ncyIsInN5bmMiLCJ0b1N0cmluZyIsIm9uRnVsZmlsbGVkIiwib25SZWplY3RlZCIsInByb2Nlc3MiLCJlbnYiLCJOT0RFX0VOViIsImFzeW5jIiwiY2F0Y2giLCJmaW5hbGx5Iiwib25GaW5hbGx5IiwiaGFuZGxlRXJyb3IiLCJwbHVnaW4iLCJuYW1lIiwicG9zdGNzc1BsdWdpbiIsInNldE1lc3NhZ2UiLCJwb3N0Y3NzVmVyc2lvbiIsInBsdWdpbk5hbWUiLCJwbHVnaW5WZ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