繁体   English   中英

Webpack 服务未将 JS 插入索引。html

[英]Webpack Serve not inserting JS into index.html

再会,

I set up Webpack to create an optimized build of my React application to /dist/ with webpack --mode production --config webpack.config.production.js , which automatically inserts the correct initial script into the head of index.html ( webpack .config.production.js索引.html )。 这可以使用我的 Rust 后端为生产构建的文件提供服务。 However, my script that webpack serve --mode development --config webpack.config.js to enable hot-reloading does not insert the named script into the HTML file, and thus fails to load ( webpack.config.js ).

在过去的一个半小时里,我尝试了所有我能找到的方法来解决这个问题,但无济于事,我将非常感谢任何帮助。 此外,如果有办法利用我的 Rust 后端进行热重载,那将是一个更好的解决方案:)。

@Robert 在正确的轨道上,问题是您的publicPath中有两个不同的publicPath

例如,您的输出的 publicPath/

...
output: {
  filename: "static/[name].[fullhash].js",
  path: path.resolve(__dirname, "dist"),
  publicPath: "/",
},

而您的devServer 的 outputhttp://localhost:3000/dist/

...
devServer: {
  contentBase: path.join(__dirname, "public/"),
  port: 3000,
  publicPath: "http://localhost:3000/dist/",
  open: true,
},
...

相反,它们应该是相同的路径——在这种情况下,它们应该是/ (或者,您可以从 devServer 中删除publicPath属性,它也可以工作):

devServer: {
  contentBase: path.join(__dirname, "public/"),
  port: 3000,
  // publicPath: "/",
  open: true,
},

暂无
暂无

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

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