您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關使用webpack如何構建一個vue3.0項目工程,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
第一步
找一個你認為合適的磁盤。輸入mkdir test
, cd test
, npm init -y
。
第二步
安裝依賴
webpack 系列依賴
webpack webpack-cli
babel 系列依賴
@babel/core @babel/preset-env
移動兼容方案
amfe-flexible autoprefixer
axios
axios
webpack loader
babel-loader
css-loader
file-loader
postcss-loader
pm2rem-loader
style-loader
這個單獨說一下,vue3.0 sfc重寫了對.vue文件的編譯方式,使用parseComponent等方法,必須使用-v16后綴的這個loader,當時找這個loader用了較長的時間
vue-loader-v16
vue-template-complier
webpack plugin
html-webpack-plugin
webpack-dev-server
Vue & Vuex
vue@next
vuex@next
請使用yarn add 或者 npm i 安裝上述依賴包
第三步
在當前根目錄創建index.html,初始化HTML代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>h6 static</title> </head> <body> <div id="app"></div> </body> </html>
創建.babelrc,初始化babel配置
{ "presets": ["@babel/preset-env"] }
創建postcss.config.js初始化postcss配置
module.exports = { plugins: [require('autoprefixer')], }
創建.gitignore
node_modules .vscode .idea
創建scripts目錄,編寫yarn鎖定腳本
if (!/yarn\.js$/.test(process.env.npm_execpath || '')) { console.warn( '\u001b[33mThis repository requires Yarn 1.x for scripts to work properly.\u001b[39m\n' ) process.exit(1) }
將腳本配置在package.json配置中
{ "name": "h6-static", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "webpack-dev-server", "build": "webpack --mode=production", "preinstall": "node ./scripts/checkYarn.js" }, "author": "upeartaker", "license": "ISC", "dependencies": { "@babel/core": "^7.11.6", "@babel/preset-env": "^7.11.5", "@vue/compiler-sfc": "^3.0.0", "amfe-flexible": "^2.2.1", "autoprefixer": "8.0.0", "axios": "^0.20.0", "babel-loader": "^8.1.0", "css-loader": "^4.3.0", "file-loader": "^6.1.0", "html-webpack-plugin": "^4.5.0", "postcss-loader": "^4.0.2", "px2rem-loader": "^0.1.9", "style-loader": "^1.2.1", "vue": "^3.0.0", "vue-loader": "^15.9.3", "vue-loader-v16": "^16.0.0-beta.5.4", "vue-template-compiler": "^2.6.12", "vuex": "^4.0.0-beta.4", "webpack": "^4.44.2", "webpack-cli": "^3.3.12", "webpack-dev-server": "^3.11.0" }, "browserslist": [ "defaults", "not ie < 11", "last 2 versions", "> 1%", "iOS 7", "last 3 iOS versions" ] }
其中preinstall 為yarn鎖定腳本
const path = require('path') const VueLoaderPlugin = require('vue-loader-v16/dist/plugin.js').default const HtmlWebpackPlugin = require('html-webpack-plugin') module.exports = { entry: path.resolve(__dirname, './src/main.js'), output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js', }, resolve: { extensions: ['.js'], }, module: { rules: [ { test: /\.js$/, use: ['babel-loader'], exclude: /node_modules/, }, { test: /\.css$/, use: [ 'style-loader', 'css-loader', { loader: 'px2rem-loader', options: { remUnit: 37.5, }, }, { loader: 'postcss-loader', options: { sourceMap: true, postcssOptions: { path: 'postcss.config.js', }, }, }, ], }, { test: /\.(png|svg|jpg|gif)$/, use: ['file-loader'], }, { test: /\.(woff|woff2|eot|ttf|otf)$/, use: ['file-loader'], }, { test: /\.vue$/, loader : 'vue-loader-v16' } ], }, optimization: { minimize: true, }, plugins: [ new HtmlWebpackPlugin({ template: path.resolve(__dirname, './index.html'), inject: 'body', }), new VueLoaderPlugin() ], }
第四步
編寫Demo,測試工程是否可以使用。
進入src目錄下,創建main.js文件,編寫以下代碼,
import 'amfe-flexible' import './index.css' import { createApp } from 'vue' import App from './App.vue' const app = createApp(App) app.mount('#app')
創建App.vue文件,編寫一下代碼
<template> <Fragment> {{ message }} <button @click="handleClick">點我</button> </Fragment> </template> <script> import { Fragment, ref } from 'vue' export default { name: 'root', components: [Fragment], setup() { const message = ref('hello') const handleClick = () => { message.value = 'hit' } return { message, handleClick, } }, } </script>
第五步
執行腳本yarn start
以上就是使用webpack如何構建一個vue3.0項目工程,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。