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

Vite 3.0 リリース:新機能の概要

关于Vite 3.0 发布新特性,很多开发者只停留在 API 调用层面。本文试图从生产环境的角度,讨论实际中会遇到的问题和解决方案。

基本原理

以下の方法で改善できます:

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', 'pinia'],
          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', 'pinia'],
          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' }
      }
    }
  }
}

境界条件の処理に注意してください。これは本番環境において非常に重要です。

まとめ

  • コミュニティの動向を注視し、技術ソリューションは継続的に反復する必要があります
  • 新技術のために新技術を採用しないでください
  • コード例は参考のみであり、ビジネスシナリオに応じて調整が必要です
  • Vite 3.0 发布新特性は銀の弾丸ではなく、プロジェクトの規模と技術スタックに応じて選択する必要があります
  • APIを暗記するよりも、基盤となる原理を理解する方が重要です

MIT Licensed