123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- 'use strict';
- exports.__esModule = true;
- 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; }; }();
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
- var _mapGenerator = require('./map-generator');
- var _mapGenerator2 = _interopRequireDefault(_mapGenerator);
- var _stringify2 = require('./stringify');
- var _stringify3 = _interopRequireDefault(_stringify2);
- var _warnOnce = require('./warn-once');
- var _warnOnce2 = _interopRequireDefault(_warnOnce);
- var _result = require('./result');
- var _result2 = _interopRequireDefault(_result);
- var _parse = require('./parse');
- var _parse2 = _interopRequireDefault(_parse);
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- function isPromise(obj) {
- return (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && typeof obj.then === 'function';
- }
- var LazyResult = function () {
- function LazyResult(processor, css, opts) {
- _classCallCheck(this, LazyResult);
- this.stringified = false;
- this.processed = false;
- var root = void 0;
- if ((typeof css === 'undefined' ? 'undefined' : _typeof(css)) === 'object' && css.type === 'root') {
- root = css;
- } else if (css instanceof LazyResult || css instanceof _result2.default) {
- root = css.root;
- if (css.map) {
- if (typeof opts.map === 'undefined') opts.map = {};
- if (!opts.map.inline) opts.map.inline = false;
- opts.map.prev = css.map;
- }
- } else {
- var parser = _parse2.default;
- if (opts.syntax) parser = opts.syntax.parse;
- if (opts.parser) parser = opts.parser;
- if (parser.parse) parser = parser.parse;
- try {
- root = parser(css, opts);
- } catch (error) {
- this.error = error;
- }
- }
- this.result = new _result2.default(processor, root, opts);
- }
-
-
- LazyResult.prototype.warnings = function warnings() {
- return this.sync().warnings();
- };
-
- LazyResult.prototype.toString = function toString() {
- return this.css;
- };
-
- LazyResult.prototype.then = function then(onFulfilled, onRejected) {
- return this.async().then(onFulfilled, onRejected);
- };
-
- LazyResult.prototype.catch = function _catch(onRejected) {
- return this.async().catch(onRejected);
- };
- LazyResult.prototype.handleError = function handleError(error, plugin) {
- try {
- this.error = error;
- if (error.name === 'CssSyntaxError' && !error.plugin) {
- error.plugin = plugin.postcssPlugin;
- error.setMessage();
- } else if (plugin.postcssVersion) {
- var pluginName = plugin.postcssPlugin;
- var pluginVer = plugin.postcssVersion;
- var runtimeVer = this.result.processor.version;
- var a = pluginVer.split('.');
- var b = runtimeVer.split('.');
- if (a[0] !== b[0] || parseInt(a[1]) > parseInt(b[1])) {
- (0, _warnOnce2.default)('Your current PostCSS version ' + 'is ' + runtimeVer + ', but ' + pluginName + ' ' + 'uses ' + pluginVer + '. Perhaps this is ' + 'the source of the error below.');
- }
- }
- } catch (err) {
- if (console && console.error) console.error(err);
- }
- };
- LazyResult.prototype.asyncTick = function asyncTick(resolve, reject) {
- var _this = this;
- if (this.plugin >= this.processor.plugins.length) {
- this.processed = true;
- return resolve();
- }
- try {
- var plugin = this.processor.plugins[this.plugin];
- var promise = this.run(plugin);
- this.plugin += 1;
- if (isPromise(promise)) {
- promise.then(function () {
- _this.asyncTick(resolve, reject);
- }).catch(function (error) {
- _this.handleError(error, plugin);
- _this.processed = true;
- reject(error);
- });
- } else {
- this.asyncTick(resolve, reject);
- }
- } catch (error) {
- this.processed = true;
- reject(error);
- }
- };
- LazyResult.prototype.async = function async() {
- var _this2 = this;
- if (this.processed) {
- return new Promise(function (resolve, reject) {
- if (_this2.error) {
- reject(_this2.error);
- } else {
- resolve(_this2.stringify());
- }
- });
- }
- if (this.processing) {
- return this.processing;
- }
- this.processing = new Promise(function (resolve, reject) {
- if (_this2.error) return reject(_this2.error);
- _this2.plugin = 0;
- _this2.asyncTick(resolve, reject);
- }).then(function () {
- _this2.processed = true;
- return _this2.stringify();
- });
- return this.processing;
- };
- LazyResult.prototype.sync = function sync() {
- if (this.processed) return this.result;
- this.processed = true;
- if (this.processing) {
- throw new Error('Use process(css).then(cb) to work with async plugins');
- }
- if (this.error) throw this.error;
- for (var _iterator = this.result.processor.plugins, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
- var _ref;
- if (_isArray) {
- if (_i >= _iterator.length) break;
- _ref = _iterator[_i++];
- } else {
- _i = _iterator.next();
- if (_i.done) break;
- _ref = _i.value;
- }
- var plugin = _ref;
- var promise = this.run(plugin);
- if (isPromise(promise)) {
- throw new Error('Use process(css).then(cb) to work with async plugins');
- }
- }
- return this.result;
- };
- LazyResult.prototype.run = function run(plugin) {
- this.result.lastPlugin = plugin;
- try {
- return plugin(this.result.root, this.result);
- } catch (error) {
- this.handleError(error, plugin);
- throw error;
- }
- };
- LazyResult.prototype.stringify = function stringify() {
- if (this.stringified) return this.result;
- this.stringified = true;
- this.sync();
- var opts = this.result.opts;
- var str = _stringify3.default;
- if (opts.syntax) str = opts.syntax.stringify;
- if (opts.stringifier) str = opts.stringifier;
- if (str.stringify) str = str.stringify;
- var map = new _mapGenerator2.default(str, this.result.root, this.result.opts);
- var data = map.generate();
- this.result.css = data[0];
- this.result.map = data[1];
- return this.result;
- };
- _createClass(LazyResult, [{
- key: 'processor',
- get: function get() {
- return this.result.processor;
- }
-
- }, {
- key: 'opts',
- get: function get() {
- return this.result.opts;
- }
-
- }, {
- key: 'css',
- get: function get() {
- return this.stringify().css;
- }
-
- }, {
- key: 'content',
- get: function get() {
- return this.stringify().content;
- }
-
- }, {
- key: 'map',
- get: function get() {
- return this.stringify().map;
- }
-
- }, {
- key: 'root',
- get: function get() {
- return this.sync().root;
- }
-
- }, {
- key: 'messages',
- get: function get() {
- return this.sync().messages;
- }
- }]);
- return LazyResult;
- }();
- exports.default = LazyResult;
- module.exports = exports['default'];
|