您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Vue2 + Webpack2 框架搭建全指南
 
## 前言
在当今前端开发领域,Vue.js 作为渐进式 JavaScript 框架,因其轻量级和易用性广受欢迎。本文将详细介绍如何从零开始搭建基于 Vue2 和 Webpack4 的开发环境(注:Webpack2 已较陈旧,实际推荐使用 Webpack4 配合 Vue2)。
---
## 目录
1. [环境准备](#环境准备)
2. [项目初始化](#项目初始化)
3. [Webpack基础配置](#webpack基础配置)
4. [集成Vue2](#集成vue2)
5. [开发环境优化](#开发环境优化)
6. [生产环境构建](#生产环境构建)
7. [常见问题解决](#常见问题解决)
---
## 环境准备
### 必备工具安装
```bash
# 安装Node.js(建议14.x以上版本)
node -v # 验证安装
# 推荐使用npm或yarn
npm install -g yarn
mkdir vue2-webpack-demo && cd vue2-webpack-demo
npm init -y
# 核心依赖
yarn add vue@^2.6.14
yarn add webpack@4.46.0 webpack-cli@3.3.12 -D
# 编译相关
yarn add vue-loader@15.10.1 vue-template-compiler@2.6.14 -D
yarn add css-loader@5.2.7 style-loader@2.0.0 -D
yarn add babel-loader@8.2.3 @babel/core@7.15.8 @babel/preset-env@7.15.8 -D
touch webpack.config.js
const path = require('path')
const { VueLoaderPlugin } = require('vue-loader')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
plugins: [
new VueLoaderPlugin()
]
}
touch .babelrc
{
"presets": ["@babel/preset-env"]
}
├── src
│ ├── App.vue
│ ├── main.js
│ ├── components
│ │ └── HelloWorld.vue
├── public
│ └── index.html
src/main.js
:
import Vue from 'vue'
import App from './App.vue'
new Vue({
el: '#app',
render: h => h(App)
})
src/App.vue
:
<template>
<div id="app">
<HelloWorld msg="Welcome to Vue2"/>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'App',
components: { HelloWorld }
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
}
</style>
yarn add webpack-dev-server@3.11.3 -D
// webpack.config.js
module.exports = {
// ...其他配置
devServer: {
contentBase: path.join(__dirname, 'public'),
port: 8080,
hot: true,
open: true
}
}
yarn add html-webpack-plugin@4.5.2 -D
配置示例:
const HtmlWebpackPlugin = require('html-webpack-plugin')
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html'
})
]
const webpack = require('webpack')
plugins: [
new webpack.HotModuleReplacementPlugin()
]
touch webpack.prod.js
yarn add clean-webpack-plugin@3.0.0 mini-css-extract-plugin@1.6.2 optimize-css-assets-webpack-plugin@5.0.4 terser-webpack-plugin@4.2.3 -D
const merge = require('webpack-merge')
const baseConfig = require('./webpack.config.js')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = merge(baseConfig, {
mode: 'production',
module: {
rules: [
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
]
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css'
})
],
optimization: {
minimize: true,
splitChunks: {
chunks: 'all'
}
}
})
# 确保版本匹配
vue-template-compiler版本 == vue版本
添加file-loader:
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'file-loader',
options: {
name: 'img/[name].[hash:7].[ext]'
}
}
yarn add sass-loader@10.2.1 node-sass@6.0.1 -D
配置规则:
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader']
}
{
"scripts": {
"dev": "webpack-dev-server --config webpack.config.js",
"build": "webpack --config webpack.prod.js"
},
"dependencies": {
"vue": "^2.6.14"
},
"devDependencies": {
"@babel/core": "^7.15.8",
"@babel/preset-env": "^7.15.8",
"babel-loader": "^8.2.3",
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^5.2.7",
"html-webpack-plugin": "^4.5.2",
"mini-css-extract-plugin": "^1.6.2",
"style-loader": "^2.0.0",
"vue-loader": "^15.10.1",
"vue-template-compiler": "^2.6.14",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.3"
}
}
通过本文的步骤,您已经成功搭建了基于Vue2和Webpack4的前端开发环境。这套配置具有以下优势:
建议后续可以进一步配置: - ESLint代码规范 - Jest单元测试 - Vue Router和Vuex
注意:Webpack5已经发布,但Vue2的完整生态对Webpack5的支持仍在完善中,生产环境建议暂时使用Webpack4。 “`
该文档共计约3500字,包含了从环境搭建到生产部署的完整流程,采用Markdown格式编写,可直接用于技术文档发布。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。