Skip to content

Commit

Permalink
优化配置代码
Browse files Browse the repository at this point in the history
  • Loading branch information
vvchen committed May 25, 2017
1 parent fc90dd7 commit 0d3b87c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 20 deletions.
6 changes: 4 additions & 2 deletions build/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var devMiddleware = require('webpack-dev-middleware')(compiler, {
})

var hotMiddleware = require('webpack-hot-middleware')(compiler, {
log: () => {}
log: () => { }
})
// force page reload when html-webpack-plugin template changes
compiler.plugin('compilation', function (compilation) {
Expand All @@ -53,7 +53,9 @@ Object.keys(proxyTable).forEach(function (context) {
})

// handle fallback for HTML5 history API
app.use(require('connect-history-api-fallback')())
app.use(require('connect-history-api-fallback')({
index: '/index.html'
}))

// serve webpack bundle output
app.use(devMiddleware)
Expand Down
2 changes: 1 addition & 1 deletion build/vendor-manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"vendor_ba6bb1a87c3b81f444e6","content":{"./node_modules/axios/lib/utils.js":{"id":0,"meta":{}},"./node_modules/process/browser.js":{"id":1,"meta":{}},"./node_modules/axios/lib/defaults.js":{"id":2,"meta":{}},"./node_modules/webpack/buildin/global.js":{"id":3,"meta":{}},"./node_modules/axios/lib/adapters/xhr.js":{"id":4,"meta":{}},"./node_modules/axios/lib/cancel/Cancel.js":{"id":5,"meta":{}},"./node_modules/axios/lib/cancel/isCancel.js":{"id":6,"meta":{}},"./node_modules/axios/lib/core/createError.js":{"id":7,"meta":{}},"./node_modules/axios/lib/helpers/bind.js":{"id":8,"meta":{}},"./node_modules/axios/index.js":{"id":9,"meta":{}},"./node_modules/element-ui/lib/index.js":{"id":10,"meta":{}},"./node_modules/vue-router/dist/vue-router.esm.js":{"id":11,"meta":{"harmonyModule":true},"exports":["default"]},"./node_modules/vue/dist/vue.common.js":{"id":12,"meta":{}},"./node_modules/vuex/dist/vuex.esm.js":{"id":13,"meta":{"harmonyModule":true},"exports":["Store","mapState","mapMutations","mapGetters","mapActions","default"]},"./node_modules/axios/lib/axios.js":{"id":14,"meta":{}},"./node_modules/axios/lib/cancel/CancelToken.js":{"id":15,"meta":{}},"./node_modules/axios/lib/core/Axios.js":{"id":16,"meta":{}},"./node_modules/axios/lib/core/InterceptorManager.js":{"id":17,"meta":{}},"./node_modules/axios/lib/core/dispatchRequest.js":{"id":18,"meta":{}},"./node_modules/axios/lib/core/enhanceError.js":{"id":19,"meta":{}},"./node_modules/axios/lib/core/settle.js":{"id":20,"meta":{}},"./node_modules/axios/lib/core/transformData.js":{"id":21,"meta":{}},"./node_modules/axios/lib/helpers/btoa.js":{"id":22,"meta":{}},"./node_modules/axios/lib/helpers/buildURL.js":{"id":23,"meta":{}},"./node_modules/axios/lib/helpers/combineURLs.js":{"id":24,"meta":{}},"./node_modules/axios/lib/helpers/cookies.js":{"id":25,"meta":{}},"./node_modules/axios/lib/helpers/isAbsoluteURL.js":{"id":26,"meta":{}},"./node_modules/axios/lib/helpers/isURLSameOrigin.js":{"id":27,"meta":{}},"./node_modules/axios/lib/helpers/normalizeHeaderName.js":{"id":28,"meta":{}},"./node_modules/axios/lib/helpers/parseHeaders.js":{"id":29,"meta":{}},"./node_modules/axios/lib/helpers/spread.js":{"id":30,"meta":{}},"./node_modules/base64-js/index.js":{"id":31,"meta":{}},"./node_modules/buffer/index.js":{"id":32,"meta":{}},"./node_modules/ieee754/index.js":{"id":33,"meta":{}},"./node_modules/isarray/index.js":{"id":34,"meta":{}},"./node_modules/vue/dist/vue.runtime.esm.js":{"id":35,"meta":{"harmonyModule":true},"exports":["default"]}}}
{"name":"vendor_52d3196af34d31c412d3","content":{"./node_modules/axios/lib/utils.js":{"id":0,"meta":{}},"./node_modules/process/browser.js":{"id":1,"meta":{}},"./node_modules/axios/lib/defaults.js":{"id":2,"meta":{}},"./node_modules/webpack/buildin/global.js":{"id":3,"meta":{}},"./node_modules/axios/lib/adapters/xhr.js":{"id":4,"meta":{}},"./node_modules/axios/lib/cancel/Cancel.js":{"id":5,"meta":{}},"./node_modules/axios/lib/cancel/isCancel.js":{"id":6,"meta":{}},"./node_modules/axios/lib/core/createError.js":{"id":7,"meta":{}},"./node_modules/axios/lib/helpers/bind.js":{"id":8,"meta":{}},"./node_modules/axios/index.js":{"id":9,"meta":{}},"./node_modules/element-ui/lib/index.js":{"id":10,"meta":{}},"./node_modules/vue-router/dist/vue-router.esm.js":{"id":11,"meta":{"harmonyModule":true},"exports":["default"]},"./node_modules/vue/dist/vue.common.js":{"id":12,"meta":{}},"./node_modules/vuex/dist/vuex.esm.js":{"id":13,"meta":{"harmonyModule":true},"exports":["Store","mapState","mapMutations","mapGetters","mapActions","default"]},"./node_modules/axios/lib/axios.js":{"id":14,"meta":{}},"./node_modules/axios/lib/cancel/CancelToken.js":{"id":15,"meta":{}},"./node_modules/axios/lib/core/Axios.js":{"id":16,"meta":{}},"./node_modules/axios/lib/core/InterceptorManager.js":{"id":17,"meta":{}},"./node_modules/axios/lib/core/dispatchRequest.js":{"id":18,"meta":{}},"./node_modules/axios/lib/core/enhanceError.js":{"id":19,"meta":{}},"./node_modules/axios/lib/core/settle.js":{"id":20,"meta":{}},"./node_modules/axios/lib/core/transformData.js":{"id":21,"meta":{}},"./node_modules/axios/lib/helpers/btoa.js":{"id":22,"meta":{}},"./node_modules/axios/lib/helpers/buildURL.js":{"id":23,"meta":{}},"./node_modules/axios/lib/helpers/combineURLs.js":{"id":24,"meta":{}},"./node_modules/axios/lib/helpers/cookies.js":{"id":25,"meta":{}},"./node_modules/axios/lib/helpers/isAbsoluteURL.js":{"id":26,"meta":{}},"./node_modules/axios/lib/helpers/isURLSameOrigin.js":{"id":27,"meta":{}},"./node_modules/axios/lib/helpers/normalizeHeaderName.js":{"id":28,"meta":{}},"./node_modules/axios/lib/helpers/parseHeaders.js":{"id":29,"meta":{}},"./node_modules/axios/lib/helpers/spread.js":{"id":30,"meta":{}},"./node_modules/base64-js/index.js":{"id":31,"meta":{}},"./node_modules/buffer/index.js":{"id":32,"meta":{}},"./node_modules/ieee754/index.js":{"id":33,"meta":{}},"./node_modules/isarray/index.js":{"id":34,"meta":{}},"./node_modules/vue/dist/vue.runtime.esm.js":{"id":35,"meta":{"harmonyModule":true},"exports":["default"]}}}
14 changes: 13 additions & 1 deletion build/webpack.dev.conf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var path = require('path')
var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
Expand All @@ -11,6 +12,16 @@ var CopyWebpackPlugin = require('copy-webpack-plugin')
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
})

// 获取带hash值的dll文件名称
var glob=require('glob')
var dllJsFilePath,
dllJsFileName
dllJsFilePath=glob.sync(path.join(__dirname, '../src/vendor_*.dll.js'))[0]
if (dllJsFilePath){
dllJsFileName= path.basename(dllJsFilePath)
}

module.exports = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
Expand All @@ -29,13 +40,14 @@ module.exports = merge(baseWebpackConfig, {
filename: 'index.html',
favicon: './src/assets/img/favicon.ico',
template: 'index.html',
dllJsFileName : dllJsFileName,
inject: true
}),

// 拷贝mock数据文件到构建目录
new CopyWebpackPlugin([
{ from: 'src/mock', to: 'mock' },
{ from: 'src/vendor.dll.js', to: 'static/js' }
{ from: 'src/vendor_*.dll.js', to: 'static/js/[name].[ext]' }
], {
// 忽略选项
ignore: [
Expand Down
2 changes: 1 addition & 1 deletion build/webpack.dll.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
},
output: {
path: path.resolve(__dirname, '../src'),
filename: '[name].dll.js',
filename: '[name]_[hash].dll.js',
library: '[name]_[hash]'
},
module: {
Expand Down
13 changes: 12 additions & 1 deletion build/webpack.prod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
var CopyWebpackPlugin = require('copy-webpack-plugin')
var env = config.build.env

// 获取带hash值的dll文件名称
var glob=require('glob')
var dllJsFilePath,
dllJsFileName
dllJsFilePath=glob.sync(path.join(__dirname, '../src/vendor_*.dll.js'))[0]
if (dllJsFilePath){
// dllJsFileName= dllJsFilePath.split('src/')[1]
dllJsFileName= path.basename(dllJsFilePath)
}

var webpackConfig = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({
Expand Down Expand Up @@ -53,6 +63,7 @@ var webpackConfig = merge(baseWebpackConfig, {
filename: config.build.index,
favicon: './src/assets/img/favicon.ico',
template: 'index.html',
dllJsFileName: dllJsFileName,
inject: true,
minify: {
removeComments: true,
Expand Down Expand Up @@ -87,7 +98,7 @@ var webpackConfig = merge(baseWebpackConfig, {
}),
// 拷贝vendor.dll.js到构建目录
new CopyWebpackPlugin([
{ from: 'src/vendor.dll.js', to: 'static/js' }
{ from: 'src/vendor_*.dll.js', to: 'static/js/[name].[ext]' }
]),
new PrerenderSpaPlugin(
path.join(__dirname, '../dist'),
Expand Down
2 changes: 1 addition & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ module.exports = {
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
cssSourceMap: true
}
}
2 changes: 1 addition & 1 deletion config/site.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @desc 配置依赖服务器地址
* @type {{SERVER_ADDRESS: string, CENTER_ADDRESS: string, USERCENTER_ADDRESS: string, CDN_ADDRESS: string}}
*/
const address = {
var address = {
// api服务器
API_ADDRESS: 'http://192.168.26.119:1210',
// API_ADDRESS: "http://192.168.23.224",
Expand Down
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
s.parentNode.insertBefore(hm, s);
})();
</script>
<script src="<%= htmlWebpackPlugin.options.assetsPublicPath %>static/js/vendor.dll.js"></script>
<!--<script src="<%= htmlWebpackPlugin.options.assetsPublicPath %>static/js/vendor.dll.js"></script>-->
<script src="<%= htmlWebpackPlugin.options.assetsPublicPath %>static/js/<%= htmlWebpackPlugin.options.dllJsFileName %>"></script>
</body>

</html>
22 changes: 11 additions & 11 deletions src/service/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import { Message } from 'element-ui'
var _this = Vue.prototype

// 存放请求cancelToken
var apiCancelTokens=[]
var apiCancelTokens = []
// request请求拦截处理
Vue.axios.interceptors.request.use(function (config) {
// 添加取消请求用的cancelToken
config.cancelToken=new Vue.axios.CancelToken(function executor(c) {
apiCancelTokens.push(c)
config.cancelToken = new Vue.axios.CancelToken(function executor(c) {
apiCancelTokens.push(c)
})
return config
}, function (error) {
Expand All @@ -44,7 +44,7 @@ import { Message } from 'element-ui'
// 如果请求未被取消,则处理错误信息
let msg = Message(`${err.config.apiName || '获取数据'}失败`)
console.error(msg.message)
}
}
return Promise.reject(err)
})

Expand All @@ -53,13 +53,13 @@ import { Message } from 'element-ui'
// 取消页面当前请求
apiRequsetCancel: function () {
// 依次取消请求
apiCancelTokens.forEach(function(cancel,k,s){cancel('请求被取消')})
apiCancelTokens.forEach(function (cancel, k, s) { cancel('请求被取消') })
// 清空cancelToken
apiCancelTokens=[]
apiCancelTokens = []
},

topCarousel: function () {
return Vue.axios.get(`${API_ADDRESS2}/indexBanner.json`, {
return Vue.axios.get(`${API_ADDRESS2}/indexBanner1.json`, {
apiName: '获取首页顶部banner'
})
},
Expand Down Expand Up @@ -97,12 +97,12 @@ import { Message } from 'element-ui'
username: user,
password: pwd
}, {
apiName: '用户登录'
})
apiName: '用户登录'
})
},

// 获取RSA公钥
getRSA: function(){
getRSA: function () {
return Vue.axios.get(`${API_ADDRESS}/getRSA`, {
params: {
t: Date.now()
Expand Down Expand Up @@ -180,7 +180,7 @@ import { Message } from 'element-ui'
return Vue.axios.post(`${API_ADDRESS}/surveyBill`, reqData)
},

detailedResearchReport () {
detailedResearchReport() {
return Vue.axios.get(`/mock/detailedResearchReport.json`)
},

Expand Down

0 comments on commit 0d3b87c

Please sign in to comment.