Skip to content
⚠️ This article was written in 2020. Some content may be outdated.

Rollupによるライブラリバンドルのベストプラクティス

日々の開発で Rollup によるライブラリバンドルを扱う機会が増えてきている。本記事では、その使い方・仕組み・最適化の手法を体系的に解説する。

クイックスタート ​

この上にさらに最適化を重ねられる:

javascript
module.exports = {
  entry: './src/index.js',
  output: { path: __dirname + '/dist', filename: '[name].[contenthash:8].js' },
  module: {
    rules: [
      { test: /\.jsx?$/, exclude: /node_modules/, use: 'babel-loader' },
      { test: /\.css$/, use: ['style-loader', 'css-loader', 'postcss-loader'] }
    ]
  },
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendor: { test: /[\\/]node_modules[\\/]/, name: 'vendors' }
      }
    }
  }
}

このパターンは大規模なプロジェクトで非常に実用になり、保守コストを大きく下げられる。

内部原理 ​

実際のプロジェクトでは、もう少し込み入った使い方になる:

javascript
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

export default defineConfig({
  plugins: [vue()],
  resolve: { alias: { '@': resolve(__dirname, 'src') } },
  server: {
    port: 3000,
    proxy: { '/api': { target: 'http://localhost:8080', changeOrigin: true } }
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['vue', 'vue-router', 'vuex'],
          utils: ['lodash-es', 'dayjs']
        }
      }
    }
  }
})

このアプローチなら、コードのテスタビリティと拡張性がともに高まる。

ビジネス実践 ​

以下は全体のサンプルだ:

javascript
module.exports = {
  entry: './src/index.js',
  output: { path: __dirname + '/dist', filename: '[name].[contenthash:8].js' },
  module: {
    rules: [
      { test: /\.jsx?$/, exclude: /node_modules/, use: 'babel-loader' },
      { test: /\.css$/, use: ['style-loader', 'css-loader', 'postcss-loader'] }
    ]
  },
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendor: { test: /[\\/]node_modules[\\/]/, name: 'vendors' }
      }
    }
  }
}

境界条件の処理には注意が必要だ。本番環境では特に重要になる。

パフォーマンス比較 ​

大切なのは根幹となるロジックを理解することだ:

javascript
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

export default defineConfig({
  plugins: [vue()],
  resolve: { alias: { '@': resolve(__dirname, 'src') } },
  server: {
    port: 3000,
    proxy: { '/api': { target: 'http://localhost:8080', changeOrigin: true } }
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['vue', 'vue-router', 'vuex'],
          utils: ['lodash-es', 'dayjs']
        }
      }
    }
  }
})

パフォーマンスの最適化は具体的な場面に応じて行うべきで、どの場合も過度に最適化する必要はない。

トラブルシューティング ​

以下の方法で改善できる:

javascript
module.exports = {
  entry: './src/index.js',
  output: { path: __dirname + '/dist', filename: '[name].[contenthash:8].js' },
  module: {
    rules: [
      { test: /\.jsx?$/, exclude: /node_modules/, use: 'babel-loader' },
      { test: /\.css$/, use: ['style-loader', 'css-loader', 'postcss-loader'] }
    ]
  },
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendor: { test: /[\\/]node_modules[\\/]/, name: 'vendors' }
      }
    }
  }
}

この手法は本番環境で半年以上安定稼働しており、実際に検証済みだ。

まとめ ​

  • 新しい技術を使うために使うべきではない
  • コード例はあくまで参考であり、実際の要件に合わせて調整する
  • Rollup でのライブラリバンドルのベストプラクティスは万能薬ではなく、プロジェクトの規模や技術スタックに応じて選ぶ
  • API を丸暗記するより、基礎となる仕組みを理解するほうが重要だ

MIT Licensed