1

My problem is, no matter what I do I get a 404 on the wepack_hmr and I can't for the life of me work out why its not available.

Full Webpack Config

var url = 'http://localhost:3000';

module.exports = {

    resolve: {
        extensions: ['', '.js']
    },

    entry: ['webpack-hot-middleware/client','./src/client/js/Kindred'],

    devtool: 'cheap-module-source-map',

    module: {
        loaders:[
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel',
                query: {presets: ['es2015', 'react', 'react-hmre', 'stage-0']}
            },
            {test: /\.png$/, loader: "url-loader?limit=100000"},
            // Images
            {test: /\.jpg$/, loader: "file-loader"},
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                loader: 'file?name=./font/[name].[ext]'
            },
            // Stylesheets
            { test: /\.scss$/, loader: ExtractTextPlugin.extract('style', 
                [
                    'css?sourceMap&modules&importLoaders=1',
                    'sass?sourceMap&modules',
                ]
            ) },
            // Font Definitions
            { test: /\.svg$/, loader: 'url?limit=65000&mimetype=image/svg+xml&name=public/font/[name].[ext]' },
            { test: /\.woff$/, loader: 'url?limit=65000&mimetype=application/font-woff&name=public/font/[name].[ext]' },
            { test: /\.woff2$/, loader: 'url?limit=65000&mimetype=application/font-woff2&name=public/font/[name].[ext]' },
            { test: /\.[ot]tf$/, loader: 'url?limit=65000&mimetype=application/octet-stream&name=public/font/[name].[ext]' },
            { test: /\.eot$/, loader: 'url?limit=65000&mimetype=application/vnd.ms-fontobject&name=public/font/[name].[ext]' }
        ]
    },

    sassLoader: {
        includePaths: [ 'src/client/scss' ]
    },

    plugins: process.env.NODE_ENV === 'production' ? [
        new ExtractTextPlugin ('app.css', {allChunks: true}),
        new webpack.optimize.DedupePlugin (),
        new webpack.optimize.OccurrenceOrderPlugin (),
        new webpack.optimize.UglifyJsPlugin ()
    ] :  [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new ExtractTextPlugin("style.css")
    ],

    devServer: {
        hot: true,
        contentBase: './public'
    },

    output: {
        path: path.join(__dirname),
        publicPath: '/',
        filename: 'bundle.js'
    }

};

Node cmd to run

webpack-dev-server --inline --history-api-fallback --port 3000

When loading up the page

[HMR] Waiting for update signal from WDS...
home:1 GET http://localhost:3000/__webpack_hmr 
client:22 [WDS] Hot Module Replacement enabled.

It takes me its running but its not :/

Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291
  • 1
    Webpack hmr is a tricky devil to get working properly. It is not enjoyable enough that I would be able to do it for you. I recommend you review one of the available react boilerplate's that include it, and look for differences between your config. https://github.com/gaearon/react-transform-boilerplate – John Aug 05 '16 at 19:42
  • Haha cheers john :) I appreciate the help and also insight that this might get ugly. – Jamie Hutber Aug 05 '16 at 19:44
  • Yeah sorry heh, I usually enjoy helping people solve react related problems and don't mind jumping in writing code but webpack config is the worst.. lol – John Aug 05 '16 at 20:40
  • 1
    Coming from grunt -> gulp -> webpack. I must say webpack seems the most black box to me. But thats almost certainly because I've not read the documentation like the others. :) – Jamie Hutber Aug 05 '16 at 21:51
  • Yeah it's just got a lot it can do so that entails a lot of config and a lot of docs; plus you only really set it up at the beginning of your project so it's not something that you're really doing day in day out – John Aug 05 '16 at 22:53
  • By the looks of it you are missing certain client entry. Hence the error. You could try running the dev server in inline mode to avoid problems with entries. – Juho Vepsäläinen Aug 08 '16 at 03:16
  • 1
    webpack-hot-middleware - I believe this one works only with express, you are using webpack dev server here. Also HMR doesn't work with ExtractTextPlugin. – Dauren Akilbekov Aug 08 '16 at 14:47

1 Answers1

1

Ok just so its easy for people to see what I did to get it working here it is:

I think it was easier for me to completely remove scss and just go with css modules out of the box. This seemed to help a lot.

I used git@github.com:christianalfoni/webpack-express-boilerplate.git to help me guide myself through. Knowing I had a working thing actually taught me a hell of a lot more than I have most likely learnt before hand in webpack. As you can guess that wasn't very much :D

Also the one main change that really helped was changing the paths not surprisingly. But these were relative to the path sex in output.path I had read this before but thought that was relative to the wepack.config.js not that from there on everything would count as document root, even for the html and css.

**KEY PART :) **

I also have to update my express set up, as I hadn't given it a express.static path... Oh my folly, how could I miss such a basic thing.. So:

Express

app.use(express.static(__dirname + '/public/')); //Don't forget me :(

Final CSS

/* Webfont: Lato-Black */@font-face {
    font-family: 'LatoBlack';
    src: url('/font/Lato-Black.eot'); /* IE9 Compat Modes */
    src: url('/font/Lato-Black.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
         url('/font/Lato-Black.woff2') format('woff2'), /* Modern Browsers */
         url('/font/Lato-Black.woff') format('woff'), /* Modern Browsers */
         url('/font/Lato-Black.ttf') format('truetype');
    font-style: normal;
    font-weight: normal;
    text-rendering: optimizeLegibility;
}

Wekpack.config

'use strict';
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var qs = require('querystring');
var precss = require('precss');
var autoprefixer = require('autoprefixer');
var stripInlineComments = require('postcss-strip-inline-comments');

module.exports = {
    devtool: 'eval-source-map',
    // resolve: { modulesDirectories: ['node_modules'], extension: ['', '.js', '.css'] },
    entry: [
        'webpack-hot-middleware/client?reload=true',
        path.join(__dirname, 'src/client/js/Kindred')
        // path.join(__dirname, 'app/main')
    ],
    output: {
        path: path.join(__dirname, '/public/'),
        filename: '[name].js',
        publicPath: '/'
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'public/index.tpl.html',
            inject: 'body',
            filename: 'index.html'
        }),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify('development')
        })
    ],
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                loader: 'babel',
                query: {"presets": ["react", "es2015", "stage-0", "react-hmre"]}
            }, {
                test: /\.json?$/,
                loader: 'json'
            },
            {
                test: /\.jpg$/, loader: "file-loader"
            },
            {
                test: /\.css$/,
                loaders: [
                    'style-loader',
                    'css-loader?importLoaders&' + qs.stringify({
                        modules: true,
                        importLoaders: 1,
                        localIdentName: '[path][name]-[local]'
                    }),
                    'postcss-loader?parser=postcss-scss'
                ]
            },
            // Font Definitions
            { test: /\.svg$/, loader: 'url?limit=65000&mimetype=image/svg+xml&name=font/[name].[ext]' },
            { test: /\.woff$/, loader: 'url?limit=65000&mimetype=application/font-woff&name=font/[name].[ext]' },
            { test: /\.woff2$/, loader: 'url?limit=65000&mimetype=application/font-woff2&name=font/[name].[ext]' },
            { test: /\.[ot]tf$/, loader: 'url?limit=65000&mimetype=application/octet-stream&name=font/[name].[ext]' },
            { test: /\.eot$/, loader: 'url?limit=65000&mimetype=application/vnd.ms-fontobject&name=font/[name].[ext]' }
        ]
    },
    postcss: function (webpack) {
        return [
            stripInlineComments
            , precss
            , autoprefixer
            , require('postcss-simple-vars')
            , require('postcss-nested'
            , autoprefixer({browsers: ['last 2 versions']}))
        ];
    }
};

I have actually posted the same answer twice. But they are both related. Sadly :(

Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291