-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (49 loc) · 1.24 KB
/
index.js
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
import FastGlob from "fast-glob";
import { resolve } from "node:path";
const PLUGIN_NAME = "glob-import-esbuild-plugin";
const REGEX_MATCHING_BLOB_PATTERN = /(\*\*?)/;
export default {
name: PLUGIN_NAME,
setup(build) {
build.onResolve(
{ filter: REGEX_MATCHING_BLOB_PATTERN },
({ resolveDir, path }) => {
return {
path,
namespace: PLUGIN_NAME,
pluginData: {
resolveDir,
},
};
}
);
build.onLoad(
{ filter: /.*/, namespace: PLUGIN_NAME },
async ({ path, pluginData: { resolveDir } }) => {
const files = (
await FastGlob(path, {
cwd: resolveDir,
})
).sort();
const contents = [
files
.map(
(module, index) => `import * as module${index} from '${module}'`
)
.join(";\n"),
`const modules = Object.fromEntries([${files
.map(
(file, index) =>
`['${resolve(resolveDir, file)}', module${index}]`
)
.join(",")}]);`,
"export default modules;",
].join("\n");
return {
contents,
resolveDir,
};
}
);
},
};