詳解vue.js根據(jù)不同環(huán)境(正式、測試)打包到不同目錄
1、在build文件夾中創(chuàng)建testing.js文件
// 配置環(huán)境變量 type 為 testing
process.env.type = '"testing"'
// 引入build.js文件
require('./build')
2、修改config文件夾中的prod.env.js文件
module.exports = {
NODE_ENV: '"production"',
// 將上文設(shè)置的環(huán)境變量,賦值到 type 屬性上
type: process.env.type
}
3、在package.json文件中添加npm run testing命令
"testing": "node build/testing.js", // 添加testing命令 "build": "node build/build.js"
4、config ->index.js中把build這個命令復(fù)制一份改成testing(此步為了打包到不同文件夾)
build: {
env: require('./prod.env'),
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/mshop/',
/**
* Source Maps
*/
productionSourceMap: true,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
},
testing: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../testing/index.html'),
assetsRoot: path.resolve(__dirname, '../testing'),
assetsSubDirectory: 'static',
assetsPublicPath: '/',
productionSourceMap: true,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
},
5、修改build->webpack.prod.conf文件
修改filename
new HtmlWebpackPlugin({
filename: process.env.type == '"testing"' ? config.testing.index : config.build.index
}),
修改output
output: {
path: process.env.type == '"testing"' ? config.testing.assetsRoot : config.build.assetsRoot,
},
6、修改build->build.js文件
路徑都修改為根據(jù)正式、測試環(huán)境判斷(不然執(zhí)行npm run testing, npm run build命令時輸出的文件有問題)
7、根據(jù)不同環(huán)境配置不同域名地址
let baseURL
if (process.env.NODE_ENV === 'production') {
if (process.env.type === 'testing') { // 測試環(huán)境
baseUrl = '測試環(huán)境地址'
} else { // 正式環(huán)境
baseUrl = '正式環(huán)境地址'
}
} else { // 本地環(huán)境
baseUrl = '本地環(huán)境地址'
}
最后執(zhí)行:
npm run testing 就會執(zhí)行測試環(huán)境配置的地址,并生成testing文件夾
npm run build就會執(zhí)行正式環(huán)境配置的地址,并生成dist文件夾
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用echarts餅狀圖label既在內(nèi)部顯示數(shù)值(百分比),又顯示外部指示線
這篇文章主要介紹了使用echarts餅狀圖label既在內(nèi)部顯示數(shù)值(百分比),又顯示外部指示線問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
JavaScript 開發(fā)規(guī)范要求(圖文并茂)
作為一名開發(fā)人員(WEB前端JavaScript開發(fā)),不規(guī)范的開發(fā)不僅使日后代碼維護(hù)變的困難,同時也不利于團(tuán)隊的合作,通常還會帶來代碼安全以及執(zhí)行效率上的問題。2010-06-06
解決layui 復(fù)選框等內(nèi)置控件不顯示的問題
今天小編就為大家分享一篇解決layui 復(fù)選框等內(nèi)置控件不顯示的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
JavaScript ES6 Class類實現(xiàn)原理詳解
這篇文章主要介紹了JavaScript ES6 Class類實現(xiàn)原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05

