I have a config file (config.js), which depends on two files: local.js(for development environment) and deploy.js(for production environment). After run serve
or run build
, it will create some configurations rely on the environment. I used it like the following:
<template>
</template>
<script>
import config from '@/assets/scripts/config.js'
export default {
data() {
return {
apiBasePath: config.apiBasePath
}
}
}
</script>
My config.js:
import local from '../../../config/local'
import deploy from '../../../config/deploy'
export default {
apiBasePath:
process.env.NODE_ENV === 'production'
? deploy.corsDomain + ':' + deploy.emergencyPort
: local.corsDomain + ':' + local.emergencyPort,
fileBasePath:
process.env.NODE_ENV === 'production'
? deploy.corsDomain + ':' + deploy.emergencyPort
: local.corsDomain + ':5000',
dataBasePath:
process.env.NODE_ENV === 'production'
? deploy.corsDomain + ':' + deploy.emergencyPort
: 'http://localhost' + ':' + local.emergencyPort,
vocApiPath:
process.env.NODE_ENV === 'production'
? deploy.corsDomain + ':' + deploy.sharePort
: local.corsDomain + ':' + local.sharePort,
visualBasePath:
process.env.NODE_ENV === 'production'
? deploy.corsDomain + ':' + deploy.simulatorPort
: local.corsDomain + ':' + local.simulatorPort,
spotBasePath:
process.env.NODE_ENV === 'production'
? deploy.corsDomain + ':' + deploy.samplePort
: local.corsDomain + ':' + local.samplePort,
shareBasePath:
process.env.NODE_ENV === 'production'
? deploy.corsDomain + ':' + deploy.sharePort
: local.corsDomain + ':' + local.sharePort,
envBasePath:
process.env.NODE_ENV === 'production'
? deploy.corsDomain + ':' + deploy.envPort
: local.corsDomain + ':' + local.envPort,
ep360BasePath:
process.env.NODE_ENV === 'production'
? deploy.corsDomain + ':' + deploy.ep360Port
: local.corsDomain + ':' + local.ep360Port,
defaultDivision: {
province: deploy.defaultProvince ? deploy.defaultProvince : '浙江省',
city: deploy.defaultCity ? deploy.defaultCity : '宁波市',
area: deploy.defaultArea ? deploy.defaultArea : '鄞州区'
}
}
But after buiding, these configurations will be insert in chunk-common scripts.
My destination is:
Split this config file from my common chunk as a single one(suppose its name is config.[hash].js) and it will become a script tag insert in HTML when run build
. After that, I can change some configurations in server and needn't rebuild my project when I set some wrong configurations
More Detail: My project is a multiple-entries project, there is almost import the config.js in each entry. I tried the dynamic import as @Arunmozhi said, I can't get the configurations in the data
function because of the Promise
return although the config chunk is split. I'm trying to use splitChunks
to solve my problems, but it didn't work.
optimization: {
splitChunks: {
cacheGroups: {
myconfig: {
test: module => {
module
.identifier()
.split('/')
.reduceRight(item => item)
.indexOf('config') !== -1
},
name: 'myconfig',
enforce: true,
chunks: 'all',
reuseExistingChunk: true,
minChunks: 1,
minSize: 0
}
}
}
}