Closed GamzeeRakoon closed 1 year ago
forgot i made my repo private, its public now sorry for the inconvience
You can't use:
import autoprefixer from 'autoprefixer',
import cssnano from 'cssnano' ({
reduceIdents: {keyframes: false},
discardUnused: {keyframes: false}
})
it is invalid syntax
while this is true using
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
export default {
plugins: [
cssnano,
autoprefixer
]
};
still gives the same error of a postcss-loader dependency not supporting ESM
Please use:
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
still gives the error that a dependencie of postcss loader doesnt support esm and fails
Same issue here.
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
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 errorPlease paste the results of
npx webpack info
here, and mention other relevant information