Skip to content

Commit

Permalink
fix: avoid vite sideEffects bug
Browse files Browse the repository at this point in the history
Use `import` instead of <script>
Ref vitejs/vite#10735
  • Loading branch information
azu committed Feb 10, 2023
1 parent 1b4308a commit 746be5b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
8 changes: 7 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
<meta charset="UTF-8">
<title>sentence-splitter</title>
<link href="./index.css" rel="stylesheet">
<script src="./index.ts" type="module"></script>
<script type="module">
// Avoid Vite sideEffects bug
// https://github.com/vitejs/vite/issues/10735
import { run } from "./index.ts";

run();
</script>
</head>
<body>
<main class="main">
Expand Down
38 changes: 20 additions & 18 deletions public/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { split } from "../src/sentence-splitter.js";

const textElement = document.querySelector("#text") as HTMLTextAreaElement;
const jsonElement = document.querySelector("#json") as HTMLTextAreaElement;
export const run = () => {
const textElement = document.querySelector("#text") as HTMLTextAreaElement;
const jsonElement = document.querySelector("#json") as HTMLTextAreaElement;

const onUpdate = (text: string) => {
try {
const json = split(text);
jsonElement.textContent = JSON.stringify(json, null, 4);
location.hash = encodeURIComponent(text);
} catch {}
};
const onUpdate = (text: string) => {
try {
const json = split(text);
jsonElement.textContent = JSON.stringify(json, null, 4);
location.hash = encodeURIComponent(text);
} catch {}
};

textElement?.addEventListener("input", () => {
onUpdate(textElement.value);
});
textElement?.addEventListener("input", () => {
onUpdate(textElement.value);
});

const textFromURL = location.hash;
if (textFromURL.length > 0) {
const decodedText = decodeURIComponent(textFromURL);
textElement.value = decodedText;
onUpdate(decodedText);
}
const textFromURL = location.hash;
if (textFromURL.length > 0) {
const decodedText = decodeURIComponent(textFromURL);
textElement.value = decodedText;
onUpdate(decodedText);
}
};

0 comments on commit 746be5b

Please sign in to comment.