正文
require.context() 用于获取一个特定上下文的,webpack的一个api
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
参考链接:
1、https://www.jianshu.com/p/c894ea00dfec
2、https://www.jianshu.com/p/c894ea00dfec
require.context()
1、可以使用require.context()函数创建自己的上下文。它允许您传入一个,目录进行搜索,一个标志指示是否应该搜索子目录,还有一个正则表达式来匹配文件。
例:查找当前svg目录下的,以.svg结尾的文件,且可以对子目录进行搜索
const svgFiles = require.context('./svg', true, /\.svg$/);
2、require.context()函数执行后返回的是一个函数,并且这个函数有3个属性:resolve 、keys、id
① resolve {Fuction} 接收一个参数request,request为 svg 文件夹下面匹配文件的相对路径,返回这个匹配文件对于整个工程的相对路径;
② keys {Function} 返回匹配成功模块的名字组成的数组,例:如下:
const iconList = svgFiles.keys().map(item => svgFiles(item));
③ id {String} 执行环境的id,返回的是一个字符串