webpack5+vue3搭建項目


一、創建package.json文件

npm init -y

package.json文件:

{
  "name": "03-app1",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@vue/compiler-sfc": "^3.2.26",
    "html-webpack-plugin": "^5.5.0",
    "vue-loader": "^16.0.0-beta.4",
    "webpack": "^5.65.0",
    "webpack-cli": "^4.9.1",
    "webpack-dev-server": "^4.6.0"
  },
  "dependencies": {
    "vue": "^3.2.26"
  }
}

二、安裝vue

npm i vue@next
npm i @vue/compiler-sfc -D //單文件組件開發的配套工具

請注意 @vue/compiler-sfc 替換掉了 vue-template-compiler

三、安裝打包工具

npm i webpack webpack-cli webpack-dev-server html-webpack-plugin -D

創建入口文件index.js

import {createApp} from 'vue'
import App from './App.vue'

createApp(App).mount('#app')

創建App.vue文件

<template>
    <div id="app">
        {{appName}}
    </div>
</template>

<script>
    export default {
        name: '',
        data() {
            return {
               appName:'我是app1' 
            }
        },
        props: {
           
        },
        components: {
            
        },
        computed: {
            
        },
        watch: {
           
        },
        created() {
            
        },
        methods: {
            
        },
    }
</script>

<style scoped lang='less'>
    
</style>

如果使用了自定義的 webpack 設置:將 vue-loader 升級至 ^16.0.0

npm i vue-loader@16.0.0-beta.4 -D

創建webpack.config.js文件

const HtmlWebpackPlugin = require('html-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader')
module.exports = {
  mode: 'development',

  entry: './src/index.js',

  plugins: [
    new HtmlWebpackPlugin({
      title: 'webpack+vue3搭建項目',
      template: './index.html'
    }),
    new VueLoaderPlugin()
  ],

  module: {
    rules: [
      {
        test: /\.vue$/,
        use: 'vue-loader'
      },
      {
        test: /\.(css||less)$/,
        use: ['style-loader', 'less-loader', 'css-loader']
      }
    ]
  }
}

五、錯誤記錄

Module build failed (from ./node_modules/vue-loader/dist/index.js): Error: Cannot find module 'webpack/lib/rules/DescriptionDataMatcherRulePlugin'

vue-laoder版本問題

更新到vue-loader@16.0.0-beta.4

 

ERROR in ./App.vue
Module Error (from ./node_modules/vue-loader/dist/index.js):
vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin in your webpack config.

在webpack.config.js:

const { VueLoaderPlugin } = require('vue-loader')

module.exports={
	plugins: [
    	new HtmlWebpackPlugin({
      		template: './index.html'
    	}),
    	new VueLoaderPlugin()
  	],
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM