vue.config.js基本配置

该文章详细展示了VueCLI项目中关于模块导出、基础路径、输出目录、静态资源路径、HTML输出、文件命名哈希、多页配置、ESLint检查、运行时编译器、源码映射、webpack配置调整、devServer代理设置等关键配置项。

module.exports = {

// 选项
// 基本路径
publicPath: “./”,
// 构建时的输出目录
outputDir: “dist”,
// 放置静态资源的目录
assetsDir: “static”,
// html 的输出路径
indexPath: “index.html”,
//文件名哈希
filenameHashing: true,
//用于多页配置,默认是 undefined
pages: {
index: {
// page 的入口文件
entry: ‘src/index/main.js’,
// 模板文件
template: ‘public/index.html’,
// 在 dist/index.html 的输出文件
filename: ‘index.html’,
// 当使用页面 title 选项时,
// template 中的 title 标签需要是 <%= htmlWebpackPlugin.options.title %>
title: ‘Index Page’,
// 在这个页面中包含的块,默认情况下会包含
// 提取出来的通用 chunk 和 vendor chunk。
chunks: [‘chunk-vendors’, ‘chunk-common’, ‘index’]
},
// 当使用只有入口的字符串格式时,
// 模板文件默认是 public/subpage.html
// 如果不存在,就回退到 public/index.html
// 输出文件默认是 subpage.html
subpage: ‘src/subpage/main.js’
},
// 是否在保存的时候使用 eslint-loader 进行检查。

lintOnSave: true,

// 是否使用带有浏览器内编译器的完整构建版本

runtimeCompiler: false,

// babel-loader 默认会跳过 node_modules 依赖。

transpileDependencies: [ /* string or regex */ ],

// 是否为生产环境构建生成 source map?

productionSourceMap: true,

// 调整内部的 webpack 配置

configureWebpack: () => {}, //(Object | Function)

chainWebpack: () => {},

// 配置 webpack-dev-server 行为。

devServer: {
open: process.platform === ‘darwin’,
host: ‘0.0.0.0’,
port: 8080,
https: false,
hotOnly: false,
// 查阅 https://github.com/vuejs/vue-docs-zh-cn/blob/master/vue-cli/cli-service.md#配置代理
proxy: {
‘/api’: {
target: “http://app.rmsdmedia.com”,
changeOrigin: true,
secure: false,
pathRewrite: {
“^/api”: “”//效果实际是http://192.168.38.73:8080/ebus/tyj/highRiskSports/getAreaFitnessVenues
“^/api”: “api”//效果实际是http://192.168.38.73:8080/api/ebus/tyj/highRiskSports/getAreaFitnessVenues
使用起来看实际接口需求
},
},
‘/foo’: {
target: ‘<other_url>’
}
}, // string | Object
before: app => {}
},
// CSS 相关选项
css: {
// 将组件内的 CSS 提取到一个单独的 CSS 文件 (只用在生产环境中)
// 也可以是一个传递给 extract-text-webpack-plugin 的选项对象
extract: true,
// 是否开启 CSS source map?
sourceMap: false,
// 为预处理器的 loader 传递自定义选项。比如传递给
// Css-loader 时,使用 { Css: { ... } }
loaderOptions: {
css: {
// 这里的选项会传递给 css-loader
},
postcss: {
// 这里的选项会传递给 postcss-loader
}
},
// 为所有的 CSS 及其预处理文件开启 CSS Modules。
// 这个选项不会影响 *.vue 文件。
modules: false
},

// 在生产环境下为 Babel 和 TypeScript 使用 thread-loader

// 在多核机器下会默认开启。

parallel: require(‘os’).cpus().length > 1,

// PWA 插件的选项。

// 查阅 https://github.com/vuejs/vue-docs-zh-cn/blob/master/vue-cli-plugin-pwa/README.md

pwa: {},

// 三方插件的选项

pluginOptions: {

// ...

}

}

vue.config.js是一个可选的配置文件,若项目根目录存在该文件,会被@vue/cli-service自动加载,也可使用package.json中的vue字段,但需严格遵照JSON格式书写[^1]。以下是vue2中配置vue.config.js的方法和常见配置项说明: ### 常见配置项及用途 - **publicPath**:设置部署应用时的基础路径,例如 `/` 或 `/my-app/` [^2]。 - **outputDir**:构建输出目录,默认是 `dist` [^2]。 - **assetsDir**:放置静态资源(图片、字体、JS/CSS)的子目录 [^2]。 - **lintOnSave**:是否在保存时使用ESLint检查代码 [^2]。 - **devServer**:配置开发服务器,可设置端口、代理、HTTPS、自动打开浏览器等 [^2]。 - **configureWebpack / chainWebpack**:用于修改Webpack配置,如添加插件、loader等 [^2]。 - **productionSourceMap**:控制是否生成生产环境的source map文件 [^2]。 - **transpileDependencies**:设置需要通过Babel转译的依赖包 [^2]。 ### 配置示例 #### 配置src别名为@ ```javascript // vue.config.js const { resolve } = require('path'); module.exports = { chainWebpack: config => { const types = ['vue-modules', 'vue', 'normal-modules', 'normal']; config.resolve.alias.set('@', resolve('src')); } }; ``` 此代码通过 `chainWebpack` 配置项修改Webpack配置,将 `src` 目录别名为 `@`,方便在代码中引用 `src` 下的文件 [^3]。 #### 关闭生产环境的source map ```javascript // vue.config.js module.exports = { productionSourceMap: false }; ``` 该配置用于控制是否生成生产环境的源代码映射文件,关闭可减少打包后的文件体积 [^4]。 #### 去掉eslint检查 ```javascript // vue.config.js module.exports = { lintOnSave: false }; ``` `lintOnSave` 配置项可设置 `eslint-loader` 是否在保存时检查代码,将其设为 `false` 可去掉eslint检查 [^4]。 #### 配置开发环境的Source Map类型 ```javascript // vue.config.js configureWebpack: config => { if (process.env.NODE_ENV === 'production') { // 为生产环境修改配置... config.mode = 'production'; config.devtool = "source-map"; } else { // 为开发环境修改配置... config.mode = 'development'; } }; ``` `config.devtool` 是Webpack的选项,设置为 `"source-map"` 会生成独立的Source Map文件,用于将编译后的代码映射回原始源代码,在开发环境中开启有助于提高调试效率,但会增加构建时间和打包后文件的体积 [^4]。 #### 多页面配置 ```javascript // vue.config.js module.exports = { pages: { index: { // page 的入口 entry: "src/index/main.js", // 模板来源 template: "public/index.html", // 在 dist/index.html 的输出 filename: "index.html", // 当使用 title 选项时, // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title> title: "Index Page", // 在这个页面中包含的块,默认情况下会包含 // 提取出来的通用 chunk 和 vendor chunk。 chunks: ["chunk-vendors", "chunk-common", "index"], }, // 当使用只有入口的字符串格式时, // 模板会被推导为 `public/subpage.html` // 并且如果找不到的话,就回退到 `public/index.html`。 // 输出文件名会被推导为 `subpage.html`。 subpage: "src/subpage/main.js", }, }; ``` `pages` 配置项用于多页面应用,可指定每个页面的入口、模板、输出文件名等 [^5]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奇怪的农民

你打赏的是无话不说

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值