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