-
-
Notifications
You must be signed in to change notification settings - Fork 211
postcss-loader dependency does not support ESM #613
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
Comments
forgot i made my repo private, its public now sorry for the inconvience |
You can't use:
it is invalid syntax |
while this is true using
still gives the same error of a postcss-loader dependency not supporting ESM |
Please use:
|
still gives the error that a dependencie of postcss loader doesnt support esm and fails |
Same issue here. |
Related: cosmiconfig/cosmiconfig#224 |
Okay, let's fix it on our side, firstly, please fix your configuration: import path from 'path';
import url from 'url';
import webpack from 'webpack';
import * as MiniCssExtractPlugin from 'mini-css-extract-plugin';
const extractSASS = new MiniCssExtractPlugin.default({filename: "app.min.css"});
const jQuery = new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
});
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default {
mode: "production",
entry: {
main: [
"./src/js/index.js"
],
},
output: {
filename: "app.min.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.default.loader
},
{
loader: "css-loader"
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
path: path.resolve(__dirname, "postcss.config.js")
}
}
},
{
loader: "sass-loader"
}
]
}
]
},
plugins: [
extractSASS,
jQuery
]
}; the second please fix your postcss.config.js import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
export default {
plugins: [
autoprefixer(),
cssnano({
reduceIdents: {keyframes: false},
discardUnused: {keyframes: false}
})
]
}; and then we will aply this patch on our code: const moduleName = "postcss";
const explorer = (0, _cosmiconfig.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 = _cosmiconfig.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" &&
require("url").pathToFileURL &&
importESM
) {
const urlForConfig = require("url").pathToFileURL(args[0]);
result = await importESM(urlForConfig);
} else {
throw requireError;
}
}
return result;
},
'.mjs': async (...args) => {
let importESM;
try {
// eslint-disable-next-line no-new-func
importESM = new Function("id", "return import(id);");
} catch (e) {
importESM = null;
}
if (
require("url").pathToFileURL &&
importESM
) {
const urlForConfig = require("url").pathToFileURL(args[0]);
result = await importESM(urlForConfig);
}
return result;
}
}
}); And now ESM configuration will work |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
while compiling css with postcss-loader it uses the dependency cosmiconfig but this does not support ESM type
and will give the error
Actual Behavior
when postcss loader uses cosmiconfig it uses non esm compatible code and gives an error and fails to compile the css
Expected Behavior
postcss loader should compile the css while being ESM compatible
How Do We Reproduce?
heres my testing repository:
https://github.com/LithRakoon/npm
npm i
npm run build
see error
Please paste the results of
npx webpack info
here, and mention other relevant informationThe text was updated successfully, but these errors were encountered: