Skip to content
This repository was archived by the owner on Oct 29, 2021. It is now read-only.

Commit aad193a

Browse files
committed
Closely imitate postcss-loader's config logic
This will make css-customs-loader behave more like postcss-loader.
1 parent c3ba0d7 commit aad193a

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

lib/index.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,24 @@ module.exports = async function(content, sourceMap, meta) {
5050
let postcssPlugins
5151
let postcssOptions
5252

53-
if (postcssLoader.options == null) {
53+
// imitating postcss-loader behavior
54+
// https://github.com/postcss/postcss-loader/blob/master/src/index.js
55+
56+
if (
57+
Object.keys(postcssLoader.options || {}).filter(
58+
option => !['ident', 'config', 'sourceMap'].includes(option)
59+
).length > 0
60+
) {
61+
const { options, plugins } = await parsePostcssLoaderOptions.call(
62+
this,
63+
postcssLoader.options
64+
)
65+
postcssOptions = options
66+
postcssPlugins = plugins
67+
} else {
5468
const file = this.resourcePath
5569
const rc = {
70+
path: path.dirname(file),
5671
ctx: {
5772
cwd: this.context,
5873
file: {
@@ -61,8 +76,18 @@ module.exports = async function(content, sourceMap, meta) {
6176
basename: path.basename(file),
6277
},
6378
options: {},
79+
webpack: this,
6480
},
65-
path: path.dirname(file),
81+
}
82+
83+
if (postcssLoader.options != null) {
84+
const { config = {} } = postcssLoader.options
85+
if (config.path != null) {
86+
rc.path = path.resolve(config.path)
87+
}
88+
if (config.ctx != null) {
89+
rc.ctx.options = config.ctx
90+
}
6691
}
6792

6893
try {
@@ -72,14 +97,14 @@ module.exports = async function(content, sourceMap, meta) {
7297
} catch (err) {
7398
return callback(err)
7499
}
75-
} else {
76-
const { options, plugins } = await parsePostcssLoaderOptions(
77-
postcssLoader.options
78-
)
79-
postcssOptions = options
80-
postcssPlugins = plugins
81100
}
82101

102+
;['parser', 'syntax', 'stringifier'].forEach(option => {
103+
if (typeof postcssOptions[option] === 'string') {
104+
postcssOptions[option] = require(option)
105+
}
106+
})
107+
83108
postcssPlugins = postcssPlugins.map(plugin =>
84109
plugin.postcssPlugin == null ? plugin() : plugin
85110
)
@@ -106,6 +131,7 @@ module.exports = async function(content, sourceMap, meta) {
106131
]).process(css, {
107132
...postcssOptions,
108133
from: this.resourcePath,
134+
to: undefined,
109135
})
110136

111137
const exportContent = [

0 commit comments

Comments
 (0)