首先祝你們元宵節快樂,最近已經很久沒有寫過文章了,恰好趁着這幾天剛剛上班,領導尚未來,偷偷的寫一篇關於webpack搭建vue的博客。由於公司使用vue比較多,構建vue項目使用vue-cli顯得有點臃腫,感受仍是本身配置比較好些,因此就有了這篇教程。因爲水平有限,歡迎你們指正,一塊兒進步。javascript
npm init -y複製代碼
在webpackconfig文件夾中生成package.josncss
npm i webpack webpack-dev-server webpack-cli -D複製代碼
alert(1)複製代碼
webpack.config.js文件html
var path = require('path');
var config = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname + '/dist'),//打包生成文件地址
filename: '[name].build.js',//生成文件ming
publicPath: '/dist/'//文件輸出的公共路徑
}
}
module.exports = config;
複製代碼
entry: 引入文件,對象寫法能夠引入多文件vue
entry: {
app: './src/app.js',
vendors: './src/vendors.js'
}複製代碼
output:文件輸出地址java
path: 輸出文件地址node
filename:輸出文件名webpack
publicPath:文件輸出路徑git
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script src="/dist/main.build.js"></script> </body> </html>複製代碼
"dev": "webpack-dev-server --open --hot",
"build": "webpack --mode=development --progress --hide-modules",複製代碼
配置以後運行github
npm run dev複製代碼
會打開一個服務並彈出1web
可是webpack會有一個警告,這個警告就是由於沒有配置mode,就是沒有配置相應模式
mode有兩個參數,一個是開發模式development一個是生產模式production。
能夠在package.json裏直接配置
"dev": "webpack-dev-server --mode=development --open --hot"複製代碼
接下來運行
npm run build複製代碼
會打包生成一個新的dist文件夾
npm i babel-loader babel-core babel-preset-env -D複製代碼
babel-preset-env 幫助咱們配置 babel。咱們只須要告訴它咱們要兼容的狀況(目標運行環境),它就會自動把代碼轉換爲兼容對應環境的代碼。
更改webpack.config.js文件
module: {
rules: [
{
test: '/\.js$/',
include: path.resolve(__dirname + '/src'),
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: ['env']
}
]
}
]
}複製代碼
import Vue from 'vue';
new Vue({
el: '#app',
data: {
msg: 'hello'
}
})複製代碼
運行項目發現報錯
vue.runtime.esm.js:620 [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
(found in <Root>)
複製代碼
報這個錯誤緣由就是由於使用的是運行版本的vue,編譯版本不能用,這時候在咱們須要隨後咱們還要配置別名,將 resolve.alias
配置爲以下對象
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': path.resolve(__dirname, '/src')
}
}複製代碼
而後在運行項目,發現已經在頁面上打印出了hello。
一個簡單的基於webpack的vue項目已經搭建好了。
接下來就是一些配置
輸入命令下載style-loader css-loader
npm i style-loader css-loader -D複製代碼
配置module中的rules
{
test: /\.css$/,
use:['style-loader','css-loader'],
include: path.resolve(__dirname + '/src/'),
exclude: /node_modules/
}複製代碼
測試引入css,新建index.css並在在main.js中引入
index.css
div{ color:skyblue; } 複製代碼
import './index.css';複製代碼
能夠看到文字顏色已經改變了
輸入命令下載file-loader url-loader
npm i file-loader url-loader -D複製代碼
配置module中的rules
{
test: /\.(jpg|png|gif|svg)$/,
use: 'url-loader',
include: path.resolve(__dirname + '/src/'),
exclude: /node_modules/
}複製代碼
測試引入圖片,新建asset文件夾放一張圖片並在在main.js中引入
import img from'./assets/a.png'複製代碼
在html中顯示
<img :src="img" alt="">複製代碼
輸入命令下載html-webpack-plugin
npm i html-webpack-plugin -D複製代碼
設置plugins
var HtmlWebpackPlugin = require('html-webpack-plugin');
plugins: [
new HtmlWebpackPlugin({
template: './index.html',
chunks: ['main']
})
]複製代碼
輸入命令下載vue-loader vue-style-loader vue-template-compiler
npm i vue-loader vue-style-loader vue-template-compiler -D複製代碼
配置vue-loader
{
test: '/\.vue$/',
loader: 'vue-loader'
}複製代碼
還須要引入vue-loader/lib/plugin
var VueLoaderPlugin = require('vue-loader/lib/plugin');複製代碼
並在plugin實例化
new VueLoaderPlugin()複製代碼
新建App.vue
<template> <h1>Hello World!</h1></template>
<script> export default { name: 'App' }</script>
<style></style>複製代碼
更改main.js
import Vue from 'vue';import App from './App.vue';
new Vue({ el: '#app', render: h => h(App)});複製代碼
運行項目
由於 vue-style-loader 封裝了 style-loader,熱更新開箱即用,可是 js 熱更新還不能用,每次修改代碼咱們都會刷新瀏覽器,因此咱們須要繼續配置。
const webpack = require('webpack');複製代碼
並在plugin中配置
new webpack.HotModuleReplacementPlugin()複製代碼
熱更新已靜開啓
到如今爲止webpack構建的vue項目已經跑起來了。
接下來就是一些優化,
resolve: {
extensions: ['.js', '.jsx','.ts','.tsx', '.scss','.json','.css'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
"@": path.resolve(__dirname, 'src'),
"components": path.resolve(__dirname, '/src/components'),
"utils": path.resolve(__dirname + '/src/utils'),
},
modules: ['node_modules']
}複製代碼
輸入命令下載sass-loader node-sass
npm i sass-loader node-sass -D
複製代碼
修改webpack.config.js的css
{
test: /\.sass$/,
use:['vue-style-loader',
'css-loader', 'sass-loader'
],
include: path.resolve(__dirname + '/src/'),
exclude: /node_modules/
},複製代碼
基本也配置個差很少了,還須要補充一些東西,之後會加上。
這一篇算是webpack構建vue項目基礎吧,下一篇會再次補充並深刻,謝謝你們,但願你們能多提意見,一塊兒在碼農的道路上越走越遠
感恩騙點star項目地址github.com/mr-mengbo/w…