forked from lydell/elm-safe-virtual-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (52 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const fs = require("fs");
const path = require("path");
const { replacements, debuggerReplacements } = require("./patch");
function runReplacements(code) {
if (!/^function _Browser_/m.test(code)) {
throw new Error(
`
Could not find \`function _Browser_\`.
Make sure that:,
- The Elm code has \`import Browser\`.
- The JavaScript code is NOT minified.
`.trim()
);
}
const newCode = replacements.reduce(strictReplace, code);
return code.includes("Compiled in DEBUG mode")
? debuggerReplacements.reduce(strictReplace, newCode)
: newCode;
}
function strictReplace(code, [search, replacement, allow0matches = false]) {
const parts = code.split(search);
if (!allow0matches && parts.length <= 1) {
const filePath = path.resolve("elm-virtual-dom-patch-error.txt");
const content = replaceErrorMessage(search, replacement, code);
try {
fs.writeFileSync(filePath, content);
} catch (error) {
throw new Error(
`Elm Virtual DOM patch: Code to replace was not found! Tried to write more info to ${filePath}, but got this error: ${error.message}`
);
}
throw new Error(
`Elm Virtual DOM patch: Code to replace was not found! More info written to ${filePath}`
);
}
return typeof search === "string"
? parts.join(replacement)
: code.replace(search, replacement);
}
function replaceErrorMessage(search, replacement, code) {
return `
Patching Elm’s JS output to avoid virtual DOM errors caused by browser extensions failed!
This message is defined in the app/patches/ folder.
### Code to replace (not found!):
${search}
### Replacement:
${replacement}
### Input code:
${code}
`.trimStart();
}
module.exports = runReplacements;