[英]How can I move “index.html” when using webpack?
Generally,when will "index.html" be loaded using webpack? 通常,何时使用webpack加载“index.html”?
I want to move "index.html" to in build/. 我想将“index.html”移到build /中。
I'm in trouble over data flow from npm start using webpack. 我在使用webpack从npm开始的数据流方面遇到了麻烦。
In package.json, "start" is defined as: 在package.json中,“start”定义为:
"node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js".
However,I couldn't find where it would jump from "webpack-dev-server.js". 但是,我无法从“webpack-dev-server.js”中找到它的位置。
I found two description about "index.html" in it as below: 我在其中找到了关于“index.html”的两个描述,如下所示:
.boolean("history-api-fallback").describe("history-api-fallback", "Fallback to /index.html for Single Page Applications.")
and 和
console.log("404s will fallback to %s", options.historyApiFallback.index || "/index.html");
when I try moving "index.html" to in build/, browser returned "Cannot Get error". 当我尝试将“index.html”移动到build /时,浏览器返回“无法获取错误”。
"index.html" and webpack.config.js are in same folder now. “index.html”和webpack.config.js现在在同一个文件夹中。
./ index.html package.json src/ test/
../ build/ node_modules/ public/ style/
webpack.config.js
One way is to update the script (in package.json) so it copies index.html
to the destination folder: 一种方法是更新脚本(在package.json中),以便将
index.html
复制到目标文件夹:
"scripts": {
"start": "node node_modules/.bin/webpack-dev-server --content-base src",
"build": "node node_modules/.bin/webpack && cp src/index.html build/index.html"
}
npm run build
should now also copy src/index.html
to build/index.html
. npm run build
现在也应该将src/index.html
复制到build/index.html
。
I'm not a webpack
expert, but I faced the same issue, and was able to solve it using a plugin: html-webpack-plugin 我不是
webpack
专家,但我遇到了同样的问题,并且能够使用插件来解决它: html-webpack-plugin
First install it: npm install html-webpack-plugin --save-dev
首先安装它:
npm install html-webpack-plugin --save-dev
Then edit your webpack.config.js
: 然后编辑你的
webpack.config.js
:
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
//Your config here (entry, module, resolve, etc.)
devServer: {
contentBase: './build' //Or any other path you build into
},
plugins: [
//other plugins if any
new HtmlWebpackPlugin({
template: './index.template.html', //your template file
filename: 'index.html',
inject: 'body'
})
]
};
This is going to copy your index.template.html
to ./build/index.html
and serve files from there. 这
index.template.html
您的index.template.html
复制到./build/index.html
并从那里提供文件。
It will also inject all assets at the bottom of the <body>
tag. 它还会在
<body>
标记的底部注入所有资源。
You can further costumize this setup to fit your need of course, just refer to the plugin docs . 您可以进一步降低此设置的成本以满足您的需要,只需参考插件文档即可 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.