Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: load PostCSS plugins based on the current file path instead of process.cwd() #229

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ module.exports = (env) => ({
<br />
<a href="https://github.com/daltones">Dalton Santos</a>
</td>
<td align="center">
<img width="150" height="150"
src="https://github.com/fwouts.png?v=3&s=150">
<br />
<a href="https://github.com/fwouts">François Wouts</a>
</td>
</tr>
<tbody>
</table>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"Ryan Dunckel",
"Mateusz Derks",
"Dalton Santos",
"Patrick Gilday"
"Patrick Gilday",
"François Wouts"
],
"repository": "postcss/postcss-load-config",
"license": "MIT"
Expand Down
11 changes: 4 additions & 7 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict'

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

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

if (config.syntax && typeof config.syntax === 'string') {
try {
config.syntax = req(config.syntax)
config.syntax = req(config.syntax, file)
} catch (err) {
throw new Error(`Loading PostCSS Syntax failed: ${err.message}\n\n(@${file})`)
}
}

if (config.stringifier && typeof config.stringifier === 'string') {
try {
config.stringifier = req(config.stringifier)
config.stringifier = req(config.stringifier, file)
} catch (err) {
throw new Error(`Loading PostCSS Stringifier failed: ${err.message}\n\n(@${file})`)
}
Expand Down
9 changes: 3 additions & 6 deletions src/plugins.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict'

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

/**
* Plugin Loader
Expand All @@ -23,9 +20,9 @@ const load = (plugin, options, file) => {
options === undefined ||
Object.keys(options).length === 0
) {
return req(plugin)
return req(plugin, file)
} else {
return req(plugin)(options)
return req(plugin, file)(options)
}
} catch (err) {
throw new Error(`Loading PostCSS Plugin failed: ${err.message}\n\n(@${file})`)
Expand Down
7 changes: 7 additions & 0 deletions src/req.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// eslint-disable-next-line node/no-deprecated-api
const { createRequire, createRequireFromPath } = require('module')
const path = require('path')

const req = (moduleId, file) => (createRequire || createRequireFromPath)(path.resolve(file, '_'))(moduleId)

module.exports = req