'use strict'
//引入nodejs路径模块
const path = require('path')
//引入utils工具模块,utile主要用来处理css-loader和vue-style-loader
const utils = require('./utils')
//引入config/index.js配置文件,定义开发和生产环境的属性
const config = require('../config')
//vue-loader.conf配置文件,主要是配置vue-loader的options,处理vue文件中的css和img、video、source
const vueLoaderConfig = require('./vue-loader.conf')
//返回当前目录的平行目录下的dir文件夹
function resolve (dir) {
return path.join(__dirname, '..', dir)
}
module.exports = {
/*基础目录,绝对路径,用于从配置中解析入口起点(entry point)和 loader,默认是当前目录
* path.resolve 将路径或路径片段的序列解析为绝对路径
* */
context: path.resolve(__dirname, '../'),
// 应用程序的起点入口
entry: {
app: './src/main.js'
},
//输出
output: {
//所有输出文件的目标路径;打包后文件在硬盘中的存储位置
path: config.build.assetsRoot,
// 每个输出bundle的名称
filename: '[name].js',
//输出解析文件的目录,指定资源文件引用的目录,打包后浏览器访问服务时的url路径中通用的部分
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
//自动解析确定的扩展,能够让用户在引入模块时不带扩展
extensions: ['.js', '.vue', '.json'],
/*
* 创建import或require的别名,来使引入模块变得简单
* 可以在给定对象的键后的末尾添加$,以表示精准匹配
* */
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
}
},
module: {
rules: [
//解析编译vue文件
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
//用于在旧的浏览器或环境中将ES6+代码转换为向后兼容版本的JavaScript代码
{
test: /\.js$/,
loader: 'babel-loader',
//指点src目录,test目录和client目录使用babel-loader模块
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
},
{
//对图片使用url-loader,文件大小小于限制的转换为base64,大于限制的放在static/img下,方便cache
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
//对音视频文件使用url-loader,文件大小小于限制的转换为base64,大于限制的放在static/media下,方便cache
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
//对字体文件使用url-loader,文件大小小于限制的转换为base64,大于限制的放在static/media下,方便cache
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
node: {
//用来配置是否填充或模拟某些nodejs全局变量和模块,每个属性都有以下几个值
/*
* true:提供填充
* mock:提供模拟实现预期接口,但是功能很少或没有
* empty:提供空对象
* false:什么都不提供。导入对应模块的代码会触发'Cannot find module modulename'错误
* */
// prevent webpack from injecting useless setImmediate polyfill because Vue
// source contains it (although only uses it if it's native).
setImmediate: false,
// prevent webpack from injecting mocks to Node native modules
// that does not make sense for the client
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty'
}
}
vue-cli webpack.base.conf.js学习
最新推荐文章于 2023-07-24 11:20:07 发布