简体   繁体   English

使用 webpack 构建 index.html 以与开发服务器一起使用

[英]Build index.html with webpack to use with dev server

Im using webpack for running my Three.js application with the following configuration in the webpack.config file:我使用 webpack 在webpack.config 文件中使用以下配置运行我的 Three.js 应用程序

module.exports = {
  entry: `${__dirname}/src/tut15.js`,
  output: {
    path: __dirname + '/dist',
    filename: 'index_bundle.js'
  },
  devtool: 'inline-source-map',
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 9000
  },

  plugins: [
    new HtmlWebpackPlugin()
  ]
}

package.json包.json

  "scripts": {
    "start": "webpack-dev-server --open",
    "build": "webpack --config webpack.config.babel.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

With the HtmlWebpackPlugin it autogenerates the html for me.使用HtmlWebpackPlugin它会为我自动生成 html。 But since I want to create a custom html index file with some new tags this doesnt work for me.但是由于我想创建一个带有一些新标签的自定义 html 索引文件,这对我不起作用。 So I build the project所以我建立了这个项目

npm run-script build

and runs the index.html in the dist folder manually with my applied edits and everything works.并使用我应用的编辑手动运行 dist 文件夹中的 index.html,一切正常。

If I remove the HtmlWebpackPlugin from the webpack.config and build the project again and then run:如果我从 webpack.config 中删除 HtmlWebpackPlugin 并再次构建项目,然后运行:

npm start

livereload works for my js files together with my new index.html with custom tags in the distfolder. livereload 适用于我的 js 文件以及我的新 index.html 和 distfolder 中的自定义标签。

Questions:问题:

  1. It doesnt feels correct to create the changes in the dist folder.在 dist 文件夹中创建更改感觉不正确。 How can I generate a index.html straight from source?如何直接从源代码生成 index.html? I guess that might solve all my problems being able to run dev server with a custom index.html我想这可能会解决我能够使用自定义 index.html 运行开发服务器的所有问题
  2. Or is it possible to get some livereload for the build as well?或者是否也可以为构建获得一些 livereload?
  3. after I have built my project it generates index.html and index.bundle in my distfolder.在我构建了我的项目之后,它会在我的 distfolder 中生成 index.html 和 index.bundle。 If I remove the index.bundle the project works anyway.如果我删除 index.bundle 项目仍然可以工作。 What exactly does index.bundle do? index.bundle 到底做了什么?

What is the best approach or do I need to build my project each time Im doing an update in my index file?什么是最好的方法,或者每次我在我的索引文件中进行更新时我都需要构建我的项目吗?

Thanks!谢谢!

For questions regarding custom index.html and hot-reloading at least.至少对于关于自定义 index.html 和热重载的问题。

The plugin has some config you can add for templating.该插件有一些配置,您可以添加用于模板化。

new HtmlWebPackPlugin({
  filename: 'index.html',
  template: './public/index.html',
}),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM