我在同事开发的Vue.js项目中:
我的config文件夹是这样的:
index.js
:
'use strict'
const path = require('path')
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
host:'localhost', // can be overwritten by process.env.HOST
port: 8081, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
devtool: 'cheap-module-eval-source-map',
cacheBusting: true,
cssSourceMap: true,
},
devServer: {
historyApiFallbak: true,
hot: true,
host: "localhost", //填写你自己的IP地址
port: 8080, //填写刚刚在dev字段中找到的port端口号
inline: true,
progress: true
},
build: {
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/',
productionSourceMap: true,
devtool: 'cheap-module-source-map',
productionGzip: true, //开启 gzip 压缩
productionGzipExtensions: ['js', 'css'],
bundleAnalyzerReport: process.env.npm_config_report
}
}
prod.env.js
代码:
'use strict'
const MODEL = require('../static/config.js')
const pro= {
NODE_ENV: '"production"'
}
module.exports =Object.assign({}, pro, MODEL)
这里引入的../static/config.js
:
'use strict'
var pro= {
BASE_API: '"http://10.10.10.100:8000/"',
APP_ORIGIN: '"http://103.20.32.16:8000/"'
};
var Process=process.env
(function(){
var isNODE_ENV= Process.env.NODE_ENV=="production";
if(isNODE_ENV) return;
window.dev=pro ;
})();
dev.env.js
是这个:
'use strict'
const merge = require('webpack-merge')
const prodEnv = require('./prod.env')
module.exports = merge(prodEnv, {
NODE_ENV: '"development"',
})
我现在要配置发布的环境的API和端口,是在哪里配置呢?
在index.html
中还有全局埋点:
<script type="text/javascript">
// 全局请求埋点
'use strict'
var dev = dev || {};
dev.NODE_ENV = {
BASE_API: 'http://localhost:8000',
APP_ORIGIN: 'http://103.20.32.16:8000/'
}
</script>
我搜索全局APP_ORGIN 只有index.html和上面的../static/config.js
才有,这个是规定的变量名还是自己写的变量名?
这个变量名是自定义的,我们开发的时候为了方便会定义各种变量,来区分开发环境和生产环境,甚至是测试环境。