Skip to content

Commit 62e2a2b

Browse files
author
evilebottnawi
committed
refactor: improved loading algorithm of options and configs.
1 parent f7aa9a8 commit 62e2a2b

File tree

4 files changed

+51
-50
lines changed

4 files changed

+51
-50
lines changed

lib/index.js

+45-43
Original file line numberDiff line numberDiff line change
@@ -47,53 +47,55 @@ module.exports = function loader (css, map) {
4747

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

50-
const rc = {
51-
path: path.dirname(file),
52-
ctx: {
53-
file: {
54-
extname: path.extname(file),
55-
dirname: path.dirname(file),
56-
basename: path.basename(file)
57-
},
58-
options: {}
50+
Promise.resolve().then(() => {
51+
if (!options.config) {
52+
return parseOptions.call(this, options)
5953
}
60-
}
6154

62-
if (options.config) {
63-
if (options.config.path) {
64-
rc.path = path.resolve(options.config.path)
55+
const rc = {
56+
path: path.dirname(file),
57+
ctx: {
58+
file: {
59+
extname: path.extname(file),
60+
dirname: path.dirname(file),
61+
basename: path.basename(file)
62+
},
63+
options: {}
64+
}
6565
}
6666

67-
if (options.config.ctx) {
68-
rc.ctx.options = options.config.ctx
69-
}
70-
}
71-
72-
const sourceMap = options.sourceMap
73-
74-
Promise.resolve().then(() => {
75-
const length = Object.keys(options).length
76-
77-
// TODO
78-
// Refactor
79-
if (!options.config && !sourceMap && length) {
80-
return parseOptions.call(this, options)
81-
} else if (options.config && !sourceMap && length > 1) {
82-
return parseOptions.call(this, options)
83-
} else if (!options.config && sourceMap && length > 1) {
84-
return parseOptions.call(this, options)
85-
} else if (options.config && sourceMap && length > 2) {
86-
return parseOptions.call(this, options)
67+
// Read options from options.config only when options.config is object
68+
if (typeof options.config === 'object' &&
69+
options.config !== null &&
70+
Object.prototype.toString.call(options.config) === '[object Object]'
71+
) {
72+
if (options.config.path) {
73+
rc.path = path.resolve(options.config.path)
74+
}
75+
76+
if (options.config.ctx) {
77+
rc.ctx.options = options.config.ctx
78+
}
8779
}
8880

8981
return postcssrc(rc.ctx, rc.path, { argv: false })
9082
}).then((config) => {
9183
if (!config) config = {}
84+
if (!config.options) config.options = {}
9285

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

88+
let sourceMap = options.sourceMap || config.options.map
9589
let plugins = config.plugins || []
96-
let options = Object.assign({
90+
91+
// Disable override `to` option
92+
if (config.options.to) delete config.options.to
93+
// Disable override `from` option
94+
if (config.options.from) delete config.options.from
95+
// Disable override `map` option
96+
if (config.options.map) delete config.options.map
97+
98+
let postcssOption = Object.assign({
9799
to: file,
98100
from: file,
99101
map: sourceMap
@@ -105,20 +107,20 @@ module.exports = function loader (css, map) {
105107

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

112-
if (typeof options.parser === 'string') {
113-
options.parser = require(options.parser)
114+
if (typeof postcssOption.parser === 'string') {
115+
postcssOption.parser = require(postcssOption.parser)
114116
}
115117

116-
if (typeof options.syntax === 'string') {
117-
options.syntax = require(options.syntax)
118+
if (typeof postcssOption.syntax === 'string') {
119+
postcssOption.syntax = require(postcssOption.syntax)
118120
}
119121

120-
if (typeof options.stringifier === 'string') {
121-
options.stringifier = require(options.stringifier)
122+
if (typeof postcssOption.stringifier === 'string') {
123+
postcssOption.stringifier = require(postcssOption.stringifier)
122124
}
123125

124126
// Loader API Exec (Deprecated)
@@ -132,10 +134,10 @@ module.exports = function loader (css, map) {
132134
}
133135

134136
if (sourceMap && typeof map === 'string') map = JSON.parse(map)
135-
if (sourceMap && map) options.map.prev = map
137+
if (sourceMap && map) postcssOption.map.prev = map
136138

137139
return postcss(plugins)
138-
.process(css, options)
140+
.process(css, postcssOption)
139141
.then((result) => {
140142
result.warnings().forEach((msg) => this.emitWarning(msg.toString()))
141143

lib/options.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ module.exports = function parseOptions (params) {
1111
else if (Array.isArray(params.plugins)) plugins = params.plugins
1212
else plugins = params.plugins
1313

14-
const options = {}
15-
16-
if (typeof params !== 'undefined') {
17-
options.parser = params.parser
18-
options.syntax = params.syntax
19-
options.stringifier = params.stringifier
14+
const options = {
15+
parser: params.parser,
16+
syntax: params.syntax,
17+
stringifier: params.stringifier
2018
}
2119

2220
const exec = params && params.exec

lib/options.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"type": "object",
33
"properties": {
44
"config": {
5-
"type": "object",
5+
"type": ["object", "boolean"],
66
"properties": {
77
"path": {
88
"type": "string"

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"docs": "jsdoc2md lib/index.js > LOADER.md",
3434
"pretest": "npm run lint && npm run test:build",
3535
"test": "jest",
36+
"test-only": "npm run test:build && jest && npm run clean",
3637
"test:build": "node test/webpack.build.js",
3738
"posttest": "npm run clean",
3839
"release": "standard-version"

0 commit comments

Comments
 (0)