Common options for file devServer fields
Generated in Vue CLIIn the file,
devServer
Fields are used to configure the development server options. Here are some descriptions of some of the commonly used options:
-
host
: Specify the host name of the development server, the default value is'localhost'
. You can set it to'0.0.0.0'
, to allow access to the development server through other devices in the LAN. For example,host: '0.0.0.0'
。 -
port
: Specify the port number of the development server, the default value is8080
. You can set it to another port number as needed. For example,port: 3000
。 -
open
: A boolean value that specifies whether to automatically open the project in the browser after starting the development server. The default value isfalse
. If set totrue
, the project will automatically open in the browser after it is started. For example,open: true
。 -
proxy
: Options for configuring the development server proxy to proxy a specific request to another address. This is very useful for solving cross-domain problems during development. Multiple proxy rules can be configured. A common usage is to proxy the development server to the backend API server. For example:
= { devServer: { proxy: { '/api': { target: '', changeOrigin: true, pathRewrite: { '^/api': '', }, }, }, }, }; ``` In the above example,`/api` The request at the beginning will be proxied to `` address,And pass `changeOrigin: true` Enable cross-domain request。`pathRewrite` Options are used to rewrite the request path。
These options can be configured according to your project requirements to meet specific development environment requirements. You can use it as neededdevServer
Make corresponding settings in the field.
Extensions:
Detailed explanation of common configurations
Abstract: This article will introduce commonly used configuration options, including publicPath, outputDir, devServer, chainWebpack, etc., and provide corresponding code examples to help readers better understand and configure Vue projects.
## 1. publicPath
The publicPath option is used to configure the base path of the project. By default, the base path to a Vue project is / , i.e. the root directory. You can configure it according to actual needs. For example, when deploying a project to a subdirectory, you can set publicPath as the path to the subdirectory.
// = { publicPath: .NODE_ENV === 'production' ? '/sub-directory/' : '/' }
In the above example, we dynamically set publicPath based on the value of the environment variable NODE_ENV . In production environment, set publicPath to /sub-directory/ , and in development environment, set publicPath to / .
## 2. outputDir
The outputDir option is used to configure the directory for packaged output, and the default is dist . You can configure it according to actual needs, for example, put the packaged output files in a specified directory.
// = { outputDir: 'build' }
In the above example, we place the packaged output file in the build directory.
## 3. devServer
The devServer option is used to configure the development server. You can configure according to actual needs, such as setting up a proxy, changing the default port, etc.
// = { devServer: { port: 8080, proxy: { '/api': { target: 'http://localhost:3000', pathRewrite: { '^/api': '' } } } } }
In the above example, we set the port of the development server to 8080 and configured a proxy to proxy requests starting with /api to http://localhost:3000 .
## 4. chainWebpack
The chainWebpack option is used to make finer granular modifications to the internal webpack configuration via [webpack-chain] (/neutrinojs/webpack-chain). You can configure according to actual needs, such as adding custom loaders, plug-ins, etc.
// = { chainWebpack: config => { .rule('svg') .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]' }) } }
In the above example, we added a loader for the .svg file through chainWebpack configuration and set the symbolId option.
Through the introduction of this article, we have learned about the commonly used configuration options and provide corresponding code examples. Hopefully these examples can help readers better understand and configure Vue projects, improve development efficiency and code quality.
This is the article about the common options of the file devServer field. For more relevant common options, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!