I'm not sure why my front end isn't running in hot mode. I am trying to decouple the front end and backend as much as possible. The only thing that connects the two is that I use django's webpack_loader
app to load the webpack
build to load into a template (index.html
).
My backend is in Django and it is currently running on http://127.0.0.1:8000/
.
My frontend is hosted on localhost:8080
My devServer for hot mode is on 0.0.0.0:8080
I tried changing a bit of my front end and saving it to see what would happen and nothing on the front end has been updated. In my developer tools I see this:
[HMR] Update check failed: SyntaxError: Unexpected token <
at Object.parse (native)
at XMLHttpRequest.request.onreadystatechange (http://localhost:8000/static/js/main.js:44:33)
Here are my npm scripts:
"scripts": {
"watch-client": "node ./node_modules/webpack/bin/webpack.js --verbose --colors --display-error-details --config webpack.watch.js && node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js --config webpack.watch.js",
"watch": "npm run watch-client"
},
webpack.config.js:
var path = require('path')
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry: [
'./app/index.js'
],
output: {
path: path.resolve(__dirname, 'public'),
publicPath: '/js/',
filename: 'main.js'
},
devtool: 'eval',
debug: true,
module: {
loaders: [
{ test: /\.json$/, loader: 'json-loader' },
{ test: /\.jsx$/, loader: 'jsx-loader' },
{ test: /\.es6$/, exclude: /node_modules/, loader: 'babel-loader?stage=0&optional=runtime'},
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?stage=0&optional=runtime'},
{ test: /\.less$/, exclude: /node_modules/, loader: ExtractTextPlugin.extract("style-loader", "css-loader")},
{ test: /\.css$/, exclude: /node_modules/, loader: 'style-loader!css-loader' }
]
},
plugins: [
new ExtractTextPlugin('style.css', {
allChunks: true
}),
new webpack.optimize.CommonsChunkPlugin('common.js'),
]
};
webpack.watch.js:
var Webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var config = require("./webpack.config.js");
var BundleTracker = require('webpack-bundle-tracker');
var port = process.env.WEBPACK_PORT || 8080;
var host = process.env.HOST || 'localhost';
// add hot loader
config.entry.unshift(
"webpack-dev-server/client?http://" + host + ":" + port,
"webpack/hot/only-dev-server" // only prevents reload on syntax errors
);
config.plugins = [
new ExtractTextPlugin("style.css", {
allChunks: true
}),
new Webpack.HotModuleReplacementPlugin(),
new BundleTracker({filename: './webpack-stats.json'})
];
config.module.loaders = [
{ test: /\.json$/, loader: 'json-loader' },
{ test: /\.jsx$/, loader: 'jsx-loader' },
{ test: /\.es6$/, exclude: /node_modules/, loader: 'babel-loader?stage=0&optional=runtime'},
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?stage=0&optional=runtime'},
{ test: /\.less$/, exclude: /node_modules/, loader: ExtractTextPlugin.extract("style-loader", "css-loader")},
{ test: /\.css$/, exclude: /node_modules/, loader: 'style-loader!css-loader' }
];
// dev server for react hot loader
config.devServer = {
publicPath: "/js/",
filename: 'main.js',
contentBase: "./public",
hot: true,
inline: true,
lazy: false,
quiet: true,
noInfo: true,
headers: {"Access-Control-Allow-Origin": "*"},
stats: {colors: true},
host: "0.0.0.0",
port: port
};
module.exports = config;