From b458828829e75795435b41ad0b7fa8da67c1f50d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Wouts?= Date: Thu, 10 Feb 2022 23:40:34 +1100 Subject: [PATCH] fix: require PostCSS plugins based on the current file path instead of process.cwd() --- README.md | 6 ++++++ package.json | 3 ++- src/options.js | 11 ++++------- src/plugins.js | 9 +++------ src/req.js | 7 +++++++ 5 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 src/req.js diff --git a/README.md b/README.md index cb9bf01..a72bade 100644 --- a/README.md +++ b/README.md @@ -458,6 +458,12 @@ module.exports = (env) => ({
Dalton Santos + + +
+ François Wouts + diff --git a/package.json b/package.json index 7b84f4b..de67bc7 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,8 @@ "Ryan Dunckel", "Mateusz Derks", "Dalton Santos", - "Patrick Gilday" + "Patrick Gilday", + "François Wouts" ], "repository": "postcss/postcss-load-config", "license": "MIT" diff --git a/src/options.js b/src/options.js index 84d7b9d..d3ef2d6 100644 --- a/src/options.js +++ b/src/options.js @@ -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 @@ -18,7 +15,7 @@ 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})`) } @@ -26,7 +23,7 @@ const options = (config, 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})`) } @@ -34,7 +31,7 @@ const options = (config, 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})`) } diff --git a/src/plugins.js b/src/plugins.js index f1ef631..6cb994b 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -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 @@ -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})`) diff --git a/src/req.js b/src/req.js new file mode 100644 index 0000000..1d2985d --- /dev/null +++ b/src/req.js @@ -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