本篇文章给大家分享的是有关使用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。