vue.config.js这个文件在创建完vue脚手架工程之后,需要自己手动创建,存放的是一些配置项,遵循json 书写规则。
这里,粘贴一个自己写好的配置项。
注意:配置项中内容不能为空,我的文件中pluginOptions第三方插件配置 已经 注释。
一定要注意,否则会报错"ValidationError: webpack Dev Server Invalid Options options should NOT have additional properties"
const goods = require("./data/goods.json");
const rating = require("./data/rating.json");
const seller = require("./data/seller.json");
module.exports = {
publicPath: '/', //根路径
outputDir: 'dist', //构建输出目录
assetsDir: 'assets', //静态资源目录 js css img fonts
lintOnSave: false, //是否开启eslint保存检测,有效值:ture || false || error
devServer: {
open: true, //npm run serve 自动弹开浏览器
host: 'localhost',
port: '8080',
https: false,
hotOnly: false, // 热更新
/* proxy: {
//配置跨域
'/api': {
target: 'http//localhost:5000/api/', //api服务器的地址
ws: true, // 代理websockets
changOrigin: true, // 虚拟的站点需要更管origin
pathRewrite: { // 重写路径
'^/api': ''
}
}
}, */
before: app => { //设置代理
// http://localhost:8080/api/goods
app.get('/api/goods', (req, res) => {
res.json(goods);
});
app.get('/api/rating', (req, res) => {
res.json(rating);
});
app.get('/api/seller', (req, res) => {
res.json(seller);
});
},
/* pluginOptions: {
//... 第三方插件配置
} */
}
}
标准版的
// vue.config.js
const path = require('path');
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
const resolve = (dir) => path.join(__dirname, dir);
module.exports = {
publicPath: process.env.NODE_ENV === 'production' ? '/site/vue-demo/' : '/', // 公共路径
indexPath: 'index.html' , // 相对于打包路径index.html的路径
outputDir: process.env.outputDir || 'dist', // 'dist', 生产环境构建文件的目录
assetsDir: 'static', // 相对于outputDir的静态资源(js、css、img、fonts)目录
lintOnSave: false, // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码
runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本
productionSourceMap: !IS_PROD, // 生产环境的 source map
parallel: require("os").cpus().length > 1, // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用,仅作用于生产构建。
pwa: {}, // 向 PWA 插件传递选项。
chainWebpack: config => {
config.resolve.symlinks(true); // 修复热更新失效
// 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中
config.plugin("html").tap(args => {
// 修复 Lazy loading routes Error
args[0].chunksSortMode = "none";
return args;
});
config.resolve.alias // 添加别名
.set('@', resolve('src'))
.set('@assets', resolve('src/assets'))
.set('@components', resolve('src/components'))
.set('@views', resolve('src/views'))
.set('@store', resolve('src/store'));
},
css: {
extract: IS_PROD,
requireModuleExtension: false,// 去掉文件名中的 .module
loaderOptions: {
// 给 less-loader 传递 Less.js 相关选项
less: {
// `globalVars` 定义全局对象,可加入全局变量
globalVars: {
primary: '#333'
}
}
}
},
devServer: {
overlay: { // 让浏览器 overlay 同时显示警告和错误
warnings: true,
errors: true
},
host: "localhost",
port: 8080, // 端口号
https: false, // https:{type:Boolean}
open: false, //配置自动启动浏览器
hotOnly: true, // 热更新
// proxy: 'http://localhost:8080' // 配置跨域处理,只有一个代理
proxy: { //配置多个跨域
"/api": {
target: "http://172.11.11.11:7071",
changeOrigin: true,
// ws: true,//websocket支持
secure: false,
pathRewrite: {
"^/api": "/"
}
},
"/api2": {
target: "http://172.12.12.12:2018",
changeOrigin: true,
//ws: true,//websocket支持
secure: false,
pathRewrite: {
"^/api2": "/"
}
},
}
}
}