1

There is a simple config for webpack 4

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
    entry: {
        app: './src/index.js',
    },
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, './dist'),
        publicPath: '/dist',
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: '[name].css',
        })
    ],
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: '/node_modules/'
            },
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader'
                ],
            },
        ],
    },
}

CSS-file is imported to the entry point (index.js), it is necessary to output it in the production build to a separate CSS-file (using mini-css-extract-plugin, want to try exactly this plugin, not other methods). But an error occurs: https://i.stack.imgur.com/VoqTW.png

Here is full file structure https://github.com/DazzRune/webpack4test

DazzRune
  • 11
  • 4

2 Answers2

0

Everything works quite fine, webpack is compiling without any errors. Try to do npm install, as you have all necessary loaders to do the task.

Alonad
  • 1,986
  • 19
  • 17
  • I tried to install again, then `npm run build` and got the same error like on my screenshot. – DazzRune Oct 20 '19 at 17:18
  • the error says that you don't have appropriate loader to handle css file, while in your config you have it. Besides on my machine compilation succeeded without any error. – Alonad Oct 20 '19 at 18:56
0

You can try this:

{
                test: /\.css$/,
                use: [{
                    loader: MiniCssExtractPlugin.loader,
                    options: {
                        publicPath: (resourcePath, context) => {
                            return path.relative(path.dirname(resourcePath), context) + '/';
                        }
                    }
                },
                'css-loader'
            ]
            }

and

new MiniCssExtractPlugin({
            filename: "[name].css",
            chunkFilename: "[id].css",
            ignoreOrder: false
        })

It is working with me All credit to this guy!

Waleed
  • 1