webpack配置
webpack.config.js :
'use strict';
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
main: './src/main.js'
},
output: {
filename: './build.js',
path: path.join(__dirname,'..','dist',)
},
module: {
loaders:[
{
test: /\.css$/,
loader:'style-loader!css-loader!autoprefixer-loader',
},
{
test: /\.less$/,
loader: 'style-loader!css-loader!autoprefixer-loader!less-loader'
},
{
test: /\.(jpg|png|svg|ttf|woff|woff2|gif)$/,
loader: 'url-loader?limit=4096&name=[name].[ext]',
options: {
limit:4096,
name:'[name].[ext]'
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: ['es2015'],
plugins: ['transform-runtime'],
}
},
{
test: /\.vue$/,
loader: 'vue-loader'
}
]
},
plugins: [
new htmlWebpackPlugin({
template:'./src/index.html',
})
]
}
自定义图标
vender-mui-dist-css-mui.css
@font-face {
font-family: Muiicons;
font-weight: normal;
font-style: normal;
src: url('../fonts/iconfont.ttf') format('truetype');
}
.icon-shouye1:before {content: "\e6cc";}
.icon-diamond:before {content: "\e653";}
.icon-huiyuan1:before {content: "\e60f";}
.icon-gouwucheman:before {content: "\e600";}
.icon-huiyuan:before {content: "\e61f";}
.icon-huiyuan2:before {content: "\e601";}
.icon-icon:before {content: "\e602";}
moment:JavaScript 日期处理类库
moment:http://momentjs.cn/
安装:
npm install moment --save
<p>发表时间:{{news.add_time | convertDate}}</p>
import Moment from 'moment';
Vue.filter('convertDate', function(value){
return Moment(value).format('YYYY-MM-DD');
});
图片懒加载
懒加载:https://mint-ui.github.io/docs/#/zh-cn2/lazyload
<img v-lazy="img.img_url">
<style>
image[lazy=loading] {
width: 40px;
height: 300px;
margin: auto;
</style>
缩略图、预览
预览: https://github.com/Ls1231/vue-preview
安装:
npm i vue-preview -S
<ul>
<li v-for="(img,index) in imgs" :key="index">
<img :src="img.src" height="100" @click="$preview.open(index, imgs)">
</li>
</ul>
<script>
// 缩略图
this.$ajax.get('getthumimages/' + pid)
.then(res=>{
this.imgs = res.data.message;
this.imgs.forEach((ele)=>{
ele.w = 300;
ele.h = 200; // 缩率图显示的高
})
})
.catch(err=>{
console.log(err)
});
</script>
main.js
import VuePreview from 'vue-preview'
Vue.use(VuePreview);
上拉加载更多
loadmore:https://mint-ui.github.io/docs/#/zh-cn/loadmore
<mt-loadmore
:bottom-method="loadBottom"
:bottom-all-loaded="allLoaded"
:auto-fill="isAutoFill"
ref="loadmore"> <!-- 上拉完毕调用该元素的onBottomLoaded函数 -->
</mt-loadmore>
<script>
export default(){
data(){
return {
allLoaded:false, // 是否禁止出发上拉函数,false不禁止
isAutoFill:false, // 是否自动触发上拉函数
}
},
methods:{
loadBottom(){
console.log('上拉触发了');
// 通知上拉操作已经完结
this.$refs.loadmore.onBottomLoaded();
}
}
}
</script>
文件上传
app.js
'use strict';
const express = require('express');
const expressArtTemplate = require('express-art-template');
const app = express();
const formidable = require('formidable');
const router = express.Router();
const path = require('path');
app.engine('html', expressArtTemplate);
app.set('view options', {
debug: process.env.NODE_ENV !== 'production',
extname: '.html',
});
app.set('view engine', 'html');
router.get('/', (req, res, next) => {
res.render('index');
});
router.post('/upload', (req, res, next) => {
var form = new formidable.IncomingForm();
form.uploadDir = path.join(__dirname,'files');
form.parse(req, function(err, fields, files) {
if(err) next(err);
console.log('上传文件完毕')
});
})
app.use(router);
app.use('/node_modules',express.static('./node_modules'));
app.listen(8000, () => {
console.log('服务器启动了');
})
同步上传:
<form enctype="multipart/form-data" action="/upload" method="post">
<input type="file" name="file" id="file">
<input type="submit" name="" value="提交">
</form>
异步上传:
<form>
<input type="file" name="" id="file">
<input type="submit" name="" value="提交">
</form>
<script type="text/javascript">
document.querySelector('form').onsubmit = function(e){
e.preventDefault();
var formData = new FormData();
formData.append('myFile',document.getElementById('file').files[0])
var xhr = new XMLHttpRequest();
xhr.open('post','/upload');
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status === 200){
alert('成功');
}
}
xhr.send(formData);
}
</script>
jquery上传:
<form>
<input type="file" name="" id="file">
<input type="submit" name="" value="提交">
</form>
<script type="text/javascript" src="/node_modules/jquery/dist/jquery.js"></script>
<script type="text/javascript">
$('form').on('submit', function(e){
var formData = new FormData();
formData.append('myFile',document.getElementById('file').files[0]);
e.preventDefault();
$.ajax({
url: '/upload',
type: 'post',
data: formData,
contentType: false,
processData: false,
success: function(){
console.log('ok');
}
})
})
</script>
打包
提取css
安装:
npm install --save-dev extract-text-webpack-plugin
使用:
const version = 'v1.1.1';
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
output: {
path: path.join(__dirname, 'dist_css'),
filename: 'build.js',
},
module:{
loaders:[
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader!autoprefixer-loader"
})
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
]
}
css+hash_1
path: path.join(__dirname, 'dist_hash'),
filename: '[chunkhash].js',
new ExtractTextPlugin("[contenthash].css"),
提取第三方包
const webpack = require('webpack');
vendors: ['vue','vue-router','moment','axios','vue-preview'],
path: path.join(__dirname, 'dist_vendors'),
new webpack.optimize.CommonsChunkPlugin({
names:['vendors','manifest']
}),
CommonsChunkPlugin:https://webpack.js.org/plugins/commons-chunk-plugin/#root
提取第三方包2
filename: '[name].[chunkhash:6].js',
new ExtractTextPlugin("[name].[contenthash:6].css"),
uglifyjs-webpack-plugin
安装:
npm install uglifyjs-webpack-plugin --save-dev
问题:Cannot read property ‘compilation’ of undefined.
解决:package.json 中改uglifyjs-webpack-plugin 版本号为"uglifyjs-webpack-plugin": "^1.0.0"
webpack.config.js
dist_uglify
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
path: path.join(__dirname, 'dist_uglify'),
new UglifyJsPlugin(),
按需加载mint-ui
import Header from 'mint-ui/lib/header';
import Switch from 'mint-ui/lib/switch';
import Swipe from 'mint-ui/lib/swipe';
import SwipeItem from 'mint-ui/lib/swipe-item';
import Lazyload from 'mint-ui/lib/lazyload';
import Loadmore from 'mint-ui/lib/loadmore';
import Indicator from 'mint-ui/lib/indicator';
import Button from 'mint-ui/lib/button';
Vue.component(Header.name, Header);
Vue.component(Switch.name, Switch);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Loadmore.name, Loadmore);
Vue.component(Button.name, Button);
Vue.use(Lazyload);
webpack.config.js
path: path.join(__dirname, 'dist_mintui_partload'),
划分目录
webpack.config.js
path: path.join(__dirname, 'dist_dir'),
// 设置资源路径的请求地址
// style.css -> /css/ assets/mui.ttf
// load的时候配置的name属性 assets/mui.ttf
publicPath:'/', // 解决报错mui.tff的路径不对问题
filename: 'js/[name].[chunkhash:6].js',
name: 'assets/[name].[ext]'
new ExtractTextPlugin("css/[contenthash].css"),
分块路由懒加载
路由懒加载:https://router.vuejs.org/zh/guide/advanced/lazy-loading.html#%E6%8A%8A%E7%BB%84%E4%BB%B6%E6%8C%89%E7%BB%84%E5%88%86%E5%9D%97
webpack.config.js
path: path.join(__dirname, 'dist_lazy_load'),
main.js
import App from './app.vue';
const Home = r => require(['./components/home/home.vue'],r);
const Member = r => require(['./components/member/member.vue'],r);
const Shopcart = r => require(['./components/shopcart/shopcart.vue'],r);
const Search = r => require(['./components/search/search.vue'],r);
const NewsList = r => require(['./components/news/newsList.vue'],r);
const NewsDetail = r => require(['./components/news/newsDetail.vue'],r);
const PhotoShare = r => require(['./components/photo/photoShare.vue'],r);
const PhotoDetail = r => require(['./components/photo/photoDetail.vue'],r);
const GoodsList = r => require(['./components/goods/goodsList.vue'],r);
const GoodsDetail = r => require(['./components/goods/goodsDetail.vue'],r);
const GoodsComment = r => require(['./components/goods/goodsComment.vue'],r);
去除警告
Production Deployment: https://vuejs.org/v2/guide/deployment.html
path: path.join(__dirname, 'dist_production'),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
|