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 May 16, 2017
1 parent f7aa9a8 commit 7acce91
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
69 changes: 29 additions & 40 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,78 +47,67 @@ 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: {}
Promise.resolve().then(() => {
if (!options.config || (options.config && !options.config.path)) {
return parseOptions.call(this, options)
}

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)
}

if (options.config.ctx) {
rc.ctx.options = options.config.ctx
}
}

const sourceMap = options.sourceMap

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)
}

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

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

const sourceMap = options.sourceMap
delete options.sourceMap

let plugins = config.plugins || []
let options = Object.assign({
let resolvedOptions = Object.assign({
to: file,
from: file,
map: sourceMap
? sourceMap === 'inline'
? { inline: true, annotation: false }
: { inline: false, annotation: false }
: false
}, config.options)
}, options, config.options)

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

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

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

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

// Loader API Exec (Deprecated)
Expand All @@ -132,10 +121,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) resolvedOptions.map.prev = map

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

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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",
"test:build": "node test/webpack.build.js",
"posttest": "npm run clean",
"release": "standard-version"
Expand Down

0 comments on commit 7acce91

Please sign in to comment.