使用webpack-dev-server时,webpack build中缺少bundle.js

时间:2016-03-08 14:32:32

标签: javascript reactjs webpack 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 config:

    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()
     ]
    };       

5 个答案:

答案 0 :(得分:5)

使用dev服务器时,会在其上输出输出。所以你实际上没有在你的文件中看到它。从index.html文件中,您需要从服务器加载它。

例如,对于我的应用程序,我加载了dev服务器,我的供应商文件,然后是我自己的代码。

<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
},

答案 1 :(得分:4)

此Webpack plugin强制服务器将捆绑包写入磁盘。

虽然我同意Austin和lux,但如果您需要将文件保存在磁盘中,请直接致电webpack。

答案 2 :(得分:0)

您还可以告诉webpack使用配置中的标志进行观察。这将生成捆绑文件

module.exports = {
    watch: true,
};

答案 3 :(得分:0)

将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

答案 4 :(得分:0)

在webpack.config.js文件中包含以下脚本

devServer: {
  writeToDisk: true
}