手动配置vux详解

前言

vux文档的确很好。。。,但是有些细节部分不是那么清晰,希望此文对你有所帮助

初始化项目

vue-cli初始化项目

1
vue init webpack <项目名>

成功运行项目后

安装vux

进入到项目里面

1
npm install vux --save

修改配置文件

修改build/webpack.base.conf.js

参考vux文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
const vuxLoader = require('vux-loader')

function resolve (dir) {
return path.join(__dirname, '..', dir)
}



const webpackConfig = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
}
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
node: {
// prevent webpack from injecting useless setImmediate polyfill because Vue
// source contains it (although only uses it if it's native).
setImmediate: false,
// prevent webpack from injecting mocks to Node native modules
// that does not make sense for the client
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty'
}
}

module.exports = vuxLoader.merge(webpackConfig, {
plugins: ['vux-ui']
})

安装vue-loader

1
npm install vux-loader --save-dev

上一步的webpack.base.conf.js已经完成配置

1
2
3
module.exports = vuxLoader.merge(webpackConfig, {
plugins: ['vux-ui']
})

安装less-loader以正确编译less源码

到这里已经可以直接用了(起码我暂时没发现什么问题

1
cnpm install less less-loader --save

其他配置

安装 yaml-loader 以正确进行语言文件读取

(貌似是支持国际化)

1
npm install yaml-loader --save-dev

添加 viewport meta(不解释)

1
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">

添加 Fastclick 移除移动端点击延迟

src/main.js里面添加如下代码

1
2
const FastClick = require('fastclick')
FastClick.attach(document.body)

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import FastClick from 'fastclick'

Vue.config.productionTip = false

FastClick.attach(document.body)

/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})

路由

按自己的来就好了

添加 webpack plugin, 在构建后去除重复css代码

1
2
3
plugins: [{
name: 'duplicate-style'
}]

配置build/webpack.base.conf.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module.exports = vuxLoader.merge(webpackConfig, {
plugins: [
"vux-ui",
{
name: "duplicate-style",
options: {
cssProcessorOptions: {
safe: true,
zindex: false,
autoprefixer: {
add: true,
browsers: ["iOS >= 7", "Android >= 4.1"]
}
}
}
}
]
});