Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack-dev-server serving in different folder than webpack output

This is in my webpack.config.js:

var path = require('path');
module.exports = {
  entry: './src/index.jsx',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js'
  },
  devServer: {
    inline: true,
    port: 8080
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel',
        query: {
          presets: ['es2015', 'react']
        }
      }
    ]
  }
}

And this is how I run it:

$ webpack-dev-server --watch-poll --progress --colors
$ webpack --progress --colors

The problem I'm having is that webpack-dev-server is serving the bundle file at the root of my folder (not on the build) folder how I would expect. But webpack outputs the bundle file to build/ (how I expect) to.

So I have to change the script scr when I do a build.

Is there a way to solve that? Maybe is just bad configuration on my webpack.config.js.

I'm running ubuntu BTW.

like image 570
Christian Gill Avatar asked Mar 30 '16 10:03

Christian Gill


People also ask

Where does webpack-dev-server serve files from?

The webpack-dev-server will serve the files in the current directory, unless you configure a specific content base. Using this config webpack-dev-server will serve the static files in your public folder. It'll watch your source files for changes and when changes are made the bundle will be recompiled.

On which port does webpack-dev-server runs by default?

Either method will start a server instance and begin listening for connections from localhost on port 8080 . webpack-dev-server is configured by default to support live-reload of files as you edit your assets while the server is running. See the documentation for more use cases and options.

What is publicPath in output webpack?

publicPath tells webpack-dev-server or express server to serve this bundled file ie examples/dist/app.js from specified virtual location in server ie /public/assets/js. So in your html file, you have to reference this file as <script src="public/assets/js/app.js"></script>

Does webpack minify by default?

Webpack v4+ will minify your code by default in production mode . Note that while the TerserPlugin is a great place to start for minification and being used by default, there are other options out there: ClosureWebpackPlugin.


1 Answers

publicPath tells webpack-dev-server where to serve your bundle in memory

output: {
   path: path.resolve(__dirname, 'build'),
   filename: 'bundle.js',
   publicPath: '/build/'
}
like image 200
Frederick Mfinanga Avatar answered Jan 04 '23 10:01

Frederick Mfinanga