UNPKG

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