Error loading .css files
I've experienced an error while trying to build a library. It just appears:
"Failed to compile: TypeError: This[MODULE_TYPE] is not a function" and it's only a .css file without any less/sass code on it.
Any idea?
Try looking at https://github.com/webpack-contrib/mini-css-extract-plugin/issues/73 It seems to indicate that this issue is actually a misconfiguration where a loader is added for the css but the related plugin is missing, or that the plugin is in the wrong order (needs to be before the HtmlWebpackPlugin if they're both there).
It looks like an error in what your config-overrides.js function is doing rather than being an error caused by react-app-rewired.
Same problem here! Any updates ?
Try looking at webpack-contrib/mini-css-extract-plugin#73 It seems to indicate that this issue is actually a misconfiguration where a loader is added for the css but the related plugin is missing, or that the plugin is in the wrong order (needs to be before the HtmlWebpackPlugin if they're both there).
It looks like an error in what your config-overrides.js function is doing rather than being an error caused by react-app-rewired.
I'm just using react-app-rewire-create-react-library with CRA without ejecting, so I don't have any extra config file besides config-overrides.js, that only includes the before mentioned package to override...
Same problem here! Any updates ?
Still not...
- You had not given any detail on what your config-overrides.js contained.
- You had not given any detail on what version of create-react-app you were using.
- You had not provided an example repository that showed the issue. Based on that, I had provided the only information I could which was detail on what the error message meant.
Now that you have at least mentioned what rewire you were using, I can add a little more detail based on guesses of what I think you are using.
Conditions I believe you're working with:
- You're using the new major update of create-react-app version 2 includes a major change from using Webpack 2/3 to using Webpack 4.
- The css is working properly in
yarn start, but not inyarn build.
If those conditions are correct, then the bug I listed is actually the issue. The problem is that one of the changes in create-react-app is they added the mini-css-extract-plugin to the production build configuration for webpack (i.e. what is used for yarn build).
https://github.com/facebook/create-react-app/blob/5fecfee2373ebd1eda0c405b82a1c2d24afae6a1/packages/react-scripts/config/webpack.config.prod.js#L64-L71
https://github.com/facebook/create-react-app/blob/5fecfee2373ebd1eda0c405b82a1c2d24afae6a1/packages/react-scripts/config/webpack.config.prod.js#L494-L499
The react-app-rewire-create-react-library rewire hasn't been updated for the last year so isn't aware of the change to the webpack 4 configuration, and is removing all plugins from the webpack config.plugins array. This means that the loader for mini-css-extract-plugin has been added for the css but the plugin initialization for mini-css-extract-plugin has been deleted by the rewire, resulting in the error message you are receiving.
https://github.com/osdevisnot/react-app-rewire-create-react-library/blob/b3a123efd5fad216906182f0e6ef8cbbfff5d5f4/index.js#L46-L49
If you want to continue to use the rewire, you will need to add the mini-css-extract-plugin back into the plugins array after calling the react-app-rewire-create-react-library rewire. For example:
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const rewireCreateReactLibrary = require('react-app-rewire-create-react-library');
module.exports = function(config, env) {
config = rewireCreateReactLibrary(config, env);
if (env === 'production') {
// Add the MiniCssExtractPlugin back into the plugins array as it is _required_ for CRA 2.
config.plugins.push(new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: 'static/css/[name].[contenthash:8].css',
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
}));
}
return config;
}
Create-React-App 2 is very new, and most rewires won't have been checked for compatibility yet. Some will "just work" anyway, but it is worth taking a bit of a look at what the rewire is actually doing if you are getting odd errors. For this error message in particular, you need to check what the rewire does to the config.plugins array as you will need to re-add the mini-css-extract-plugin to the array if any rewire you use has deleted it.
@jjuradogi you may need this:
module.exports = {
//...
plugins: [
new MiniCssExtractPlugin(),
],
}
This works for me
const path = require('path')
const webpack = require('webpack')
const HtmlWebPackPlugin = require("html-webpack-plugin")
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: './src/client/index.js',
mode: 'production',
module: {
rules: [
{
test: '/\.js$/',
exclude: /node_modules/,
loader: "babel-loader"
},
{
test: /\.scss$/,
use: [ MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader' ]
}
]
},
plugins: [
new MiniCssExtractPlugin(),
new HtmlWebPackPlugin({
template: "./src/client/views/index.html",
filename: "./index.html",
})
]
}