简体   繁体   English

使用webpack时如何移动“index.html”?

[英]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.

相关问题 使用webpack-dev-server时,html-webpack-plugin不会将js文件注入index.html - html-webpack-plugin not inject js file into index.html when using webpack-dev-server 当 webpack 服务器运行时,如何运行不同的 html 文件而不是 index.html? - how to run a different html file instead of index.html when webpack server runs? 如何使用HTML Webpack插件将文件加载器所需的CSS文件包含到index.html中 - how to include css file required with file loader into index.html using html webpack plugin 当我运行 npm 启动时,我的 webpack 找不到我的索引。html 页面 - my webpack cannot find my index.html page when I run npm start Webpack - 如何包含。html 片段进入索引。html 实时重新加载? - Webpack - How to include .html fragments into index.html with live reloading? Webpack Build 生成 index.html 但在浏览器中加载时为空白 - Webpack Build generates index.html but blank when loaded in browser 如何将文件 vuejs 组件导入 index.html? - how can I import a file vuejs component into index.html? 如何判断用户是否在index.html上? - How can I tell if a user is on index.html? 如何在 Svelte 中重命名 index.html 文件? - How can I rename index.html file in Svelte? 如何将index.html中的数据迁移到控制器? - How can I migrate data in index.html to a controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM