webpack dev server 不是会在内存中生成 bundle.js 吗?

文件结构如下:

Project
 |
 +-- src/
 |   |
 |   +-- index.js
 |
 +-- index.html
 |
 +-- webpack.config.js

src/index.js:

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>webpack learning</title>

    <script src="builds/bundle.js"></script>
</head>
<body>

</body>
</html>

webpack.config.js:

var path = require('path');

module.exports = {
  entry: './src',
  output: {
    path: path.join(__dirname, 'builds'),
    filename: 'bundle.js',
  }
};

当我在项目目录运行 webpack-dev-server 时,webpack-dev-server 正常启动并输出:’webpack: bundle is now VALID'。然而,当我打开浏览器,并访问localhost:8080 时,控制台报错:
图片描述

确实,没有生成builds/bundle.js。可是,我记得webpack-dev-server 是在内存里生成bundle.js 的,不会写到硬盘上,不知道为什么会报这个错。是我的webpack的配置问题吗?

阅读 7k
2 个回答

When I add output.publicPath and set it to "builds", everything works just fine. It seems that when webpack-dev-server is generating bundle.js in memory, it puts that file in the directory specified by output.publicPath and ignores what directory the "output.path" is set to. Because whatever output.path is set to, as long as output.publicPath remains "builds", no error appears in the console and the browser renders "Hello World."

新手上路,请多包涵

devServer没写publicPath

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题