UNPKG

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