0

I am new to webpack so this is probably something silly.

My current file structure is as follows

---src
------css
---------colors.sass
---------main.sass
------img
---------img1.png
------js
---------app.js
---------alert.js
---index.html
---test.html
package.json
webpack.config.js

My webpack.config.js is as follows

//NPM
var path = require('path');
var webpack = require('webpack');
var _ = require('underscore');

//Plugins
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');

var extractPlugin = new ExtractTextPlugin({
    filename: 'main.css'
});

module.exports = {
    entry: {
        app: './src/js/app.js',
        alert: './src/js/alert.js'
    },
    devtool: 'eval',
    devServer: {
        contentBase: './dist',
        host: 'localhost',
        port: 8080,
        hot: true,
        noInfo: false,
        inline: true
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].[hash].js'
    },
    module: {
        rules: [{
                test: /\.js$/,
                loaders: ['babel-loader'],
                exclude: /node_modules/,
                include: __dirname
            },
            {
                test: /\.sass$/,
                use: extractPlugin.extract({
                    use: ['css-loader', 'sass-loader']
                })
            },
            {
                test: /\.html$/,
                use: ['html-loader']
            },
            {
                test: /\.(jpg|png)$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: 'img/'
                    }
                }]
            },
            {
                test: /\.html$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: '/'
                    }
                }],
                exclude: path.resolve(__dirname, 'index.html')
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            chunks: ['app'],
            filename: 'index.html',
            template: './src/index.html'
        }),
        new HtmlWebpackPlugin({
            chunks: ['alert'],
            filename: 'test.html',
            template: './src/test.html'
        }),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery",
            _: "underscore"
        }),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.optimize.UglifyJsPlugin({
            // ...
        }),
        new CleanWebpackPlugin(['dist']),
        extractPlugin
    ]
};

My package.json scripts

"scripts": {
        "start": "webpack-dev-server --config webpack.config.js --inline --progress --colors",
        "build": "webpack --config webpack.config.js --progress --profile --colors",
        "prod": "webpack -p"
    },

In app.js & alert.js I simply have console logs. In index.html & test.html I just have a basic HTML5 layout and H1 tags with the page name.

I wanted to be able to go to index.html and only use app.js and in test.html to only use alert.js

Now, when I run npm run start I get the following appearing on the page, inside the body of the HTML.

module.exports = __webpack_public_path__ + "/index.html";

In the console, there are no errors but the console logs from either app.js or alert.js do appear, but the HTML doesn't render. Any idea why?

1 Answer 1

1

You defined two different rules for .html files.

{
    test: /\.html$/,
    use: ['html-loader']
},
{
    test: /\.html$/,
    use: [{
        loader: 'file-loader',
        options: {
            name: '[name].[ext]',
            outputPath: '/'
        }
    }],
    exclude: path.resolve(__dirname, 'index.html')
}

These two rules are conflicting (except for ./index.html) and you might get an undesired result. html-webpack-plugin will also apply any configured loaders to the template file. In your case it applied the file-loader to it, which copies the file and returns a reference to it (the export you got), such that the copied file can be referenced in your HTML. That is definitely not what you want. The easiest solution is to remove the second rule and only keep the html-loader.

{
    test: /\.html$/,
    use: ['html-loader']
},

If you would like to use file-loader for any other HTML files, you can do it by making sure to not apply the configured loader to the templates. There are several different options, one of which is using a different extension and as html-webpack-plugin will apply an EJS loader as a fallback if no loader is specified, you should use .ejs. See The template option for other options.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.