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