# 快速上手

# 简单的例子

假设我们有这样两个文件,一个 index.html 一个 src/index.js

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="./dist/main.js"></script>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
document.getElementById("app").innerText = "Hello, Webpack";

# 安装依赖

# 本地安装
npm install webpack webpack-cli -D
// webpack.config.js
const path = require("path");

module.exports = {
  mode: "development",
  entry: path.join(__dirname, "./src/index.js"), // dirname代表索引到文件所在目录
  output: {
    path: path.join(__dirname, "./dist"),
    filename: "main.js"
  }
};

在上面的示例中,我们通过 output.filenameoutput.path 属性,来告诉 webpack 打包出 bundle 的名称,以及我们想要把 bundle 生成到哪里

{
  "scripts": {
    "build": "webpack --config webpack.config.js"
  }
}
npm run build

# 热更新

npm install webpack-dev-server -D
npm install html-webpack-plugin -D
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  mode: "development", // development-开发环境。 production-生产环境
  entry: path.join(__dirname, "./index.js"),
  output: {
    path: path.join(__dirname, "./dist"),
    filename: "main.js"
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, "./index.html")
    })
  ]
};
{
  "scripts": {
    "dev": "webpack-dev-server --open --port 3002 --hot",
    "build": "webpack --config webpack.config.js"
  }
}
npm run dev

# 处理 CSS

# 处理 ES6

# 处理图片资源

# HTML 文件的热更新