热装似乎不再起作用。我不确定在我的设置中有什么可以更改的,事实上,据我所知,git日志显示,没有任何更改。
无论如何,我试图重新加载CSS并使.jsx
文件Reacts正常工作。
当前,我在另一个控制台选项卡中运行npm run watch
,然后运行npm run start
。应用程序启动,但是文件中的任何更改都不会重新加载。我的package.json如下所示:
{
...
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "cross-env ENVIRONMENT=DEV electron main.js",
"watch": "webpack-dev-server --hot --inline",
"build": "webpack"
},
"dependencies": {
"axios": "^0.15.2",
"babel": "^6.5.2",
"babel-loader": "^6.2.5",
"babel-plugin-transform-class-properties": "^6.19.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babelify": "^7.2.0",
"cross-env": "^3.1.1",
"electron": "^1.3.4",
"electron-prebuilt": "^1.4.2",
"json-loader": "^0.5.4",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"react-router": "^2.8.1",
"react-tap-event-plugin": "^1.0.0",
"semantic-ui-react": "^0.56.13",
"socket.io-client": "^1.5.0",
"store": "^1.3.20",
"webpack": "^1.13.3"
},
"devDependencies": {
"css-loader": "^0.25.0",
"style-loader": "^0.13.1",
"webpack-dev-server": "^1.16.2"
}
}
省略了一些不必要的依赖项。我的
webpack.config.js
如下所示:var path = require("path");
module.exports = {
entry: path.resolve(__dirname, "app/index.jsx"),
output: {
path: path.resolve(__dirname, "build"),
publicPath: 'http://localhost:8080/build/',
filename: 'app.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel',
exclude: /node_modules/
},
{
test: /\.css$/,
loader: "style-loader!css-loader"
},
{
test: /\.json$/,
loader: "json-loader"
}
]
}
};
在此设置中进行热重装时,我缺少什么?
编辑:如您所见,入口点定义为
index.jsx
。该文件非常简单,如下所示:import React from 'react';
import ReactDOM from 'react-dom';
import styles from './styles.css'
import routes from './router.jsx'
ReactDOM.render(
routes,
document.getElementById('app')
);
每当我对此文件进行更改时,都会触发热重装。此外,还会重新加载对此处导入的文件所做的更改(例如styles.css)。问题是,我所有的组件显然都导入了路由器。因此,对我的组件所做的任何更改都不会被热加载。如何使它正常工作?
最佳答案
我在webpack.config.js
中使用以下选项:
var path = require('path');
var webpack = require('webpack');
module.exports = {
name: 'client',
entry: './client.js',
output: {
path: __dirname, filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?$/, // Match both .js and .jsx files
exclude: /node_modules/,
loader: "babel-loader",
query:
{
presets: ['es2015', 'react']
}
}
]
},
devServer: {
historyApiFallback: true
},
}
并放入
package.json
这个选项: "scripts": {
"start": "npm run dev",
"webpack": "webpack --progress --colors",
"dev": "webpack-dev-server --devtool eval --progress --colors --inline"
},