no-work-result.js 2.6 KB

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