Skip to content

Webpack 速查表

JavaScript 应用的模块打包器。

01

配置

基础配置

entry 和 output 定义打包方式。

webpack
module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve("dist"),
    filename: "bundle.js",
  },
};

Loaders

loader 转换非 JS 模块。

webpack
module: {
  rules: [
    { test: /\.css$/, use: ["style-loader", "css-loader"] },
    { test: /\.ts$/, use: "ts-loader" },
  ],
}

Plugins

plugin 扩展 webpack 功能。

webpack
const HtmlWebpackPlugin = require("html-webpack-plugin");

plugins: [
  new HtmlWebpackPlugin({ template: "./src/index.html" }),
]
02

优化

代码分割

把代码拆分成更小的 chunk。

webpack
optimization: {
  splitChunks: {
    chunks: "all",
  },
}

开发服务器

支持 HMR 的开发服务器。

webpack
devServer: {
  contentBase: "./dist",
  hot: true,
  port: 3000,
}

这篇内容对您有帮助吗?