6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

webpack エントリーポイント Entry Points

Last updated at Posted at 2019-12-31

エントリーポイントとは

モジュールバンドラーでバンドルする際の、解析のスタート地点のファイル。
エントリーポイントを起点として、そのファイルでimportしているモジュールを順番に辿っていく。

単一のエントリーポイント => 単一の出力先

entryプロパティを指定しない場合、デフォルトでは./src/index.jsファイルを見る。
=> エントリーポイントが./src/index.jsであれば、entryプロパティを省略できる。

ファイル名やパスが違う場合はentryプロパティで、webpack.config.jsファイルからの相対パスを以下のように文字列で指定することができる。

webpack.config.js
module.exports = {
  entry: './src/hoge.js'
};

上記は以下の短縮系。エントリーポイントが1つであれば上記で十分。

webpack.config.js
module.exports = {
  entry: {
    main: './src/hoge.js'
  }
};

出力先のファイル名を変えたい場合は、mainをそのファイル名にする。

複数のエントリーポイント => 単一の出力先

エントリーポイントが複数ある場合は、以下のように配列で渡すとバンドルファイルを1つにまとめることができる

webpack.config.js
module.exports = {
  entry: ['./src/hoge.js', './src/fuga.js']
};

実行するとdist/main.jsにまとめられている。

複数のエントリーポイント => 複数の出力先

エントリーポイントが複数あり、バンドルファイルもそれぞれ分けたい場合は、以下のように記述する。

webpack.config.js
module.exports = {
  entry: {
    app: './src/app.js',
    adminApp: './src/adminApp.js'
  }
};

バンドルすると以下のようになる。

スクリーンショット 2020-01-01 0.34.14.png

こちらも同様に配列で渡すことが可能。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?