no-work-result.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. this._root = root
  66. return root
  67. }
  68. get messages() {
  69. return []
  70. }
  71. warnings() {
  72. return []
  73. }
  74. toString() {
  75. return this._css
  76. }
  77. then(onFulfilled, onRejected) {
  78. if (process.env.NODE_ENV !== 'production') {
  79. if (!('from' in this._opts)) {
  80. warnOnce(
  81. 'Without `from` option PostCSS could generate wrong source map ' +
  82. 'and will not find Browserslist config. Set it to CSS file path ' +
  83. 'or to `undefined` to prevent this warning.'
  84. )
  85. }
  86. }
  87. return this.async().then(onFulfilled, onRejected)
  88. }
  89. catch(onRejected) {
  90. return this.async().catch(onRejected)
  91. }
  92. finally(onFinally) {
  93. return this.async().then(onFinally, onFinally)
  94. }
  95. async() {
  96. if (this.error) return Promise.reject(this.error)
  97. return Promise.resolve(this.result)
  98. }
  99. sync() {
  100. if (this.error) throw this.error
  101. return this.result
  102. }
  103. }
  104. module.exports = NoWorkResult
  105. NoWorkResult.default = NoWorkResult