no-work-result.js 2.6 KB

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