-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite-plugin.js
39 lines (35 loc) · 1005 Bytes
/
vite-plugin.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
const fs = require("fs");
const { getSourceWithSourceCodeLocation } = require("./dist/index.js");
const htmlTags = require("html-tags");
module.exports = function vitePlugin() {
return {
name: "vite-plugin-click-to-component",
enforce: "pre",
load(id) {
if (
process.env.VUE_CLICK_TO_COMPONENT_FORCE_ENABLE !== "true" &&
process.env.NODE_ENV !== "development"
) {
// disable vite plugin
return;
}
if (id.endsWith(".vue")) {
try {
const source = fs.readFileSync(id, "utf-8");
const sourceWithSourceCodeLocation = getSourceWithSourceCodeLocation({
source,
filePath: id,
htmlTags,
});
return sourceWithSourceCodeLocation;
} catch (error) {
console.error("[vue-click-to-component-vite-plugin] error", {
file: id,
error: error && error.message,
});
return;
}
}
},
};
};