这是我个人在进修vue+webpack的一个实例,愿望对读者是有效的,同时也求大神指教。菜鸟第一次写这,程度有限。
目次构造
加载依靠
在这之前确保安装了node和npm
加载依靠
{
"name": "05-five-vue",
"version": "1.0.0",
"description": "vue+webapck",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --hot --inline"
},
"dependencies": {
"vue": "^1.0.18",
"webpack": "^1.12.0"
},
"devDependencies": {
"autoprefixer-loader": "^2.0.0",
"babel": "^6.3.13",
"babel-core": "^6.3.21",
"babel-loader": "^6.2.0",
"babel-plugin-transform-runtime": "^6.3.13",
"babel-preset-es2015": "^6.3.13",
"babel-runtime": "^5.8.34",
"css-loader": "^0.16.0",
"file-loader": "^0.8.5",
"html-loader": "^0.3.0",
"node-sass": "^3.4.2",
"sass-loader": "^3.2.0",
"style-loader": "^0.12.3",
"url-loader": "^0.5.6",
"vue-html-loader": "^1.2.0",
"vue-loader": "^7.2.0",
"webpack": "^1.12.0",
"webpack-dev-server": "^1.14.0"
},
"author": "guowenfh",
"license": "MIT",
"keywords": [
"vue",
"webpack"
]
}
在根目次下建立package.json文件,复制上面的代码,在根目次的DOS敕令中运转npm install;
守候下载完后。
webpack设置文件
根目次下建立webpack.config.js文件
var path = require('path');
// NodeJS中的Path对象,用于处置惩罚目次的对象,进步开辟效力。
var HtmlWebpackPlugin = require('html-webpack-plugin');
// 模块导入
module.exports = {
entry: './src/main.js',
//定义webpack输出的文件,这里设置了让打包后天生的文件放在dist文件夹下的build.js文件中
output: {
path: path.join(__dirname, './dist'),
// 文件地点,运用绝对途径情势
filename: '[name].js',
//[name]这里是webpack供应的依据路口文件自动天生的名字
publicPath: '/dist/'
// 大众文件天生的地点
},
// 服务器设置相干,自动革新!
devServer: {
historyApiFallback: true,
hot: false,
inline: true,
port:9010
},
//加载器
module: {
// 加载器
loaders: [
// 剖析.vue文件
{ test: /\.vue$/, loader: 'vue' },
// 转化ES6的语法
{ test: /\.js$/, loader: 'babel', exclude: /node_modules/ },
// 编译css并自动增加css前缀
{ test: /\.css$/, loader: 'style!css!autoprefixer'},
//.scss 文件想要编译,scss就须要这些东西!来编译处置惩罚
//install css-loader style-loader sass-loader node-sass --save-dev
{ test: /\.scss$/, loader: 'style!css!sass?sourceMap'},
// 图片转化,小于8K自动转化为base64的编码
{ test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=8192'},
//html模板编译?
{ test: /\.(html|tpl)$/, loader: 'html-loader' },
]
},
// .vue的设置。须要零丁出来设置
vue: {
loaders: {
css: 'style!css!autoprefixer',
html:'html-loader'
}
},
// 转化成es5的语法
babel: {
presets: ['es2015'],
plugins: ['transform-runtime']
},
resolve: {
// require时省略的扩展名,如:require('module') 不须要module.js
extensions: ['', '.js', '.vue'],
// 别号,能够直接运用别号来代表设定的途径以及其他
alias: {
filter: path.join(__dirname, './src/filters'),
components: path.join(__dirname, './src/components')
}
},
//插件
plugins: [
//依据模板插进去css/js等天生终究的html
new HtmlWebpackPlugin({
filename: 'index.html',//天生的html天生途径,相对于path
inject: 'body',//js插进去的位置,true插进去head,false插进去body
template: 'index.html',//html模板途径
hash: false,//为静态资本天生hash值
minify:{//紧缩HTML文件
removeComments:true,//移除html中的解释
collapseWhitespace:false//删除空白符与换行符
}
})
]
// 开启source-map,webpack有多种source-map,在官网文档能够查到
devtool: 'eval-source-map'
}
编写重要html文件
根目次下建立index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>webpack vue</title>
<style>
#app {
margin: 20px auto;
width: 800px;
height: 600px;
}
</style>
</head>
<body>
<div id="app">
</div>
<app></app>
<script src="dist/main.js"></script>
</body>
</html>
这里引入的js是由webpack打包天生的
编写vue组件
下一步在在components目次下建立app.vue
<template>
<div>
<p>{{message}}</p>
<input v-model="message">
</div>
</template>
<script>
//es6
export default {
el:"#app",
//data:function(){}, 下面是es6写法
data () {
return {
message:"Hello vue!"
}
}
}
</script>
<style lang="sass">
/*测试一下对sass的编译*/
$qwe:#94FF97;
body {
background-color: $qwe;
h1 {
background-color: #eee;
color: red;
transform: translate(10%, 10%);
}
h1:hover {
height:100px;
}
h2 {
background-color: #999;
}
}
</style>
编写进口文件
在src目次下建立main.js文件
//es6语法:
import Vue from "vue";//引入vue
//外部引入别的库都能够用如许的体式格局,比方jquery等。。
//引入我们编写的测试用vue文件。
import app from './components/app.vue';
Vue.config.debug = true;//开启毛病提醒
//建立一个vue实例,挂载在#app上
new Vue(app);
到这里一切都预备终了,如今最先运转webpack,wabpack将文件打包到dist目次天生mian.js,比及运转终了
如今翻开builde/index.html
这里有个热加载的插件,根目次下运转 npm start 翻开http://localhost:9010/build/i…一样结果