You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
2.6 KiB

  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 root
  55. let str = stringify
  56. this.result = new Result(this._processor, root, this._opts)
  57. this.result.css = css
  58. let self = this
  59. Object.defineProperty(this.result, 'root', {
  60. get() {
  61. return self.root
  62. }
  63. })
  64. let map = new MapGenerator(str, root, this._opts, css)
  65. if (map.isMap()) {
  66. let [generatedCSS, generatedMap] = map.generate()
  67. if (generatedCSS) {
  68. this.result.css = generatedCSS
  69. }
  70. if (generatedMap) {
  71. this.result.map = generatedMap
  72. }
  73. } else {
  74. map.clearAnnotation()
  75. this.result.css = map.css
  76. }
  77. }
  78. async() {
  79. if (this.error) return Promise.reject(this.error)
  80. return Promise.resolve(this.result)
  81. }
  82. catch(onRejected) {
  83. return this.async().catch(onRejected)
  84. }
  85. finally(onFinally) {
  86. return this.async().then(onFinally, onFinally)
  87. }
  88. sync() {
  89. if (this.error) throw this.error
  90. return this.result
  91. }
  92. then(onFulfilled, onRejected) {
  93. if (process.env.NODE_ENV !== 'production') {
  94. if (!('from' in this._opts)) {
  95. warnOnce(
  96. 'Without `from` option PostCSS could generate wrong source map ' +
  97. 'and will not find Browserslist config. Set it to CSS file path ' +
  98. 'or to `undefined` to prevent this warning.'
  99. )
  100. }
  101. }
  102. return this.async().then(onFulfilled, onRejected)
  103. }
  104. toString() {
  105. return this._css
  106. }
  107. warnings() {
  108. return []
  109. }
  110. }
  111. module.exports = NoWorkResult
  112. NoWorkResult.default = NoWorkResult