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

プラグインで webpack の機能を拡張します。

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

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

最適化

Code Splitting

コードをより小さなチャンクに分割します。

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

Dev Server

HMR 対応の開発用サーバーです。

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

Was this helpful?