webpack

作者: jiahzhon | 来源:发表于2020-08-12 18:05 被阅读0次

require.context

  • 在我们项目开发中,经常需要import或者export各种模块,那么有没有什么办法可以简化这种引入或者导出操作呢。

  • 我们会这样引入组件:

import A from 'components/A'
import B from 'components/B'
import C from 'components/C'
import D from 'components/D'
// ..
  • require.context(directory, useSubdirectories, regExp)

    • directory: 要查找的文件路径
    • useSubdirectories: 是否查找子目录
    • regExp: 要匹配文件的正则
  • 返回值

    • webpackContext
      • keys: 返回匹配成功模块的名字组成的数组
      • resolve: 接受一个参数request,request为test文件夹下面匹配文件的相对路径,返回这个匹配文件相对于整个工程的相对路径
      • id: 执行环境的id,返回的是一个字符串,主要用在module.hot.accept,应该是热加

相关文章

网友评论

      本文标题:webpack

      本文链接:https://www.haomeiwen.com/subject/mxymdktx.html