官网链接:https://webpack.js.org/concepts/entry-points/

这一章节讲述了配置 entry 属性的方法

一、单个入口(简写)

Usage: entry: string | [string]

使用单个字符串

  1. module.exports = {
  2. entry: './path/to/my/entry/file.js',
  3. };

它其实是下面这种语法的缩写

  1. module.exports = {
  2. entry: {
  3. main: './path/to/my/entry/file.js',
  4. },
  5. };

使用字符串数组

  1. module.exports = {
  2. entry: ['./src/file_1.js', './src/file_2.js'],
  3. output: {
  4. filename: 'bundle.js',
  5. },
  6. };

二、多个入口

Usage: entry:{ <entryChunkName> string | [string] } | {}

  1. module.exports = {
  2. entry: {
  3. app: './src/app.js',
  4. adminApp: './src/adminApp.js',
  5. },
  6. };

使用对象语法会更加冗余,但是它变得更加灵活
接下来看一看 EntryDescription object

  • dependOn:entry point 的依赖,这些依赖必须在 entry point 加载之前先加载
  • filename:指定每个输出文件的名字
  • import:启动时需要加载的模块
  • library:指定 library 选项,为当前 entry 构建一个 library
  • runtime:runtime chunk 的名字
  • publicPath:当 entry 输出的文件被浏览器引用时,为它们指定一个公共的 URL 地址,详情可见 output.publicPath
    1. module.exports = {
    2. entry: {
    3. a2: 'dependingfile.js',
    4. b2: {
    5. dependOn: 'a2',
    6. import: './src/app.js',
    7. },
    8. },
    9. };

三、常用场景

1. 分隔 App 和 Vendor Entries

webpack.config.js

  1. module.exports = {
  2. entry: {
  3. main: './src/app.js',
  4. vendor: './src/vendor.js',
  5. },
  6. };

webpack.prod.js

  1. module.exports = {
  2. output: {
  3. filename: '[name].[contenthash].bundle.js',
  4. },
  5. };

webpack.dev.js

  1. module.exports = {
  2. output: {
  3. filename: '[name].bundle.js',
  4. },
  5. };

上面的步骤做了什么?

  • 我们告诉 Webpack 使用两个分开的入口

为什么这样做?

  • 我们把不必在改动的库和文件(比如:Bootstrap,jQuery,images)放到 vendor.js 里,他们将会被捆绑在他们各自的块中
  • 这些文件的 Content hash 依然相同,这样浏览器可以各自把他们缓存起来,减少加载时间

「@浪里淘沙的小法师」