requireAllModule【自动化注册模块】
描述
背景:
1、webpack 工程化 我们会用 require.context 方法去自动注册或加载相关的文件,但是 webpack 官方给出方案 要么只获取一层文件内容,要么获取文件下及子文件所有的文件信息数据,
但有时需要我们要 排除某个文件或者只导入某几个文件,
2、vite 工程化 我们会用 import.meta.globEager 方法去自动注册或加载相关的文件,但是 vite 官方给出方案 要么只获取一层文件内容,要么获取文件下及子文件所有的文件信息数据,
但有时需要我们要 排除某个文件或者只导入某几个文件,
所以:摆在前端开发者两条路,1、调整正则表达式 2、获取到的文件 前端再做一层筛选。
自动化注册模块,🎉 支持排除。exclude 🎉 支持只包含 include。 返回值是由 require.context 读到文件内容 拼成的数组
# 1.示例 1
# webpack 目录结构
import { requireAllModule } from 'sf-utils2'
// 假设文件目录在
// 第一个打印
requireAllModule({ requireContext: require.context('./modules/', true, /\.js$/) })
// 第二个打印
requireAllModule({
requireContext: require.context('./modules/', true, /\.js$/),
exclude: ['./dayjs.js', './element-ui.js']
})
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# webpack 输出结果
第一个打印截图
第二个打印截图

⚠️ 警告
include参数 和 exclude参数 不能同时共用
# 2.示例 2
# vite 目录结构
import { requireAllModule } from 'sf-utils2'
// 假设文件目录在
// 第一个打印
requireAllModule({ requireContext: import.meta.globEager('./modules/**/*.js'), type: 'vite' })
// 第二个打印
requireAllModule({
requireContext: import.meta.globEager('./modules/**/*.js'),
exclude: ['./modules/dayjs.js', './modules/echarts.js', './modules/fastclick.js'],
type: 'vite'
})
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# vite 输出结果
第一个打印截图
第二个打印截图

⚠️ 警告
include参数 和 exclude参数 不能同时共用
# 3.入参说明
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| requireContext | 当 type 为 webpack 是,语法 require.context(目录,是否递归目录, 文件筛选条件)所有参数必须是直接字符串,变量无效;当 type=vite 时,语 | any | - | - |
| exclude | 排除的模块 | Array<String> | - | - |
| include | 只引入包括的模块 | Array<String> | - | - |
| type | 自动注入类型 可选值 webpack 和 vite,默认值是 webpack | string | - | webpack |
# 4.源码
import isArray from '@/base/isArray'
/**
* 自动化注册 需要模块
* @param requireContext 当type为webpack是,语法require.context(目录,是否递归目录, 文件筛选条件)所有参数必须是直接字符串,变量无效;当type=vite时,语法import.meta.globEager所有参数必须是直接字符串,变量无效。
* @param {Array} exclude 只引入包括的模块
* @param {Array} include 排除的模块 include 和 exclude不能同时共用
* @param {string} type 可选值 webpack 和 vite,默认值是webpack
* @returns {*} Array
*/
function requireAllModule({ requireContext, exclude = [], include = [], type = 'webpack' }) {
if (!isArray(exclude)) {
console.error('[warning] exclude字段类型应为Array')
exclude = []
}
if (!isArray(include)) {
console.error('[warning] include字段类型应为Array')
include = []
}
const isNoEmptyArray = function () {
return !!(Array.isArray(this) && this.length)
}
const isEmptyArray = function () {
return !!(Array.isArray(this) && this.length === 0)
}
if (isNoEmptyArray.apply(exclude) && isNoEmptyArray.apply(include)) {
throw new Error('include 和 exclude字段不能同时共用')
}
const requireAll = context => {
if (type == 'webpack') {
// 是webpack
return context.keys().reduce((pre, cur) => {
const _key = cur // cur.replace(/^\.\/(.*)\.\w+$/, '$1')
if (isNoEmptyArray.apply(include)) {
include.some(v => new RegExp(v).test(_key)) && pre.push([context(cur), _key])
} else if (isNoEmptyArray.apply(exclude)) {
exclude.every(v => v !== _key) && pre.push([context(cur), _key])
} else if (isEmptyArray.apply(include) && isEmptyArray.apply(exclude)) {
pre.push([context(cur), _key])
}
return pre
}, [])
} else if (type == 'vite') {
// 是vite
return Object.keys(context).reduce((pre, cur) => {
const _key = cur // cur.replace(/^\.\/(.*)\.\w+$/, '$1')
if (isNoEmptyArray.apply(include)) {
include.some(v => new RegExp(v).test(_key)) && pre.push([context(cur), _key])
} else if (isNoEmptyArray.apply(exclude)) {
exclude.every(v => v !== _key) && pre.push([context(cur), _key])
} else if (isEmptyArray.apply(include) && isEmptyArray.apply(exclude)) {
pre.push([context(cur), _key])
}
return pre
}, [])
}
}
return requireAll(requireContext)
}
export default requireAllModule
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
上次更新: 2024/01/16, 00:19:48