vue.config.js配置文件详解

vue.config.js配置文件

完整项目实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin')
const env = process.env.NODE_ENV;

module.exports = {
baseUrl: './',
outputDir: 'user-behavior-analysis',
devServer: {
open: true, //设置自动打开
port: 8080, //设置8080端口打开
https: false,
hotOnly: true,
disableHostCheck: true,
open: true,
proxy: {
'user-analysis-cms/': {
target: 'http://test.xx-motor.com/',
changeOrigin: true,
pathRewrite: {
'user-analysis-cms/': 'user-analysis-cms/',
},
cookieDomainRewrite: {
'localhost:8080': 'http://test.xx-motor.com/',
},
}
before: (app) => {},
},
configureWebpack: {
optimization: {
minimizer: [
]
},
},
chainWebpack: (config) => {
// 修复HMR
config.resolve.symlinks(true);
},
};

devServer配置

这里写你调用接口的基础路径来解决跨域,如果设置了代理,那你本地开发环境的axios的baseURL要写成空字符串。

只有一个接口ip端口时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module.exports = {
// 部署生产环境和开发环境下的URL。
// 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上
//例如 https://www.my-app.com/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.my-app.com/my-app/,则设置 baseUrl 为 /my-app/。
//baseUrl 从 Vue CLI 3.3 起已弃用,请使用publicPath
//baseUrl: process.env.NODE_ENV === "production" ? "./" : "/",
publicPath: process.env.NODE_ENV === "production" ? "./" : "/",

// outputDir: 在npm run build 或 yarn build 时 ,生成文件的目录名称(要和baseUrl的生产环境路径一致)
outputDir: "mycli3",
//用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
assetsDir: "assets",
//指定生成的 index.html 的输出路径 (打包之后,改变系统默认的index.html的文件名)
// indexPath: "myIndex.html",
//默认情况下,生成的静态资源在它们的文件名中包含了 hash 以便更好的控制缓存。你可以通过将这个选项设为 false 来关闭文件名哈希。(false的时候就是让原来的文件名不改变)
filenameHashing: false,

// lintOnSave:{ type:Boolean default:true } 问你是否使用eslint
lintOnSave: true,
//如果你想要在生产构建时禁用 eslint-loader,你可以用如下配置
// lintOnSave: process.env.NODE_ENV !== 'production',

//是否使用包含运行时编译器的 Vue 构建版本。设置为 true 后你就可以在 Vue 组件中使用 template 选项了,但是这会让你的应用额外增加 10kb 左右。(默认false)
// runtimeCompiler: false,

/**
* 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
* 打包之后发现map文件过大,项目文件体积很大,设置为false就可以不输出map文件
* map文件的作用在于:项目打包后,代码都是经过压缩加密的,如果运行时报错,输出的错误信息无法准确得知是哪里的代码报错。
* 有了map就可以像未加密的代码一样,准确的输出是哪一行哪一列有错。
* */
productionSourceMap: false,

// 它支持webPack-dev-server的所有选项
devServer: {
host: "0.0.0.0",
port: 8080, // 端口号
https: false, // https:{type:Boolean}
open: true, //配置自动启动浏览器
// proxy: 'http://localhost:4000' // 配置跨域处理,只有一个代理

// 配置多个代理
proxy: {
"/api": {
target: "http://192.168.x.xxx:8090", // 要访问的接口域名
ws: true, // 是否启用websockets
changeOrigin: true, //开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题
pathRewrite: {
"^/api": "" //这里理解成用'/api'代替target里面的地址,比如我要调用'http://40.00.100.100:3002/user/add',直接写'/api/user/add'即可
}
}
}
}
};

解决跨域原理

1
2
3
4
5
6
7
8
9
10
11
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true,
}
}
}
};

changeOrigin默认为false,请求头中的host仍然是浏览器发送过来的host 。如果设置为true,请求头中的host会设置成target,且本地会虚拟一个服务器接受你的请求并代你发送该请求。

outputDir构建输出目录

当运行vue-cli-server build时生成的生产环境构建文件的目录

1
2
3
4
module.exports = {
outputDir: 'dist'
}
/*输出到dist目录中*/

错误案例

1
2
3
4
5
module.exports = {
publicPath: './',
outputDir: 'aaa'
}
/* 这样设置后打包生成的的文件夹依然是dist*/

注意点:

  1. vue.config.js文件必须放在与package.json同级。
  2. publicPath: ./是针对hash路径的,/是history的。

单路径代理

1
2
3
4
5
6
7
8
module.exports = {
//...
devServer: {
proxy: {
'/api': 'http://localhost:3000'
}
}
};

请求到 /api/xxx 现在会被代理到请求 http://localhost:3000/api/xxx, 例如 /api/user 现在会被代理到请求 http://localhost:3000/api/user

多路径代理

如果你想要代码多个路径代理到同一个target下, 你可以使用由一个或多个「具有 context 属性的对象」构成的数组:

1
2
3
4
5
6
7
8
9
module.exports = {
//...
devServer: {
proxy: [{
context: ['/auth', '/api'],
target: 'http://localhost:3000',
}]
}
};

路径重写

如果你不想始终传递 /api ,则需要重写路径:

1
2
3
4
5
6
7
8
9
10
11
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
pathRewrite: {'^/api' : ''}
}
}
}
};

请求到 /api/xxx 现在会被代理到请求 http://localhost:3000/xxx, 例如 /api/user 现在会被代理到请求 http://localhost:3000/user

使用无效证书的服务器

默认情况下,不接受运行在 HTTPS 上,且使用了无效证书的后端服务器。如果你想要接受,只要设置 secure: false 就行。修改配置如下:

1
2
3
4
5
6
7
8
9
10
11
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'https://other-server.example.com',
secure: false
}
}
}
};

跳过代理

有时你不想代理所有的请求。可以基于一个函数的返回值绕过代理。
在函数中你可以访问请求体、响应体和代理选项。必须返回 false 或路径,来跳过代理请求。

例如:对于浏览器请求,你想要提供一个 HTML 页面,但是对于 API 请求则保持代理。你可以这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
bypass: function(req, res, proxyOptions) {
if (req.headers.accept.indexOf('html') !== -1) {
console.log('Skipping proxy for browser request.');
return '/index.html';
}
}
}
}
}
};

vue-cli中proxyTable配置接口地址代理实例

修改config/index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module.exports = {
dev: {
// 静态资源文件夹
assetsSubDirectory: 'static',
// 发布路径
assetsPublicPath: '/',

// 代理配置表,在这里可以配置特定的请求代理到对应的API接口
// 使用方法:https://vuejs-templates.github.io/webpack/proxy.html
proxyTable: {
// 例如将'localhost:8080/api/xxx'代理到'https://wangyaxing.cn/api/xxx'
'/api': {
target: 'https://wangyaxing.cn', // 接口的域名
secure: false, // 如果是https接口,需要配置这个参数
changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
},
// 例如将'localhost:8080/img/xxx'代理到'https://cdn.wangyaxing.cn/xxx'
'/img': {
target: 'https://cdn.wangyaxing.cn', // 接口的域名
secure: false, // 如果是https接口,需要配置这个参数
changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
pathRewrite: {'^/img': ''} // pathRewrite 来重写地址,将前缀 '/api' 转为 '/'。
}
},
// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
port: 4200, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
}
文章目录
  1. 1. vue.config.js配置文件
    1. 1.0.1. devServer配置
    2. 1.0.2. 解决跨域原理
    3. 1.0.3. outputDir构建输出目录
    4. 1.0.4. 单路径代理
    5. 1.0.5. 多路径代理
    6. 1.0.6. 路径重写
    7. 1.0.7. 使用无效证书的服务器
    8. 1.0.8. 跳过代理
  • 2. vue-cli中proxyTable配置接口地址代理实例
  • |