在日常开发中,Vue 3 构建 Web Componentsの使用頻度が高まっています。本記事では、その使い方、原理、最適化戦略を体系的に説明します。
クイックスタート
この基盤の上でさらに最適化できます:
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
import { reactive, toRefs, computed } from 'vue'
function useCounter(initial = 0) {
const state = reactive({ count: initial, history: [initial] })
const doubled = computed(() => state.count * 2)
function increment() {
state.count++
state.history.push(state.count)
}
return { ...toRefs(state), doubled, increment }
}
境界条件の処理に注意してください。これは本番環境で非常に重要です。
パフォーマンス比較
コアロジックを理解することが重要です:
javascript
import { ref, computed, watch, onMounted } from 'vue'
export default {
setup() {
const count = ref(0)
const doubled = computed(() => count.value * 2)
watch(count, (newVal, oldVal) => {
console.log(`count: ${oldVal} -> ${newVal}`)
})
onMounted(() => { console.log('组件已挂载') })
return { count, doubled }
}
}
パフォーマンスの最適化は具体的なシナリオに合わせる必要があり、すべてのケースで過度な最適化が必要というわけではありません。
トラブルシューティング
以下の方法で改善できます:
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' }
}
}
}
}
このアプローチは半年以上にわたって本番環境で安定稼働しており、実際に検証済みです。
まとめ
- チームコラボレーションでは、規約とドキュメントが技術そのものより重要です
- コミュニティの動向を注視し、技術的なソリューションは継続的な反復が必要です
- 新しい技術を使うためだけに新しい技術を使わないでください
- コードサンプルは参考用のみであり、ビジネスシナリオに応じて調整が必要です