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. finally(onFinally) {
  44. return this.async().then(onFinally, onFinally)
  45. }
  46. sync() {
  47. if (this.error) throw this.error
  48. return this.result
  49. }
  50. then(onFulfilled, onRejected) {
  51. if (process.env.NODE_ENV !== 'production') {
  52. if (!('from' in this._opts)) {
  53. warnOnce(
  54. 'Without `from` option PostCSS could generate wrong source map ' +
  55. 'and will not find Browserslist config. Set it to CSS file path ' +
  56. 'or to `undefined` to prevent this warning.'
  57. )
  58. }
  59. }
  60. return this.async().then(onFulfilled, onRejected)
  61. }
  62. toString() {
  63. return this._css
  64. }
  65. warnings() {
  66. return []
  67. }
  68. get content() {
  69. return this.result.css
  70. }
  71. get css() {
  72. return this.result.css
  73. }
  74. get map() {
  75. return this.result.map
  76. }
  77. get messages() {
  78. return []
  79. }
  80. get opts() {
  81. return this.result.opts
  82. }
  83. get processor() {
  84. return this.result.processor
  85. }
  86. get root() {
  87. if (this._root) {
  88. return this._root
  89. }
  90. let root
  91. let parser = parse
  92. try {
  93. root = parser(this._css, this._opts)
  94. } catch (error) {
  95. this.error = error
  96. }
  97. if (this.error) {
  98. throw this.error
  99. } else {
  100. this._root = root
  101. return root
  102. }
  103. }
  104. get [Symbol.toStringTag]() {
  105. return 'NoWorkResult'
  106. }
  107. }
  108. module.exports = NoWorkResult
  109. NoWorkResult.default = NoWorkResult