Skip to content

Commit bdcbef0

Browse files
committedAug 8, 2018
refactor(src): update code base with latest ES2015+ features
1 parent f34954f commit bdcbef0

File tree

5 files changed

+59
-44
lines changed

5 files changed

+59
-44
lines changed
 

‎src/Error.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
'use strict'
2-
31
class SyntaxError extends Error {
42
constructor (err) {
53
super(err)
64

7-
this.name = 'Syntax Error'
5+
const { line, column, reason } = err
6+
7+
this.name = 'SyntaxError'
88

99
this.message = ''
10-
this.message += `${this.name} \n\n(${err.line}:${err.column}) ${err.reason}`
10+
this.message += `${this.name} \n\n(${line}:${column}) ${reason}`
1111
this.message += `\n\n${err.showSourceCode()}\n`
1212

1313
this.stack = false

‎src/Warning.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Warning extends Error {
22
constructor (warning) {
33
super()
44

5-
const { line, column, text } = warning
5+
const { text, line, column } = warning
66

77
this.name = 'LoaderWarning'
88
this.message = `\n(${line}:${column}) ${text}\n`

‎src/index.js

+39-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
'use strict'
2-
31
const path = require('path')
42

5-
const loaderUtils = require('loader-utils')
6-
7-
const parseOptions = require('./options')
3+
const { getOptions } = require('loader-utils')
84
const validateOptions = require('schema-utils')
95

106
const postcss = require('postcss')
@@ -42,7 +38,7 @@ const parseOptions = require('./options.js')
4238
* @return {cb} cb Result
4339
*/
4440
module.exports = function loader (css, map, meta) {
45-
const options = Object.assign({}, loaderUtils.getOptions(this))
41+
const options = Object.assign({}, getOptions(this))
4642

4743
validateOptions(require('./options.json'), options, 'PostCSS Loader')
4844

@@ -96,16 +92,25 @@ module.exports = function loader (css, map, meta) {
9692

9793
return postcssrc(rc.ctx, rc.path)
9894
}).then((config) => {
99-
if (!config) config = {}
95+
if (!config) {
96+
config = {}
97+
}
10098

101-
if (config.file) this.addDependency(config.file)
99+
if (config.file) {
100+
this.addDependency(config.file)
101+
}
102102

103103
// Disable override `to` option from `postcss.config.js`
104-
if (config.options.to) delete config.options.to
104+
if (config.options.to) {
105+
delete config.options.to
106+
}
105107
// Disable override `from` option from `postcss.config.js`
106-
if (config.options.from) delete config.options.from
108+
if (config.options.from) {
109+
delete config.options.from
110+
}
107111

108112
let plugins = config.plugins || []
113+
109114
let options = Object.assign({
110115
from: file,
111116
map: sourceMap
@@ -139,29 +144,39 @@ module.exports = function loader (css, map, meta) {
139144
css = this.exec(css, this.resource)
140145
}
141146

142-
if (sourceMap && typeof map === 'string') map = JSON.parse(map)
143-
if (sourceMap && map) options.map.prev = map
147+
if (sourceMap && typeof map === 'string') {
148+
map = JSON.parse(map)
149+
}
150+
151+
if (sourceMap && map) {
152+
options.map.prev = map
153+
}
144154

145155
return postcss(plugins)
146156
.process(css, options)
147157
.then((result) => {
158+
let { css, map, root, processor, messages } = result
159+
148160
result.warnings().forEach((warning) => {
149161
this.emitWarning(new Warning(warning))
150162
})
151163

152-
result.messages.forEach((msg) => {
153-
if (msg.type === 'dependency') this.addDependency(msg.file)
164+
messages.forEach((msg) => {
165+
if (msg.type === 'dependency') {
166+
this.addDependency(msg.file)
167+
}
154168
})
155169

156-
css = result.css
157-
map = result.map ? result.map.toJSON() : null
170+
map = map ? map.toJSON() : null
158171

159172
if (map) {
160173
map.file = path.resolve(map.file)
161174
map.sources = map.sources.map((src) => path.resolve(src))
162175
}
163176

164-
if (!meta) meta = {}
177+
if (!meta) {
178+
meta = {}
179+
}
165180

166181
const ast = {
167182
type: 'postcss',
@@ -170,7 +185,7 @@ module.exports = function loader (css, map, meta) {
170185
}
171186

172187
meta.ast = ast
173-
meta.messages = result.messages
188+
meta.messages = messages
174189

175190
if (this.loaderIndex === 0) {
176191
/**
@@ -199,8 +214,12 @@ module.exports = function loader (css, map, meta) {
199214
return null
200215
})
201216
}).catch((err) => {
202-
if (err.file) this.addDependency(err.file)
217+
if (err.file) {
218+
this.addDependency(err.file)
219+
}
203220

204-
return err.name === 'CssSyntaxError' ? cb(new SyntaxError(err)) : cb(err)
221+
return err.name === 'CssSyntaxError'
222+
? cb(new SyntaxError(err))
223+
: cb(err)
205224
})
206225
}

‎src/options.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
'use strict'
2-
3-
module.exports = function parseOptions (params) {
4-
if (typeof params.plugins === 'function') {
5-
params.plugins = params.plugins.call(this, this)
1+
function parseOptions ({ exec, parser, syntax, stringifier, plugins }) {
2+
if (typeof plugins === 'function') {
3+
plugins = plugins.call(this, this)
64
}
75

8-
let plugins
9-
10-
if (typeof params.plugins === 'undefined') plugins = []
11-
else if (Array.isArray(params.plugins)) plugins = params.plugins
12-
else plugins = [ params.plugins ]
6+
if (typeof plugins === 'undefined') {
7+
plugins = []
8+
} else if (!Array.isArray(plugins)) {
9+
plugins = [ plugins ]
10+
}
1311

1412
const options = {}
1513

16-
if (typeof params !== 'undefined') {
17-
options.parser = params.parser
18-
options.syntax = params.syntax
19-
options.stringifier = params.stringifier
20-
}
21-
22-
const exec = params && params.exec
14+
options.parser = parser
15+
options.syntax = syntax
16+
options.stringifier = stringifier
2317

24-
return Promise.resolve({ options: options, plugins: plugins, exec: exec })
18+
return Promise.resolve({ options, plugins, exec })
2519
}
20+
21+
module.exports = parseOptions

‎test/__snapshots__/Errors.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Errors Syntax Error 1`] = `
4-
"Syntax Error
4+
"SyntaxError
55
66
(1:3) Unexpected separator in property
77

0 commit comments

Comments
 (0)
Please sign in to comment.