From b21cc3adf5f6892b3aeea1da42614dd4a4045cbd Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Tue, 29 Nov 2022 23:02:33 +0300 Subject: [PATCH 1/2] fix: support ESM version of `postcss.config.js` --- src/utils.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/src/utils.js b/src/utils.js index 83384e20..2d677817 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,8 +1,9 @@ import path from "path"; +import url from "url"; import Module from "module"; import { klona } from "klona/full"; -import { cosmiconfig } from "cosmiconfig"; +import { cosmiconfig, defaultLoaders } from "cosmiconfig"; import SyntaxError from "./Error"; @@ -47,7 +48,80 @@ async function loadConfig(loaderContext, config, postcssOptions) { throw new Error(`No PostCSS config found in: ${searchPath}`); } - const explorer = cosmiconfig("postcss"); + const moduleName = "postcss"; + const explorer = cosmiconfig(moduleName, { + searchPlaces: [ + "package.json", + `.${moduleName}rc`, + `.${moduleName}rc.json`, + `.${moduleName}rc.yaml`, + `.${moduleName}rc.yml`, + `.${moduleName}rc.js`, + `.${moduleName}rc.mjs`, + `.${moduleName}rc.cjs`, + `.config/${moduleName}rc`, + `.config/${moduleName}rc.json`, + `.config/${moduleName}rc.yaml`, + `.config/${moduleName}rc.yml`, + `.config/${moduleName}rc.js`, + `.config/${moduleName}rc.mjs`, + `.config/${moduleName}rc.cjs`, + `${moduleName}.config.js`, + `${moduleName}.config.mjs`, + `${moduleName}.config.cjs`, + ], + loaders: { + ".js": async (...args) => { + let result; + + try { + result = defaultLoaders[".js"](...args); + } catch (error) { + let importESM; + + try { + // eslint-disable-next-line no-new-func + importESM = new Function("id", "return import(id);"); + } catch (e) { + importESM = null; + } + + if ( + error.code === "ERR_REQUIRE_ESM" && + url.pathToFileURL && + importESM + ) { + const urlForConfig = url.pathToFileURL(args[0]); + + result = await importESM(urlForConfig); + } else { + throw new Error("ESM is not supported"); + } + } + + return result; + }, + ".mjs": async (...args) => { + let result; + let importESM; + + try { + // eslint-disable-next-line no-new-func + importESM = new Function("id", "return import(id);"); + } catch (e) { + importESM = null; + } + + if (url.pathToFileURL && importESM) { + const urlForConfig = url.pathToFileURL(args[0]); + + result = await importESM(urlForConfig); + } + + return result; + }, + }, + }); let result; From c95cd7071c87e37cf97dbde7e2bd17dfe5f4e93c Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Tue, 29 Nov 2022 23:11:45 +0300 Subject: [PATCH 2/2] fix: error reporting logic --- src/utils.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 2d677817..742381df 100644 --- a/src/utils.js +++ b/src/utils.js @@ -95,7 +95,7 @@ async function loadConfig(loaderContext, config, postcssOptions) { result = await importESM(urlForConfig); } else { - throw new Error("ESM is not supported"); + throw error; } } @@ -116,6 +116,8 @@ async function loadConfig(loaderContext, config, postcssOptions) { const urlForConfig = url.pathToFileURL(args[0]); result = await importESM(urlForConfig); + } else { + throw new Error("ESM is not supported"); } return result;