From 75965c0051cea97261273470ed8b3524a36e2a46 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 16 May 2017 17:11:16 +0300 Subject: [PATCH] refactor: improved loading algorithm of options and configs. --- lib/index.js | 94 +++++++++++++++++++++++++----------------------- lib/options.js | 11 +++--- lib/options.json | 2 +- package.json | 1 + 4 files changed, 56 insertions(+), 52 deletions(-) diff --git a/lib/index.js b/lib/index.js index a90b3074..580c6701 100644 --- a/lib/index.js +++ b/lib/index.js @@ -47,53 +47,57 @@ 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.config) { + 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 - - 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) + // Read options from options.config only when options.config is object + if (typeof options.config === 'object' && + options.config !== null && + Object.prototype.toString.call(options.config) === '[object Object]' + ) { + if (options.config.path) { + rc.path = path.resolve(options.config.path) + } + + 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) => { + 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 + // Disable override `map` option + if (config.options.map) delete config.options.map + + let postcssOption = Object.assign({ to: file, from: file, map: sourceMap @@ -105,20 +109,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) @@ -132,10 +136,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())) diff --git a/lib/options.js b/lib/options.js index 56cff551..59fd8e9a 100644 --- a/lib/options.js +++ b/lib/options.js @@ -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 diff --git a/lib/options.json b/lib/options.json index cd865de1..60125ade 100644 --- a/lib/options.json +++ b/lib/options.json @@ -2,7 +2,7 @@ "type": "object", "properties": { "config": { - "type": "object", + "type": ["object", "boolean"], "properties": { "path": { "type": "string" diff --git a/package.json b/package.json index a4aaadfc..ef7db3cb 100644 --- a/package.json +++ b/package.json @@ -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 && npm run clean", "test:build": "node test/webpack.build.js", "posttest": "npm run clean", "release": "standard-version"