Skip to content

Commit 531041a

Browse files
author
evilebottnawi
committed
refactor: improved loading algorithm of options and configs.
1 parent 2ef4449 commit 531041a

File tree

3 files changed

+50
-50
lines changed

3 files changed

+50
-50
lines changed

lib/index.js

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -47,53 +47,53 @@ 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: {}
59-
}
60-
}
61-
62-
if (options.config) {
63-
if (options.config.path) {
64-
rc.path = path.resolve(options.config.path)
50+
Promise.resolve().then(() => {
51+
if (options.exec || options.parser || options.syntax || options.stringifier || options.plugins) {
52+
return parseOptions.call(this, options)
6553
}
6654

67-
if (options.config.ctx) {
68-
rc.ctx.options = options.config.ctx
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+
}
6965
}
70-
}
7166

72-
const sourceMap = options.sourceMap
67+
// Read options from options.config only when options.config is object
68+
if (options.config) {
69+
if (options.config.path) {
70+
rc.path = path.resolve(options.config.path)
71+
}
7372

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)
73+
if (options.config.ctx) {
74+
rc.ctx.options = options.config.ctx
75+
}
8776
}
8877

8978
return postcssrc(rc.ctx, rc.path, { argv: false })
90-
}).then((config) => {
91-
if (!config) config = {}
79+
.then((config) => {
80+
// Get `sourceMap` option from loader options when no `map` option in `postcss.config.js`
81+
if (!config.options.map) config.options.map = options.sourceMap
9282

83+
return config
84+
})
85+
}).then((config) => {
9386
if (config.file) this.addDependency(config.file)
9487

88+
let 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+
96+
let postcssOption = Object.assign({
9797
to: file,
9898
from: file,
9999
map: sourceMap
@@ -105,20 +105,20 @@ module.exports = function loader (css, map) {
105105

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

112-
if (typeof options.parser === 'string') {
113-
options.parser = require(options.parser)
112+
if (typeof postcssOption.parser === 'string') {
113+
postcssOption.parser = require(postcssOption.parser)
114114
}
115115

116-
if (typeof options.syntax === 'string') {
117-
options.syntax = require(options.syntax)
116+
if (typeof postcssOption.syntax === 'string') {
117+
postcssOption.syntax = require(postcssOption.syntax)
118118
}
119119

120-
if (typeof options.stringifier === 'string') {
121-
options.stringifier = require(options.stringifier)
120+
if (typeof postcssOption.stringifier === 'string') {
121+
postcssOption.stringifier = require(postcssOption.stringifier)
122122
}
123123

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

134134
if (sourceMap && typeof map === 'string') map = JSON.parse(map)
135-
if (sourceMap && map) options.map.prev = map
135+
if (sourceMap && map) postcssOption.map.prev = map
136136

137137
return postcss(plugins)
138-
.process(css, options)
138+
.process(css, postcssOption)
139139
.then((result) => {
140140
result.warnings().forEach((msg) => this.emitWarning(msg.toString()))
141141

lib/options.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ 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,
18+
map: params.sourceMap
2019
}
2120

2221
const exec = params && params.exec

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"docs": "jsdoc2md lib/index.js > LOADER.md",
3333
"pretest": "npm run lint && npm run test:build",
3434
"test": "jest",
35+
"test-only": "npm run test:build && jest && npm run clean",
3536
"test:build": "node test/webpack.build.js",
3637
"posttest": "npm run clean",
3738
"release": "standard-version"

0 commit comments

Comments
 (0)