项目目录
package.json
{
"name": "webpack-vue",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"build": "webpack --config webpack.config.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^3.2.0",
"mini-css-extract-plugin": "^0.8.0",
"style-loader": "^1.0.0",
"vue": "^2.6.10",
"vue-loader": "^15.7.1",
"vue-template-compiler": "^2.6.10",
"webpack": "^4.39.3",
"webpack-cli": "^3.3.7"
}
}
- vue-loader和vue-template-compiler需一起使用,用于编译.vue文件
- css-loader或style-loader处理样式
- mini-css-extract-plugin:将vue里的CSS提取出来,整合成一个css文件
- 编译打包执行:npm run build
webpack.config.js
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
mode: 'production',
entry: './src/main.js', //webpack读取的入口文件
output: { //打包输出文件路径
path: path.resolve(__dirname, 'dist'),
filename: 'index.js'
},
module: {
rules: [
{
test: /\.vue$/,
use: 'vue-loader',
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
}
]
},
plugins: [
new VueLoaderPlugin(),
new MiniCssExtractPlugin({
filename: "style.css",
chunkFilename: "[id].css"
})
],
resolve: {
extensions: ['.js','.json','.vue'],
},
}
main.js
import Vue from 'vue';
import App from './App'
new Vue({
el: '#app',
render: h => h(App)
})
App.vue(vue入口文件)
<template>
<div>
<p>{{msg}}</p>
<input type="text" v-model="msg">
<button class="btn">按钮</button>
</div>
</template>
<script>
export default {
data(){
return {
msg: "Hello,world"
}
},
}
</script>
<style>
.btn{
color:red;
}
</style>
index.html
<!DOCTYPE html>
<html>
<head>
<title>搭建vue开发环境</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0">
<link rel="stylesheet" href="./dist/style.css"/>
</head>
<body>
<div id="app">
</div>
<script src="./dist/index.js"></script>
</body>
</html>