gpt4 book ai didi

reactjs - Electron/React 应用程序中的热重载

转载 作者:行者123 更新时间:2023-12-03 13:27:58 25 4
gpt4 key购买 nike

热重载似乎不再起作用。我不确定我的设置可以改变什么,事实上,据我所知,并且 git 日志显示,没有任何改变。

无论如何,我正在尝试重新加载 CSS 和 React .jsx 文件。

目前,我在另一个控制台选项卡中运行 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"
},

关于reactjs - Electron/React 应用程序中的热重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41368745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com