How to package Vue projects by environment
This time I will show you how to package the Vue project by environment. What are the precautions for Vue project packaging by environment? The following is a practical case, let’s take a look.
Step 1: Install cross-env
Run the following command in the project directory to install cross-env. My IDE is webstorm. You can run it directly in the Terminal window in the IDE. You can also navigate to the project root directory through Windows CMD or Linux Terminal and run the following command.
npm i --save-dev cross-env
Step 2: Modify the parameters in each environment
Add test.env.js and pre.env.js in the config/ directory. Modify the content in prod.env.js. The modified content is as follows:
'use strict' module.exports = { NODE_ENV: '"production"', EVN_CONFIG:'"prod"', API_ROOT:'"/apis/v1"' }
Study and modify the contents of the test.env.js and pre.env.js files respectively. The modified contents are as follows:
'use strict' module.exports = { NODE_ENV: '"testing"', EVN_CONFIG:'"test"', API_ROOT:'"/test/apis/train"' } 'use strict' module.exports = { NODE_ENV: '"presentation"', EVN_CONFIG:'"pre"', API_ROOT:'"/pre/apis/train"' }
Modify the content of the dev.env.js file. The modified content is as follows. The dev environment is configured with a service proxy, and the api before API_ROOT is the configured proxy address.
module.exports = merge(prodEnv, { NODE_ENV: '"development"', VN_CONFIG: '"dev"', API_ROOT: '"api/apis/v1"' })
Step 3: Modify the project package.json file
Personalize the scripts content in the package.json file and add several newly defined packaging processes for the environment. The parameters therein are consistent with the previous adjustments.
"scripts": { "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js", "start": "npm run dev", "build": "node build/build.js", "build:test": "cross-env NODE_ENV=production env_config=test node build/build.js", "build:pre": "cross-env NODE_ENV=production env_config=pre node build/build.js", "build:prod": "cross-env NODE_ENV=production env_config=prod node build/build.js" },
Here, it is best to set NODE_ENV to production, because only one judgment of production is made in utils.js, and personal testing will not affect the API parameters of each environment. ##Step 4: Modify config/index.js
Modify the build parameters in the config/index.js file. The parameters here will be used in build/webpackage.prod.conf.js
build:{ // Template for index.html // 添加test pre prod 三处环境的配制 prodEnv: require('./prod.env'), preEnv: require('./pre.env'), testEnv: require('./test.env'), //下面为原本的内容,不需要做任何个性 index:path.resolve(dirname,'../dist/index.html'),
Step 5: Use build environment parameters in webpackage.prod.conf.js
Modify the build/webpackage.prod.conf.js file and adjust the generation method of envconstants.
// 个性env常量的定义 // const env = require('../config/prod.env') const env = config.build[process.env.env_config+'Env']
Step 6: Adjust build/build.js
Delete the assignment of process.env.NODE_ENV and modify the definition of spinner. The adjusted content is as follows:
'use strict' require('./check-versions')() // 注释掉的代码 // process.env.NODE_ENV = 'production' const ora = require('ora') const rm = require('rimraf') const path = require('path') const chalk = require('chalk') const webpack = require('webpack') const config = require('../config') const webpackConfig = require('./webpack.prod.conf') // 修改spinner的定义 // const spinner = ora('building for production...') var spinner = ora('building for ' + process.env.NODE_ENV + ' of ' + process.env.env_config+ ' mode...' ) spinner.start() //更多的其它内容,不需要做任何调整的内容 ...
Replenish:
vue2 How to package webpack by environment
This year I had the opportunity to work on a vue2 single-page application project, and I went through two environments from development to launch. I run npm run in both the test environment and the official environment. build. The variables of these two environments are currently different. It feels a little troublesome to change the variables every time when packaging. Later, I referred to my colleagues. In their projects, they ran different commands according to the environment and obtained different packages. For example, test environment npm run test, formal environment npm run build.
Need to be configured in the file config/prod.env.js
const target = process.env.npm_lifecycle_event; if (target == 'test') { //测试 var obj = { NODE_ENV: '"production"', //post用当前域名 API_ROOT: '""', //数据字典 API_ROOT_DICT:'"http://test.gw.fdc.com.cn"', } }else { //线上 var obj = { NODE_ENV: '"production"', //post用当前域名 API_ROOT: '""', //数据字典 API_ROOT_DICT:'"http://gw.fdc.com.cn"', } } module.exports = obj;
npm provides an npm_lifecycle_event variable, which returns the name of the currently running script, such as pretest, test, posttest, etc. Therefore, you can use this variable to write code for different npm scripts commands in the same script file.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of webpack's mobile-side automatic construction rem method
Details of using angular2 modules and shared modules introduce
The above is the detailed content of How to package Vue projects by environment. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Windows Recovery Environment (WinRE) is an environment used to repair Windows operating system errors. After entering WinRE, you can perform system restore, factory reset, uninstall updates, etc. If you are unable to boot into WinRE, this article will guide you through fixes to resolve the issue. Unable to boot into the Windows Recovery Environment If you cannot boot into the Windows Recovery Environment, use the fixes provided below: Check the status of the Windows Recovery Environment Use other methods to enter the Windows Recovery Environment Did you accidentally delete the Windows Recovery Partition? Perform an in-place upgrade or clean installation of Windows below, we have explained all these fixes in detail. 1] Check Wi

In this article, we will learn about the differences between Python and Anaconda. What is Python? Python is an open source language that places great emphasis on making the code easy to read and understand by indenting lines and providing whitespace. Python's flexibility and ease of use make it ideal for a variety of applications, including but not limited to scientific computing, artificial intelligence, and data science, as well as creating and developing online applications. When Python is tested, it is immediately translated into machine language because it is an interpreted language. Some languages, such as C++, require compilation to be understood. Proficiency in Python is an important advantage because it is very easy to understand, develop, execute and read. This makes Python

Share the simple and easy-to-understand PyCharm project packaging method. With the popularity of Python, more and more developers use PyCharm as the main tool for Python development. PyCharm is a powerful integrated development environment that provides many convenient functions to help us improve development efficiency. One of the important functions is project packaging. This article will introduce how to package projects in PyCharm in a simple and easy-to-understand way, and provide specific code examples. Why package projects? Developed in Python

As the Python programming language becomes increasingly popular, more and more developers are starting to write code in Python. But in actual use, we often need to package these codes and distribute them to others for use. This article will introduce how to use Python regular expressions for code packaging and distribution. 1. Python code packaging In Python, we can use tools such as setuptools and distutils to package our code. These tools can convert Python files, modules

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node.js project into an executable file. I hope it will be helpful to you!

Detailed explanation of VSCode functions: How does it help you improve work efficiency? With the continuous development of the software development industry, developers' pursuit of work efficiency and code quality have become important goals in their work. In this process, the choice of code editor becomes a key decision. Among many editors, Visual Studio Code (VSCode for short) is loved by the majority of developers for its powerful functions and flexible scalability. This article will introduce some functions of VSCode in detail and discuss

PyInstaller: Independence of Python applications PyInstaller is an open source python packaging tool that packages Python applications and their dependencies into an independent executable file. This process eliminates dependence on the Python interpreter while allowing applications to run on a variety of platforms, including Windows, MacOS, and Linux. Packaging Process The packaging process of PyInstaller is relatively simple and involves the following steps: pipinstallpyinstallerpyinstaller--onefile--windowedmain.py--onefile option creates a single

In Linux, packaging refers to a collection of files or directories, and this collection is stored in a file; simply put, packaging refers to turning a large number of files or directories into a total file. The packed file is not compressed, so the space it takes up is the sum of all files and directories in it.
