(编辑:jimmy 日期: 2024/10/30 浏览:2)
前言
入门级别教程,适用于初级工程师
移动端 单页面 Demo
正文
第一步
找一个你认为合适的磁盘。输入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>h5 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": "h5-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项目工程的实现的文章就介绍到这了,更多相关webpack构建vue3.0项目工程内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!