-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (42 loc) · 1.69 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
import fs from "fs";
import jsesc from "jsesc";
/**
* Will escape the single file HTML based on an optional config -- for config see also jsecs docs https://www.npmjs.com/package/jsesc#api
* @param {string} wrapBefore
* @param {string} wrapAfter
* @param {{numbers: "binary" | "octal" | "decimal" | "hexadecimal", quotes: "double" | "backstick" | "single", wrap: boolean, es6: boolean, escapeEverything: boolean, minimal: boolean, isScriptContext: boolean, compact: boolean, indent: string, indentLevel: number, json: boolean, lowercaseHex: boolean}} config
* @returns () => void
*/
export default function viteSingleFileEscaped(
wrapBefore,
wrapAfter,
jsescConfigObj
) {
return {
name: " vite-plugin-singlefile-escaped",
writeBundle(_, bundle) {
// Assuming there's only one output file from vite-plugin-singlefile
const fileName = Object.keys(bundle)[0];
const filePath = `./dist/${fileName}`;
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error("Error reading the file:", err);
return;
}
// Use string-escape to escape the file content
const escapedContent = jsesc(data, jsescConfigObj);
// Wrap if needed
const wrapBeforeClean = wrapBefore ? wrapBefore : "";
const wrapAfterClean = wrapAfter ? wrapAfter : "";
const wrappedContent =
wrapBeforeClean + escapedContent + wrapAfterClean;
// Write the modified content back to the file or a new file
fs.writeFile(filePath, wrappedContent, "utf8", (writeErr) => {
if (writeErr) {
console.error("Error writing the file:", writeErr);
}
});
});
},
};
}