public:index模板css
src:vue的入口以及頁面html
其它的配置在稍後細細道來~~~vue
環境:node8.9.4 、vue2.6.十、webpack4.41.2node
1.vue頁面路由入口等(很少略過)webpack
import Vue from 'vue'
import App from './app.vue'
import router from './route.js'
import './assets/app.styl'
new Vue({
router,
render: h => h(App)
}).$mount("#app");
複製代碼
樣式主要是用stylus,固然你也能夠用sass、less等,頁面只有兩個。es6
home.vueweb
<template>
<div class="container">
<span>hello world!</span>
<img src="@/assets/bghtml_1.jpg" alt="">
</div>
</template>
<style scoped lang="stylus">
.container
span
color yellow
</style>
複製代碼
test.vuevue-cli
<template>
<div class="container">
<span>hello test!!!!</span>
</div>
</template>
<style scoped lang="stylus">
.container
span
color green
</style>
複製代碼
講幾個我配置的時候遇到的問題或應用(探索老是這麼的~~)json
1.啓動項目服務vue頁面空白?sass
配置好了一切信心滿滿的啓動開發調試,發現vue的頁面沒有展現出來(白板),我覺得是路由問題,路由就只有兩個(應該沒有問題啊~~)。因而我去查了一下vue-loade。vue-template-compiler(vue-loader有詳細介紹) 就是這個東西,安裝後去webpack的plugin中引用,以下
3.處理靜態資源只引入url-loader報錯?
4.提取css爲單獨的文件?
按照官方的安裝、配置,一切看起來都是那麼的完美,可是webpack打包的時候也沒報錯,也沒warning。找來找去找不到問題所在,這個時候只有求助於google搜索引擎,看見一篇貼子說若是webpack作tree shaking(一般用於描述移除 JavaScript上下文中的未引用代碼)的時候,必定要把css文件標記爲無反作用,不然打包會移除css文件。
5.babel中.babelrc的配置。
若是要使用es6的語法,必需要用babel轉譯爲遊覽器可運行的代碼。你要使用一些includes方法等,必需要使用@ babel / polyfill或者@babel/plugin-transform-runtime,自行查詢babel知識。 固然也能夠:
完整配置webpack.dev.js開發環境,webpack.pro.js生產環境,webpack.common.js公用配置
webpack.dev.js
const webpackMerge = require('webpack-merge');
const common = require('./webpack.common');
const webpack = require('webpack');
const path = require('path');
module.exports = webpackMerge({
mode: 'development',
devServer: {
// contentBase: '../dist',
publicPath: '/',
compress: true,
hot: true,
hotOnly: true,
port: 8080,
overlay: true,
clientLogLevel: 'none'
},
devtool: 'cheap-module-eval-source-map',
module: {
rules: [
{
test: /\.(stylus|styl)$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 2
}
},
'postcss-loader',
'stylus-loader',
]
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
'postcss-loader'
]
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin()
]
}, common);
複製代碼
webpack.pro.js
const webpackMerge = require('webpack-merge');
const common = require('./webpack.common');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
module.exports = webpackMerge({
mode: 'production',
module:{
rules:[
{
test: /\.(stylus|styl)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 2
}
},
'postcss-loader',
'stylus-loader',
]
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader'
]
}
]
},
plugins:[
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "css/[name].[hash].css",
}),
// 每次打包刪除上次的包文件
new CleanWebpackPlugin()
]
}, common);
複製代碼
webpack.common.js
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const htmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
app: './src/index.js'
},
output: {
path: path.resolve(__dirname, '../dist'),
filename: 'js/[name].[hash].js'
},
resolve:{
alias: {
'@': path.resolve(__dirname, '../src/'),
}
},
optimization: {
// 生產環境默認啓用tree shaking
usedExports: true,
// 用於代碼分離
splitChunks: {
chunks: 'all'
}
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node-modules/,
use: [
{
loader: 'babel-loader'
}
]
},
{
test: /\.vue$/,
use: [
'vue-loader'
]
},
{
test: /\.(png|jpg|gif)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
outputPath: 'images/'
}
}
]
}
]
},
plugins: [
// 使用vue必要的plugins
new VueLoaderPlugin(),
// 自動生成index.html
new htmlWebpackPlugin({
template: 'public/index.html',
favicon: 'public/favicon.ico',
inject: true
})
]
};
複製代碼
最終打包結果。