简体   繁体   English

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

[英]bundle.js missing from webpack build when using webpack-dev-server

I looked at similar but couldnt find a concerete answer that resolved my issue. 我看了类似但无法找到一个解决我的问题的概念。 I can't find the bundle.js file even though I am specifying where it should be outputted and everything works in the browser. 我找不到bundle.js文件,即使我指定它应该输出到哪里,一切都在浏览器中工作。 I understand that the webpack-dev server is loading the files from memory and nothing is being written to disk, how I can get the file to be built and added to the dir specified in the output property in the config file? 据我所知,webpack-dev服务器正在从内存中加载文件而没有任何内容被写入磁盘,我如何获取文件并将其添加到配置文件中output属性中指定的目录?

Here is my package.json: 这是我的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: 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()
     ]
    };       

When using the dev server, the output is placed on it. 使用dev服务器时,输出将放在其上。 So you won't actually see it amongst your files. 所以你不会在你的文件中看到它。 From your index.html file you will want to load it in from the server. 从index.html文件中,您需要从服务器加载它。

For example, for my app I load in dev server, my vendor files, and then my own code. 例如,对于我的应用程序,我加载开发服务器,我的供应商文件,然后我自己的代码。

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

And here is the relevant portion of my webpack config. 这是我的webpack配置的相关部分。 There is some unnecessary legacy bits from when I was also loading it in from a static build bundle. 当我从静态构建包中加载它时,有一些不必要的遗留位。

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

This Webpack plugin forces the server to write the bundle to disk. 此Webpack 插件强制服务器将捆绑包写入磁盘。

Although I agree with Austin and lux, if you need to have the file in disk, call webpack directly. 虽然我同意Austin和lux,但如果你需要将文件放在磁盘中,请直接调用webpack。

You can also tell webpack to watch using a flag in the config. 您还可以使用配置中的标志告诉webpack。 This will generate the bundle file 这将生成捆绑文件

module.exports = {
    watch: true,
};

Replace your scripts object of package.json file with the following one: 用以下内容替换package.json文件的脚本对象:

"scripts": {
     "start": "npm run build",
     "build": "webpack -p && ./node_modules/webpack-dev-server/bin/webpack-dev-server.js -- content-base build"
},

Then, run $ npm start 然后,运行$ npm start

Include below script in the webpack.config.js file 在webpack.config.js文件中包含以下脚本

devServer: {
  writeToDisk: true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM