forked from different-ai/obsidian-ava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirname.mjs
27 lines (23 loc) · 804 Bytes
/
dirname.mjs
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
import fs from "fs";
import path from "path";
const nodeModules = new RegExp(/^(?:.*[\\\/])?node_modules(?:[\\\/].*)?$/);
const dirnamePlugin = {
name: "dirname",
setup(build) {
build.onLoad({ filter: /.*/ }, ({ path: filePath }) => {
if (!filePath.match(nodeModules)) {
let contents = fs.readFileSync(filePath, "utf8");
const loader = path.extname(filePath).substring(1);
const dirname = path.dirname(filePath);
contents = contents
.replace("__dirname", ".")
.replace("__filename", `"${filePath}"`);
return {
contents,
loader,
};
}
});
},
};
export default dirnamePlugin;