peter
peter

Reputation: 257

webpack 4 config, reloading page crashes

Trying to migrate to webpack 4 (from 3). I have tried with a very basic config, it works properly when running webpack-dev-server --mode development but when I enter admin subpage (localhost:3000/admin/test) and reload page, it crashes. It wants to find the main.js file in /admin/main.js, but it is located in /main.js.

Following error is received: Refused to execute script from 'http://localhost:3000/admin/main.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

Any idea?

const path = require("path");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const devMode = process.env.NODE_ENV !== 'production'

module.exports = (env) => {
return {
    entry: { main: './src/index.js' },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'main.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: "html-loader",
                        options: {minimize: true}
                    }
                ]
            },
            {
                test: /\.(scss|css)$/,
                use: [
                    devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
                    'css-loader'
                ],
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: "[path][name].[hash].[ext]",
                        }
                    }
                ]
            }]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: "./public/index.html",
            filename: "./index.html"
        }),
        new MiniCssExtractPlugin({
            filename: devMode ? '[name].css' : '[name].[hash].css',
            chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
        }),
    ],
    devtool: devMode ? 'cheap-module-eval-source-map' : 'source-map',
    devServer: {
        contentBase: path.join(__dirname, 'public'),
        historyApiFallback: true,
    },
};
};

Upvotes: 0

Views: 805

Answers (2)

peter
peter

Reputation: 257

I added inject: false to my HtmlWebPackPlugin:

new HtmlWebPackPlugin({
            inject: false,
            template: "./public/index.html",
            filename: "./index.html"
        }),

And included the main.js in the index.html file:

Upvotes: 0

Sanzhar Dan
Sanzhar Dan

Reputation: 393

Webpack generate main.js file in dist/ folder. You subpage requests it in admin/ folder. Open the HTML file that represents this page and change the path.

Upvotes: 1

Related Questions