初始化:
npm init -y
安装webpack
npm install webpack webpack-cli --save-dev
安装vue
npm i --save-dev vue vue-loader vue-template-compiler
安装css的插件
npm install --save-dev style-loader css-loader
npm install --save-dev file-loader
webpack.config.js 配置文件
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
module.exports = {
entry: './src/main.js',
output:{
path: path.resolve(__dirname,'dist'),
filename :'bundle.js'
},
plugins:[
new VueLoaderPlugin()
],
module:{
rules:[
{
test: /\.vue$/,
use:'vue-loader'
},
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
]
},
{
test: /\.(png|svg|jpg|gif)$/,
use:[
{
loader:'file-loader',
options: {
esModule: false,
}
}
]
}
]
},
resolve:{
alias:{
"vue$":"vue/dist/vue.js"
}
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<p>{{msg}}</p>
</div>
<script src="../dist/bundle.js"></script>
</body>
</html>
main.js
import Vue from 'vue'
import app from './app.vue'
var vm = new Vue({
el: '#app',
data: {
msg: '123'
},
render:c=>c(app)
})
app.vue
<template>
<div>
<div class="example">{{ msg }}</div>
<img src="./image/1.gif">
</div>
</template>
<script>
export default {
data () {
return {
msg: 'Hello world!'
}
}
}
</script>
<style>
.example {
color: red;
}
</style>
本文详细介绍了一个使用Vue框架的项目如何通过Webpack进行构建的过程。包括初始化项目、安装必要依赖如Webpack、Vue及其加载器,以及css相关插件。具体展示了webpack.config.js的配置细节,如入口文件、输出路径、插件使用、模块规则等。同时提供了index.html、main.js和app.vue等关键文件的代码示例。
318

被折叠的 条评论
为什么被折叠?



