vue中config使用
# 在 Vue 2 中,vue.config.js
是一个用于配置 Vue CLI 项目的文件。
# vue.config.js
的基本结构
module.exports = {
// 配置选项
};
1
2
3
2
3
# 主要配置选项
publicPath
配置应用的基本 URL 路径。通常用于设置静态资源的加载路径。module.exports = { publicPath: process.env.NODE_ENV === 'production' ? '/my-app/' : '/', };
1
2
3outputDir
指定生产环境构建输出目录,默认为dist
。module.exports = { outputDir: 'dist/my-app', };
1
2
3assetsDir
指定静态资源(js、css、img、fonts)的目录,默认为''
。module.exports = { assetsDir: 'assets', };
1
2
3indexPath
指定index.html
的输出路径,默认为index.html
。module.exports = { indexPath: 'my-index.html', };
1
2
3devServer
配置开发环境下的服务器选项。module.exports = { devServer: { port: 8080, open: true, // 启动后自动打开浏览器 proxy: { '/api': { target: 'http://localhost:3000', changeOrigin: true, pathRewrite: { '^/api': '' }, }, }, }, };
1
2
3
4
5
6
7
8
9
10
11
12
13configureWebpack
用于直接修改 Webpack 配置。module.exports = { configureWebpack: { plugins: [ // 添加 Webpack 插件 ], module: { rules: [ // 自定义加载器规则 ], }, }, };
1
2
3
4
5
6
7
8
9
10
11
12chainWebpack
使用 webpack-chain (opens new window) 进行更细粒度的配置。module.exports = { chainWebpack: config => { config.plugin('html').tap(args => { args[0].title = 'My App Title'; return args; }); }, };
1
2
3
4
5
6
7
8lintOnSave
控制是否在保存时运行 eslint 检查。module.exports = { lintOnSave: true, // 或 false };
1
2
3transpileDependencies
指定需要转译的依赖包,可以是一个数组。module.exports = { transpileDependencies: ['some-package'], };
1
2
3productionSourceMap
控制是否为生产构建生成 source map。module.exports = { productionSourceMap: false, };
1
2
3
# 示例:vue.config.js
const path = require('path');
module.exports = {
// 设定应用的基本 URL 路径
publicPath: process.env.NODE_ENV === 'production' ? '/my-app/' : '/',
// 指定生产环境构建输出目录
outputDir: path.resolve(__dirname, 'dist/my-app'),
// 指定静态资源(js、css、img、fonts)的目录
assetsDir: 'assets',
// 指定 index.html 的输出路径
indexPath: 'my-index.html',
// 设置开发环境的服务器选项
devServer: {
port: 8080, // 服务器端口
open: true, // 启动后自动打开浏览器
proxy: {
'/api': {
target: 'http://localhost:3000', // 目标 API 服务器
changeOrigin: true, // 支持虚拟托管
pathRewrite: { '^/api': '' }, // 重写路径
},
},
},
// 直接修改 Webpack 配置
configureWebpack: {
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'), // 设置别名
},
},
plugins: [
// 可以添加自定义的 Webpack 插件
],
},
// 使用 webpack-chain 进行更细粒度的配置
chainWebpack: config => {
// 修改 HTML webpack 插件的标题
config.plugin('html').tap(args => {
args[0].title = 'My Vue App Title';
return args;
});
// 添加加载器规则
config.module
.rule('svg')
.use('vue-svg-loader')
.loader('vue-svg-loader');
},
// 控制是否在保存时运行 eslint 检查
lintOnSave: true, // 或 false
// 指定需要转译的依赖
transpileDependencies: ['some-package'],
// 控制是否为生产构建生成 source map
productionSourceMap: false,
};
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
55
56
57
58
59
60
61
62
63
64
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
55
56
57
58
59
60
61
62
63
64
# 解释
- publicPath: 设置应用的基本 URL 路径。根据环境变量的不同,设置生产环境和开发环境的路径。
- outputDir: 指定生产环境构建输出目录为
dist/my-app
。 - assetsDir: 指定静态资源的目录为
assets
。 - indexPath: 设置输出的
index.html
文件名为my-index.html
。 - devServer: 配置开发服务器的端口为 8080,并启用自动打开浏览器功能。设置了一个代理,将
/api
请求转发到http://localhost:3000
。 - configureWebpack: 添加了别名配置,方便在代码中使用
@
代表src
目录。 - chainWebpack: 修改了 HTML 插件的标题,并添加了一个加载器规则用于处理 SVG 文件。
- lintOnSave: 启用保存时运行 ESLint 检查。
- transpileDependencies: 指定
some-package
作为需要转译的依赖。 - productionSourceMap: 禁用生产环境的 source map 生成。
# 使用示例
开发环境: 当您运行
npm run serve
时,开发服务器将在http://localhost:8080
启动,并且会自动打开浏览器。API 请求: 当您在应用中发起
/api
请求时,它会被代理到http://localhost:3000
,比如您可以通过 Axios 发送请求:axios.get('/api/data') .then(response => { console.log(response.data); });
1
2
3
4构建生产环境: 当您运行
npm run build
时,应用会被构建到您指定的输出目录dist/my-app
。
!
在线编辑 (opens new window)
上次更新: 2025/02/25, 18:30:54