基於vue-cli3.0+webpack 4+vant ui + sass+ rem適配方案+axios封裝,構建手機端模板腳手架javascript
幫你作好的配置有css
以前寫過一個多環境的方案,是基於vue-cli2.0的vue多環境配置方案-傳送門
最近新的項目採用了vuecli3.0開始了一番折騰html
這裏參考了 vue-admin-template基本思路不變
在src的文件裏新建config 根據不一樣的環境去引用不一樣的配置文件,不一樣的是在根目錄下有三個設置環境變量的文件
這裏能夠參考vue-admin-templatevue
這裏要注意的是,要以VUE_APP_前綴設置,才能夠在vue中引用
java
這裏有個問題,既然這裏有了根據不一樣環境設置變量的文件,爲何還要去config下新建三個對應的文件呢?
我的比較喜歡這種引入的方式而,好比我須要在文件引入api.common_apinode
import { api } from '@/config'
// api
const { common_api } = api
複製代碼
仍是那句話,用vw仍是用rem,這是個問題?webpack
選用rem的緣由是由於[vant]直接推薦了這個適配方案,直接上手ios
================2019.06.06 更新===========================git
可使用cdn提升速度,這裏要在vue.config.js須要配置externals,修改public 下的index.htmlgithub
vue.config.js
'use strict'
const path = require('path')
const defaultSettings = require('./src/config/index.js')
function resolve(dir) {
return path.join(__dirname, dir)
}
const name = defaultSettings.title || 'vue mobile template' // page title
const port = 9018 // dev port
const externals = {
vue: 'Vue',
'vue-router': 'VueRouter',
vuex: 'Vuex',
vant: 'vant',
axios: 'axios',
'crypto-js': 'CryptoJS'
}
// cdn
const cdn = {
// 開發環境
dev: {
css: [],
js: [
'https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.4.4/polyfill.js'
]
},
// 生產環境
build: {
css: ['https://cdn.jsdelivr.net/npm/vant@beta/lib/index.css'],
js: [
'https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.4.4/polyfill.js',
'https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.0.6/vue-router.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/vuex/3.1.1/vuex.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js',
'https://cdn.jsdelivr.net/npm/vant@beta/lib/vant.min.js'
]
}
}
module.exports = {
publicPath: process.env.NODE_ENV === 'development' ? '/' : '/app/', // 須要區分生產環境和開發環境,否則build會報錯
outputDir: 'dist',
assetsDir: 'static',
lintOnSave: process.env.NODE_ENV === 'development',
productionSourceMap: false,
devServer: {
port: port,
open: true,
overlay: {
warnings: false,
errors: true
}
},
configureWebpack: config => {
// 爲生產環境修改配置...
if (process.env.NODE_ENV === 'production') {
// externals裏的模塊不打包
Object.assign(config, {
name: name,
externals: externals
})
}
// 爲開發環境修改配置...
if (process.env.NODE_ENV === 'development') {
}
},
chainWebpack(config) {
config.plugins.delete('preload') // TODO: need test
config.plugins.delete('prefetch') // TODO: need test
// alias
config.resolve.alias
.set('@', resolve('src'))
.set('assets', resolve('src/assets'))
.set('views', resolve('src/views'))
.set('components', resolve('src/components'))
/** * 添加CDN參數到htmlWebpackPlugin配置中, 詳見public/index.html 修改 */
config.plugin('html').tap(args => {
if (process.env.NODE_ENV === 'production') {
args[0].cdn = cdn.build
}
if (process.env.NODE_ENV === 'development') {
args[0].cdn = cdn.dev
}
return args
})
// set preserveWhitespace
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
.tap(options => {
options.compilerOptions.preserveWhitespace = true
return options
})
.end()
config
// https://webpack.js.org/configuration/devtool/#development
.when(process.env.NODE_ENV === 'development', config =>
config.devtool('cheap-source-map')
)
config.when(process.env.NODE_ENV !== 'development', config => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.after('html')
.use('script-ext-html-webpack-plugin', [
{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}
])
.end()
config.optimization.splitChunks({
chunks: 'all',
cacheGroups: {
libs: {
name: 'chunk-libs',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // only package third parties that are initially dependent
},
// elementUI: {
// name: 'chunk-elementUI', // split elementUI into a single package
// priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
// test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
// },
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // can customize your rules
minChunks: 3, // minimum common number
priority: 5,
reuseExistingChunk: true
}
}
})
config.optimization.runtimeChunk('single')
})
}
}
複製代碼
public/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<% for (var i in htmlWebpackPlugin.options.cdn&&htmlWebpackPlugin.options.cdn.css) { %>
<link href="<%= htmlWebpackPlugin.options.cdn.css[i] %>" rel="preload" as="style" />
<link href="<%= htmlWebpackPlugin.options.cdn.css[i] %>" rel="stylesheet" />
<% } %>
<title><%= webpackConfig.name %></title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= webpackConfig.name %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- 使用CDN加速的JS文件,配置在vue.config.js下 -->
<% for (var i in htmlWebpackPlugin.options.cdn&&htmlWebpackPlugin.options.cdn.js) { %>
<script src="<%= htmlWebpackPlugin.options.cdn.js[i] %>"></script>
<% } %>
<!-- built files will be auto injected -->
</body>
</html>
複製代碼
由於babel研究的還不夠透徹,因此採用了最笨的方法直接引入babel-polyfill,生產環境和開發環境都直接引用cdn js
babel.config.js 添加 useBuiltIns: 'entry'
babel.config.js
module.exports = {
presets: [['@vue/app', { useBuiltIns: 'entry' }]],
plugins: [
[
'import',
{
libraryName: 'vant',
libraryDirectory: 'es',
style: true
},
'vant'
]
]
}
複製代碼
由於項目剛剛構建起來,後面還會持續更新,實際使用過程當中必定還有不少問題,若是文章中有錯誤但願可以被指正,一塊兒成長
您能夠掃描添加下方的微信並備註 Sol 加交流羣,給我提意見,交流學習。
若是對你有幫助送我一顆小星星(づ ̄3 ̄)づ╭❤~
轉載請聯繫做者!