[WEBPACK] webpack-dev-server (ok)

C:\xampp\htdocs\abc\webpack.config.js

const path = require('path');
module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist')
  },
  watch: true,
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 9000
  }
}

C:\xampp\htdocs\abc\package.json

{
  "name": "abc",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --watch",
    "start": "webpack-dev-server --port 3000"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack-dev-server": "^3.11.0"
  },
  "dependencies": {
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12"
  }
}

C:\xampp\htdocs\abc\dist\index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Webpack từ A đến Á cùng kentrung</title>
</head>
<body>
  <h1 id="title">Hello Webpack!</h1>
  <script src="main.js"></script>
</body>
</html>

C:\xampp\htdocs\abc\src\index.js

const titleElement = document.querySelector('#title');
titleElement.style.color = 'blue';

Last updated

Navigation

Lionel

@Copyright 2023