3

I am using webpack v4 right now trying to add webpack-dev-server to my project. The Issue I am facing here is when i run the webpack-dev-server command my it opens a local server in my default browser but not serving the compiled assets and also is not watching the files for the changes.

I haven't implemented the hot module replacement yet.

Here is the project structure

enter image description here

package.json

{
  "name": "webpack-starterkit",
  "version": "1.0.0",
  "description": "webpack starter kit",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --mode production",
    "dev": "webpack --mode development",
    "start": "webpack-dev-server --mode development --open"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^8.6.3",
    "browser-sync": "^2.24.4",
    "browser-sync-webpack-plugin": "^2.2.2",
    "css-loader": "^0.28.11",
    "mini-css-extract-plugin": "^0.4.0",
    "node-sass": "^4.9.0",
    "postcss-loader": "^2.1.5",
    "sass-loader": "^7.0.3",
    "style-loader": "^0.21.0",
    "webpack": "^4.12.0",
    "webpack-cli": "^3.0.8",
    "webpack-dev-server": "^3.1.4"
  }
}

webpack.config.js

const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require("webpack");
module.exports = {
  entry: "./app/src/js/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "app/dist/js"),
    publicPath: "dist/js"
  },
  devServer: {
    contentBase: path.resolve(__dirname, "app/dist"),
    port: 3000,
    hot: true
  },
  module: {
    rules: [
      {
        test: /\.(sa|sc|c)ss$/,
        use: [
          {
            loader: "style-loader"
          },
          {
            loader: MiniCssExtractPlugin.loader
          },
          {
            loader: "css-loader"
          },
          {
            loader: "postcss-loader"
          },
          {
            loader: "sass-loader"
          }
        ]
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "../css/style.css"
    }),
  ]
};

Please Help me to solve the issue.

Chandra Shekhar
  • 3,550
  • 2
  • 14
  • 22
  • @ippi, i want to implement development environment with webpack-dev-server. I built using production mode it is working assets are being compiled and generated to " dist " folder. Most tutorials about webpack-devserver i have gone through are automatically compiling the assets and watching for changes, but that is not happening in my case. any idea?? – Chandra Shekhar Jun 23 '18 at 05:56
  • I just guess, try change to `publicPath: "/js/"` – Vitaliy Smolyakov Jun 23 '18 at 08:34
  • @VitaliySmolyakov, not working – Chandra Shekhar Jun 23 '18 at 09:22

1 Answers1

0

output.path is used to set the output directory for your app.

You tried to output everything in "app/dist/js", instead you should set it to "app/dist" and then you can choose a specific output path for your assets.

To output the javascript bundle in a folder named js you can set output.filename: "js/bundle.js".

For css you can set filename: "css/style.css" in MiniCssExtractPlugin options.

No need to set publicPath in this example (assets will be served from the root of your localhost server).

Important: If you run the production bundle and then upload your app in a folder different from the server root you need to set publicPath accordingly.

Also you don't need any devServer options (by default serve to http://localhost:8080/).

Here is the solution (I simplified the code a little bit), devServer will work fine even with html files.

package.json

{
  "name": "webpack-test",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode=development --devtool=false",
    "start": "webpack-dev-server --mode=development"
  },
  "license": "MIT",
  "devDependencies": {
    "css-loader": "^0.28.11",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.4.0",
    "webpack": "^4.12.0",
    "webpack-cli": "^3.0.8",
    "webpack-dev-server": "^3.1.4"
  }
}

webpack.config.js

const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: "./app/src/js/index.js",
  output: {
    path: path.resolve(__dirname, "app/dist"),
    filename: "js/bundle.js"
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          { loader: MiniCssExtractPlugin.loader },
          { loader: "css-loader" }
        ]
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "css/style.css"
    }),
    new HtmlWebpackPlugin({
      template: "./app/src/index.html",
      title: "Test"
    })
  ]
};

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <h1>Hello World</h1>
  </body>
</html>

index.js

import "../css/style.css";

console.log("test!");

style.css

body {
  background-color: red;
}

folder structure

folder-structure

pldg
  • 2,427
  • 3
  • 22
  • 37