【发布时间】:2018-09-09 04:46:49
【问题描述】:
目前我正在使用带有 Webpack 的 React。我想在不运行“npm start”的情况下执行 React。所以经过大量研究后,我发现我可以在生产环境中运行,而无需使用“npm start”,只需使用 Webpack 创建一个“bundle.js”。
到目前为止,我已经创建了我的“bundle.js”,它在我的节点服务器上运行良好。
我现在想在 Apache 服务器中进行测试。
“bundle.js”文件与“index.html”一起在“dist”中创建。当我尝试在 Apache 服务器中运行时,它不包含 React 代码。
所以我的问题是:如何在 Apache 中运行我的 React 应用程序?
这是我的 webpack-config.js
const path = require("path");
const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
entry: ["./src/js/root.js"],
output: {
path: path.resolve(__dirname, "/dist"),
filename: "js/main.js"
},
devServer: {
contentBase: "./dist"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.html$/,
use: [
{
loader: "html-loader"
}
]
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
})
]
};
我的 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-
beta.2/css/bootstrap.min.css" >
<title>How to set up React, Webpack, and Babel</title>
</head>
<body>
<div class="container">
<div class="row mt-5">
<div class="col-md-4 offset-md-1">
<p>Create a new article</p>
<div id="root-container">
<!-- form -->
</div>
</div>
</div>
</div>
<script src="../dist/js/main.js"></script>
</body>
</html>
在我的 dist 文件夹中,它创建了 2 个文件, index.html 和 main.js。
【问题讨论】:
-
您将 index.html 文件放在哪里?它有 bundle.js 引用吗?
-
您是否使用
create-react-app创建反应应用程序?还是使用自定义设置您自己的 webpack? -
如果我们看到代码,它将帮助我们更轻松地帮助您。但只是在黑暗中拍摄,请检查
index.html中的<script src='bundle.js'></script>路径 -
我使用了自定义设置。我没有使用 create-react-app。
-
我现在将更新我的代码
标签: apache reactjs webpack bundle