no-work-result.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. 'use strict'
  2. let MapGenerator = require('./map-generator')
  3. let stringify = require('./stringify')
  4. let warnOnce = require('./warn-once')
  5. let parse = require('./parse')
  6. const Result = require('./result')
  7. class NoWorkResult {
  8. constructor(processor, css, opts) {
  9. css = css.toString()
  10. this.stringified = false
  11. this._processor = processor
  12. this._css = css
  13. this._opts = opts
  14. this._map = undefined
  15. let root
  16. let str = stringify
  17. this.result = new Result(this._processor, root, this._opts)
  18. this.result.css = css
  19. let self = this
  20. Object.defineProperty(this.result, 'root', {
  21. get() {
  22. return self.root
  23. }
  24. })
  25. let map = new MapGenerator(str, root, this._opts, css)
  26. if (map.isMap()) {
  27. let [generatedCSS, generatedMap] = map.generate()
  28. if (generatedCSS) {
  29. this.result.css = generatedCSS
  30. }
  31. if (generatedMap) {
  32. this.result.map = generatedMap
  33. }
  34. }
  35. }
  36. async() {
  37. if (this.error) return Promise.reject(this.error)
  38. return Promise.resolve(this.result)
  39. }
  40. catch(onRejected) {
  41. return this.async().catch(onRejected)
  42. }
  43. get content() {
  44. return this.result.css
  45. }
  46. get css() {
  47. return this.result.css
  48. }
  49. finally(onFinally) {
  50. return this.async().then(onFinally, onFinally)
  51. }
  52. get map() {
  53. return this.result.map
  54. }
  55. get messages() {
  56. return []
  57. }
  58. get opts() {
  59. return this.result.opts
  60. }
  61. get processor() {
  62. return this.result.processor
  63. }
  64. get root() {
  65. if (this._root) {
  66. return this._root
  67. }
  68. let root
  69. let parser = parse
  70. try {
  71. root = parser(this._css, this._opts)
  72. } catch (error) {
  73. this.error = error
  74. }
  75. if (this.error) {
  76. throw this.error
  77. } else {
  78. this._root = root
  79. return root
  80. }
  81. }
  82. get [Symbol.toStringTag]() {
  83. return 'NoWorkResult'
  84. }
  85. sync() {
  86. if (this.error) throw this.error
  87. return this.result
  88. }
  89. then(onFulfilled, onRejected) {
  90. if (process.env.NODE_ENV !== 'production') {
  91. if (!('from' in this._opts)) {
  92. warnOnce(
  93. 'Without `from` option PostCSS could generate wrong source map ' +
  94. 'and will not find Browserslist config. Set it to CSS file path ' +
  95. 'or to `undefined` to prevent this warning.'
  96. )
  97. }
  98. }
  99. return this.async().then(onFulfilled, onRejected)
  100. }
  101. toString() {
  102. return this._css
  103. }
  104. warnings() {
  105. return []
  106. }
  107. }
  108. module.exports = NoWorkResult
  109. NoWorkResult.default = NoWorkResult