Skip to content

Commit 98da51e

Browse files
authored
fix: require PostCSS plugins based on the current file path instead of process.cwd() (#229)
1 parent 410413f commit 98da51e

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@ module.exports = (env) => ({
458458
<br />
459459
<a href="https://github.com/daltones">Dalton Santos</a>
460460
</td>
461+
<td align="center">
462+
<img width="150" height="150"
463+
src="https://github.com/fwouts.png?v=3&s=150">
464+
<br />
465+
<a href="https://github.com/fwouts">François Wouts</a>
466+
</td>
461467
</tr>
462468
<tbody>
463469
</table>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
"Ryan Dunckel",
6161
"Mateusz Derks",
6262
"Dalton Santos",
63-
"Patrick Gilday"
63+
"Patrick Gilday",
64+
"François Wouts"
6465
],
6566
"repository": "postcss/postcss-load-config",
6667
"license": "MIT"

src/options.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
'use strict'
22

3-
// eslint-disable-next-line node/no-deprecated-api
4-
const { createRequire, createRequireFromPath } = require('module')
5-
const path = require('path')
6-
const req = (createRequire || createRequireFromPath)(path.resolve(process.cwd(), '_'))
3+
const req = require('./req.js')
74

85
/**
96
* Load Options
@@ -18,23 +15,23 @@ const req = (createRequire || createRequireFromPath)(path.resolve(process.cwd(),
1815
const options = (config, file) => {
1916
if (config.parser && typeof config.parser === 'string') {
2017
try {
21-
config.parser = req(config.parser)
18+
config.parser = req(config.parser, file)
2219
} catch (err) {
2320
throw new Error(`Loading PostCSS Parser failed: ${err.message}\n\n(@${file})`)
2421
}
2522
}
2623

2724
if (config.syntax && typeof config.syntax === 'string') {
2825
try {
29-
config.syntax = req(config.syntax)
26+
config.syntax = req(config.syntax, file)
3027
} catch (err) {
3128
throw new Error(`Loading PostCSS Syntax failed: ${err.message}\n\n(@${file})`)
3229
}
3330
}
3431

3532
if (config.stringifier && typeof config.stringifier === 'string') {
3633
try {
37-
config.stringifier = req(config.stringifier)
34+
config.stringifier = req(config.stringifier, file)
3835
} catch (err) {
3936
throw new Error(`Loading PostCSS Stringifier failed: ${err.message}\n\n(@${file})`)
4037
}

src/plugins.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
'use strict'
22

3-
// eslint-disable-next-line node/no-deprecated-api
4-
const { createRequire, createRequireFromPath } = require('module')
5-
const path = require('path')
6-
const req = (createRequire || createRequireFromPath)(path.resolve(process.cwd(), '_'))
3+
const req = require('./req.js')
74

85
/**
96
* Plugin Loader
@@ -23,9 +20,9 @@ const load = (plugin, options, file) => {
2320
options === undefined ||
2421
Object.keys(options).length === 0
2522
) {
26-
return req(plugin)
23+
return req(plugin, file)
2724
} else {
28-
return req(plugin)(options)
25+
return req(plugin, file)(options)
2926
}
3027
} catch (err) {
3128
throw new Error(`Loading PostCSS Plugin failed: ${err.message}\n\n(@${file})`)

src/req.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// eslint-disable-next-line node/no-deprecated-api
2+
const { createRequire, createRequireFromPath } = require('module')
3+
const path = require('path')
4+
5+
const req = (moduleId, file) => (createRequire || createRequireFromPath)(path.resolve(file, '_'))(moduleId)
6+
7+
module.exports = req

0 commit comments

Comments
 (0)