3 exports.__esModule = true;
5 var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
7 var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
9 var _mapGenerator = require('./map-generator');
11 var _mapGenerator2 = _interopRequireDefault(_mapGenerator);
13 var _stringify2 = require('./stringify');
15 var _stringify3 = _interopRequireDefault(_stringify2);
17 var _warnOnce = require('./warn-once');
19 var _warnOnce2 = _interopRequireDefault(_warnOnce);
21 var _result = require('./result');
23 var _result2 = _interopRequireDefault(_result);
25 var _parse = require('./parse');
27 var _parse2 = _interopRequireDefault(_parse);
29 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
33 function isPromise(obj) {
34 return (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && typeof obj.then === 'function';
37 var LazyResult = function () {
38 function LazyResult(processor, css, opts) {
39 _classCallCheck(this, LazyResult);
41 this.stringified = false;
42 this.processed = false;
45 if ((typeof css === 'undefined' ? 'undefined' : _typeof(css)) === 'object' && css.type === 'root') {
47 } else if (css instanceof LazyResult || css instanceof _result2.default) {
50 if (typeof opts.map === 'undefined') opts.map = {};
51 if (!opts.map.inline) opts.map.inline = false;
52 opts.map.prev = css.map;
55 var parser = _parse2.default;
56 if (opts.syntax) parser = opts.syntax.parse;
57 if (opts.parser) parser = opts.parser;
58 if (parser.parse) parser = parser.parse;
61 root = parser(css, opts);
67 this.result = new _result2.default(processor, root, opts);
70 LazyResult.prototype.warnings = function warnings() {
71 return this.sync().warnings();
74 LazyResult.prototype.toString = function toString() {
78 LazyResult.prototype.then = function then(onFulfilled, onRejected) {
79 return this.async().then(onFulfilled, onRejected);
82 LazyResult.prototype.catch = function _catch(onRejected) {
83 return this.async().catch(onRejected);
86 LazyResult.prototype.handleError = function handleError(error, plugin) {
89 if (error.name === 'CssSyntaxError' && !error.plugin) {
90 error.plugin = plugin.postcssPlugin;
92 } else if (plugin.postcssVersion) {
93 var pluginName = plugin.postcssPlugin;
94 var pluginVer = plugin.postcssVersion;
95 var runtimeVer = this.result.processor.version;
96 var a = pluginVer.split('.');
97 var b = runtimeVer.split('.');
99 if (a[0] !== b[0] || parseInt(a[1]) > parseInt(b[1])) {
100 (0, _warnOnce2.default)('Your current PostCSS version ' + 'is ' + runtimeVer + ', but ' + pluginName + ' ' + 'uses ' + pluginVer + '. Perhaps this is ' + 'the source of the error below.');
104 /* istanbul ignore next */
105 if (console && console.error) console.error(err);
109 LazyResult.prototype.asyncTick = function asyncTick(resolve, reject) {
112 if (this.plugin >= this.processor.plugins.length) {
113 this.processed = true;
119 var plugin = _this.processor.plugins[_this.plugin];
120 var promise = _this.run(plugin);
123 if (isPromise(promise)) {
124 promise.then(function () {
125 _this.asyncTick(resolve, reject);
126 }).catch(function (error) {
127 _this.handleError(error, plugin);
128 _this.processed = true;
132 _this.asyncTick(resolve, reject);
136 this.processed = true;
141 LazyResult.prototype.async = function async() {
144 if (this.processed) {
145 return new Promise(function (resolve, reject) {
147 reject(_this2.error);
149 resolve(_this2.stringify());
153 if (this.processing) {
154 return this.processing;
157 this.processing = new Promise(function (resolve, reject) {
158 if (_this2.error) return reject(_this2.error);
160 _this2.asyncTick(resolve, reject);
161 }).then(function () {
162 _this2.processed = true;
163 return _this2.stringify();
166 return this.processing;
169 LazyResult.prototype.sync = function sync() {
170 if (this.processed) return this.result;
171 this.processed = true;
173 if (this.processing) {
174 throw new Error('Use process(css).then(cb) to work with async plugins');
177 if (this.error) throw this.error;
179 for (var _iterator = this.result.processor.plugins, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
183 if (_i >= _iterator.length) break;
184 _ref = _iterator[_i++];
186 _i = _iterator.next();
193 var promise = this.run(plugin);
194 if (isPromise(promise)) {
195 throw new Error('Use process(css).then(cb) to work with async plugins');
202 LazyResult.prototype.run = function run(plugin) {
203 this.result.lastPlugin = plugin;
206 return plugin(this.result.root, this.result);
208 this.handleError(error, plugin);
213 LazyResult.prototype.stringify = function stringify() {
214 if (this.stringified) return this.result;
215 this.stringified = true;
219 var opts = this.result.opts;
220 var str = _stringify3.default;
221 if (opts.syntax) str = opts.syntax.stringify;
222 if (opts.stringifier) str = opts.stringifier;
223 if (str.stringify) str = str.stringify;
225 var map = new _mapGenerator2.default(str, this.result.root, this.result.opts);
226 var data = map.generate();
227 this.result.css = data[0];
228 this.result.map = data[1];
233 _createClass(LazyResult, [{
235 get: function get() {
236 return this.result.processor;
240 get: function get() {
241 return this.result.opts;
245 get: function get() {
246 return this.stringify().css;
250 get: function get() {
251 return this.stringify().content;
255 get: function get() {
256 return this.stringify().map;
260 get: function get() {
261 return this.sync().root;
265 get: function get() {
266 return this.sync().messages;
273 exports.default = LazyResult;
274 module.exports = exports['default'];