文件结构如下:
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的配置问题吗?
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."