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
})
]
};
复制代码
最终打包结果。