我正在尝试使用webpack捆绑一个angular应用程序,
这是webpack.config.js:
var path = require('path');
module.exports = {
context: path.resolve(__dirname, 'app'),
entry:'./index.js',
output:{
path : __dirname + '/app',
filename:'bundle.js'
}
};app/index.html如下所示:
<!DOCTYPE html>
<html>
<head>
<title>test ng-webpack egghead</title>
</head>
<body ng-app="app">
<script type="text/javascript" src="bundle.js"></script>
</body>
</html>当我运行webpack-dev-server命令时,如控制台日志所示:
>webpack-dev-server --content-base app
http://localhost:8080/webpack-dev-server/
webpack result is served from /
content is served from F:\Learnings\FrontEnd\AngularJs\webpack\egghead.io - AngularJS - Angular and Webpack for Modular Applications\egg-ng-webpack\app
Hash: 98db763b035ecfaba293
Version: webpack 1.13.2
Time: 1089ms
Asset Size Chunks Chunk Names
bundle.js 1.22 MB 0 [emitted] main
chunk {0} bundle.js (main) 1.19 MB [rendered]
[0] ./app/index.js 120 bytes {0} [built]
[1] ./~/angular/index.js 48 bytes {0} [built]
[2] ./~/angular/angular.js 1.19 MB {0} [built]
webpack: bundle is now VALID.或者,当我运行与npm脚本相同的命令时,日志保持不变(看起来一切正常),但bundle.js文件不是在两个output.path中设置的/app文件夹下生成的,而是在应用程序根目录下生成的(文件实际上并不写入磁盘中),并且只能通过url http://127.0.0.1:8080/bundle.js而不是应该的http://127.0.0.1:8080/app/bundle.js获得(然后当尝试在浏览器中加载应用程序/索引.html时,我得到错误(GET http://127.0.0.1:8080/app/bundle.js 404 (Not Found)),并且在重复相同的过程多次后,现在我在任何地方都没有得到包文件,但是日志仍然和上面一样。
当我在package.json中将命令webpack-dev-server仅替换为webpack时:
"scripts": {
"start": "webpack --content-base app"
},现在,bundle.js存在于/app下,并通过http://127.0.0.1:8080/app/bundle.js显示,并且index.html正确加载,没有错误。
发布于 2016-09-28 15:44:30
在配置文件中设置publicPath属性确实解决了这个问题:
var path = require('path');
module.exports = {
context: path.resolve(__dirname, 'app'),
entry:'./index.js',
output:{
path : path.resolve(__dirname, 'app'),
publicPath: "app/",
filename:'./bundle.js'
}
};https://stackoverflow.com/questions/39740039
复制相似问题