Skip to content

Commit

Permalink
refactor: improved loading algorithm of options and configs.
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jun 14, 2017
1 parent 2ef4449 commit 531041a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 50 deletions.
88 changes: 44 additions & 44 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,53 +47,53 @@ module.exports = function loader (css, map) {

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

const rc = {
path: path.dirname(file),
ctx: {
file: {
extname: path.extname(file),
dirname: path.dirname(file),
basename: path.basename(file)
},
options: {}
}
}

if (options.config) {
if (options.config.path) {
rc.path = path.resolve(options.config.path)
Promise.resolve().then(() => {
if (options.exec || options.parser || options.syntax || options.stringifier || options.plugins) {
return parseOptions.call(this, options)
}

if (options.config.ctx) {
rc.ctx.options = options.config.ctx
const rc = {
path: path.dirname(file),
ctx: {
file: {
extname: path.extname(file),
dirname: path.dirname(file),
basename: path.basename(file)
},
options: {}
}
}
}

const sourceMap = options.sourceMap
// Read options from options.config only when options.config is object
if (options.config) {
if (options.config.path) {
rc.path = path.resolve(options.config.path)
}

Promise.resolve().then(() => {
const length = Object.keys(options).length

// TODO
// Refactor
if (!options.config && !sourceMap && length) {
return parseOptions.call(this, options)
} else if (options.config && !sourceMap && length > 1) {
return parseOptions.call(this, options)
} else if (!options.config && sourceMap && length > 1) {
return parseOptions.call(this, options)
} else if (options.config && sourceMap && length > 2) {
return parseOptions.call(this, options)
if (options.config.ctx) {
rc.ctx.options = options.config.ctx
}
}

return postcssrc(rc.ctx, rc.path, { argv: false })
}).then((config) => {
if (!config) config = {}
.then((config) => {
// Get `sourceMap` option from loader options when no `map` option in `postcss.config.js`
if (!config.options.map) config.options.map = options.sourceMap

return config
})
}).then((config) => {
if (config.file) this.addDependency(config.file)

let sourceMap = config.options.map
let plugins = config.plugins || []
let options = Object.assign({

// Disable override `to` option
if (config.options.to) delete config.options.to
// Disable override `from` option
if (config.options.from) delete config.options.from

let postcssOption = Object.assign({
to: file,
from: file,
map: sourceMap
Expand All @@ -105,20 +105,20 @@ module.exports = function loader (css, map) {

// Loader Exec (Deprecated)
// https://webpack.js.org/api/loaders/#deprecated-context-properties
if (options.parser === 'postcss-js') {
if (postcssOption.parser === 'postcss-js') {
css = this.exec(css, this.resource)
}

if (typeof options.parser === 'string') {
options.parser = require(options.parser)
if (typeof postcssOption.parser === 'string') {
postcssOption.parser = require(postcssOption.parser)
}

if (typeof options.syntax === 'string') {
options.syntax = require(options.syntax)
if (typeof postcssOption.syntax === 'string') {
postcssOption.syntax = require(postcssOption.syntax)
}

if (typeof options.stringifier === 'string') {
options.stringifier = require(options.stringifier)
if (typeof postcssOption.stringifier === 'string') {
postcssOption.stringifier = require(postcssOption.stringifier)
}

// Loader API Exec (Deprecated)
Expand All @@ -132,10 +132,10 @@ module.exports = function loader (css, map) {
}

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

return postcss(plugins)
.process(css, options)
.process(css, postcssOption)
.then((result) => {
result.warnings().forEach((msg) => this.emitWarning(msg.toString()))

Expand Down
11 changes: 5 additions & 6 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ module.exports = function parseOptions (params) {
else if (Array.isArray(params.plugins)) plugins = params.plugins
else plugins = params.plugins

const options = {}

if (typeof params !== 'undefined') {
options.parser = params.parser
options.syntax = params.syntax
options.stringifier = params.stringifier
const options = {
parser: params.parser,
syntax: params.syntax,
stringifier: params.stringifier,
map: params.sourceMap
}

const exec = params && params.exec
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"docs": "jsdoc2md lib/index.js > LOADER.md",
"pretest": "npm run lint && npm run test:build",
"test": "jest",
"test-only": "npm run test:build && jest && npm run clean",
"test:build": "node test/webpack.build.js",
"posttest": "npm run clean",
"release": "standard-version"
Expand Down

0 comments on commit 531041a

Please sign in to comment.