Webpack 预编译 Vue
需要的包:
- vue-loader
- vue-template-compiler
webpack配置文件:
const path = require('path')
module.exports = {
entry: './src/main.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, '../dist')
},
resolve: {
alias: {
'@': path.resolve(__dirname, '../src')
}
},
module: {
rules: [
{ test: /\.vue$/, loader: 'vue-loader' }
]
}
}
入口main.js:
import Vue from 'vue'
import App from '@/App.vue'
new Vue({
el: '#app',
render: (h) => h(App)
})
初始index.html:
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta http-equiv="x-dns-prefetch-control" content="on">
<title>Vuex</title>
</head>
<body>
<div id="app"></div>
<script src="dist/main.js"></script>
</body>
</html>
package.json:
{
"name": "vuex",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack --config config/webpack.config.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"vue": "^2.4.4"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"css-loader": "^0.28.7",
"style-loader": "^0.19.0",
"vue-loader": "^13.2.1",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0"
}
}