[英]bundle.js missing from webpack build when using webpack-dev-server
我看了类似但无法找到一个解决我的问题的概念。 我找不到bundle.js文件,即使我指定它应该输出到哪里,一切都在浏览器中工作。 据我所知,webpack-dev服务器正在从内存中加载文件而没有任何内容被写入磁盘,我如何获取文件并将其添加到配置文件中output属性中指定的目录?
这是我的package.json:
{
"name": "redux-simple-starter",
"version": "1.0.0",
"description": "Simple starter package for Redux with React and Babel support",
"main": "index.js",
"repository": "git@github.com:StephenGrider/ReduxSimpleStarter.git",
"scripts": {
"start": "./node_modules/webpack-dev-server/bin/webpack-dev-server.js -- content-base build"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"react-hot-loader": "^1.3.0",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"babel-preset-stage-1": "^6.1.18",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"react-redux": "^4.0.0",
"redux": "^3.0.4"
}
}
webpack配置:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server',
'./src/index.js'
],
output: {
path: path.join(__dirname, 'assets'),
publicPath: '/',
filename: 'bundle.js'
},
module: {
loaders: [{
exclude: /node_modules/,
loader: 'babel'
}]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
devServer: {
contentBase: './'
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
};
使用dev服务器时,输出将放在其上。 所以你不会在你的文件中看到它。 从index.html文件中,您需要从服务器加载它。
例如,对于我的应用程序,我加载开发服务器,我的供应商文件,然后我自己的代码。
<script src="http://localhost:8080/webpack-dev-server.js"></script>
<script src="http://localhost:8080/build/vendor.js"></script>
<script src="http://localhost:8080/build/app.js"></script>
这是我的webpack配置的相关部分。 当我从静态构建包中加载它时,有一些不必要的遗留位。
app: [
'webpack/hot/dev-server',
'webpack-dev-server/client?http://localhost:8080',
'./client/index.js'
]
},
output: {
path: __dirname + '/client/build',
publicPath: '/build/',
filename: '[name].js',
pathinfo: true
},
此Webpack 插件强制服务器将捆绑包写入磁盘。
虽然我同意Austin和lux,但如果你需要将文件放在磁盘中,请直接调用webpack。
您还可以使用配置中的标志告诉webpack。 这将生成捆绑文件
module.exports = {
watch: true,
};
用以下内容替换package.json文件的脚本对象:
"scripts": {
"start": "npm run build",
"build": "webpack -p && ./node_modules/webpack-dev-server/bin/webpack-dev-server.js -- content-base build"
},
然后,运行$ npm start
在webpack.config.js文件中包含以下脚本
devServer: {
writeToDisk: true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.