Skip to content

Commit

Permalink
refactor(src): update code base with latest ES2015+ features
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky committed Aug 8, 2018
1 parent f34954f commit bdcbef0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/Error.js
@@ -1,13 +1,13 @@
'use strict'

class SyntaxError extends Error {
constructor (err) {
super(err)

this.name = 'Syntax Error'
const { line, column, reason } = err

this.name = 'SyntaxError'

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

this.stack = false
Expand Down
2 changes: 1 addition & 1 deletion src/Warning.js
Expand Up @@ -2,7 +2,7 @@ class Warning extends Error {
constructor (warning) {
super()

const { line, column, text } = warning
const { text, line, column } = warning

this.name = 'LoaderWarning'
this.message = `\n(${line}:${column}) ${text}\n`
Expand Down
59 changes: 39 additions & 20 deletions src/index.js
@@ -1,10 +1,6 @@
'use strict'

const path = require('path')

const loaderUtils = require('loader-utils')

const parseOptions = require('./options')
const { getOptions } = require('loader-utils')
const validateOptions = require('schema-utils')

const postcss = require('postcss')
Expand Down Expand Up @@ -42,7 +38,7 @@ const parseOptions = require('./options.js')
* @return {cb} cb Result
*/
module.exports = function loader (css, map, meta) {
const options = Object.assign({}, loaderUtils.getOptions(this))
const options = Object.assign({}, getOptions(this))

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

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

return postcssrc(rc.ctx, rc.path)
}).then((config) => {
if (!config) config = {}
if (!config) {
config = {}
}

if (config.file) this.addDependency(config.file)
if (config.file) {
this.addDependency(config.file)
}

// Disable override `to` option from `postcss.config.js`
if (config.options.to) delete config.options.to
if (config.options.to) {
delete config.options.to
}
// Disable override `from` option from `postcss.config.js`
if (config.options.from) delete config.options.from
if (config.options.from) {
delete config.options.from
}

let plugins = config.plugins || []

let options = Object.assign({
from: file,
map: sourceMap
Expand Down Expand Up @@ -139,29 +144,39 @@ module.exports = function loader (css, map, meta) {
css = this.exec(css, this.resource)
}

if (sourceMap && typeof map === 'string') map = JSON.parse(map)
if (sourceMap && map) options.map.prev = map
if (sourceMap && typeof map === 'string') {
map = JSON.parse(map)
}

if (sourceMap && map) {
options.map.prev = map
}

return postcss(plugins)
.process(css, options)
.then((result) => {
let { css, map, root, processor, messages } = result

result.warnings().forEach((warning) => {
this.emitWarning(new Warning(warning))
})

result.messages.forEach((msg) => {
if (msg.type === 'dependency') this.addDependency(msg.file)
messages.forEach((msg) => {
if (msg.type === 'dependency') {
this.addDependency(msg.file)
}
})

css = result.css
map = result.map ? result.map.toJSON() : null
map = map ? map.toJSON() : null

if (map) {
map.file = path.resolve(map.file)
map.sources = map.sources.map((src) => path.resolve(src))
}

if (!meta) meta = {}
if (!meta) {
meta = {}
}

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

meta.ast = ast
meta.messages = result.messages
meta.messages = messages

if (this.loaderIndex === 0) {
/**
Expand Down Expand Up @@ -199,8 +214,12 @@ module.exports = function loader (css, map, meta) {
return null
})
}).catch((err) => {
if (err.file) this.addDependency(err.file)
if (err.file) {
this.addDependency(err.file)
}

return err.name === 'CssSyntaxError' ? cb(new SyntaxError(err)) : cb(err)
return err.name === 'CssSyntaxError'
? cb(new SyntaxError(err))
: cb(err)
})
}
32 changes: 14 additions & 18 deletions src/options.js
@@ -1,25 +1,21 @@
'use strict'

module.exports = function parseOptions (params) {
if (typeof params.plugins === 'function') {
params.plugins = params.plugins.call(this, this)
function parseOptions ({ exec, parser, syntax, stringifier, plugins }) {
if (typeof plugins === 'function') {
plugins = plugins.call(this, this)
}

let plugins

if (typeof params.plugins === 'undefined') plugins = []
else if (Array.isArray(params.plugins)) plugins = params.plugins
else plugins = [ params.plugins ]
if (typeof plugins === 'undefined') {
plugins = []
} else if (!Array.isArray(plugins)) {
plugins = [ plugins ]
}

const options = {}

if (typeof params !== 'undefined') {
options.parser = params.parser
options.syntax = params.syntax
options.stringifier = params.stringifier
}

const exec = params && params.exec
options.parser = parser
options.syntax = syntax
options.stringifier = stringifier

return Promise.resolve({ options: options, plugins: plugins, exec: exec })
return Promise.resolve({ options, plugins, exec })
}

module.exports = parseOptions
2 changes: 1 addition & 1 deletion test/__snapshots__/Errors.test.js.snap
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Errors Syntax Error 1`] = `
"Syntax Error
"SyntaxError
(1:3) Unexpected separator in property
Expand Down

0 comments on commit bdcbef0

Please sign in to comment.