Skip to content

Commit a35ec0d

Browse files
committed
fix: Intl.Segmentor works in demo on firefox
1 parent 5b3ed9d commit a35ec0d

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

docs/.vitepress/theme/index.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,42 @@ if (typeof window !== "undefined") {
1111
}
1212
import type { Theme } from "vitepress"
1313
import DefaultTheme from "vitepress/theme"
14-
// @ts-expect-error -- ignore
1514
import Layout from "./Layout.vue"
16-
// @ts-expect-error -- ignore
17-
import ESLintCodeBlock from "./components/eslint-code-block.vue"
18-
// @ts-expect-error -- ignore
19-
import PlaygroundBlock from "./components/playground-block.vue"
2015

2116
const theme: Theme = {
2217
...DefaultTheme,
2318
Layout,
24-
enhanceApp(ctx) {
19+
async enhanceApp(ctx) {
2520
DefaultTheme.enhanceApp(ctx)
21+
22+
if (typeof Intl.Segmenter === "undefined") {
23+
await setupIntlSegmenter()
24+
}
25+
26+
const ESLintCodeBlock = await import(
27+
"./components/eslint-code-block.vue"
28+
).then((m) => m.default ?? m)
29+
const PlaygroundBlock = await import(
30+
"./components/playground-block.vue"
31+
).then((m) => m.default ?? m)
2632
ctx.app.component("eslint-code-block", ESLintCodeBlock)
2733
ctx.app.component("playground-block", PlaygroundBlock)
2834
},
2935
}
3036
export default theme
37+
38+
// We can remove this polyfill once Firefox supports Intl.Segmenter.
39+
async function setupIntlSegmenter() {
40+
// For Firefox
41+
const [{ createIntlSegmenterPolyfill }, breakIteratorUrl] =
42+
await Promise.all([
43+
import("intl-segmenter-polyfill"),
44+
import(
45+
// @ts-expect-error -- polyfill
46+
"intl-segmenter-polyfill/dist/break_iterator.wasm?url"
47+
).then((m) => m.default ?? m),
48+
])
49+
50+
// @ts-expect-error -- polyfill
51+
Intl.Segmenter = await createIntlSegmenterPolyfill(fetch(breakIteratorUrl))
52+
}

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"eslint-plugin-regexp": "~1.15.0",
8888
"eslint-plugin-vue": "^9.0.0",
8989
"eslint-plugin-yml": "^1.0.0",
90+
"intl-segmenter-polyfill": "^0.4.4",
9091
"markdownlint-cli": "^0.37.0",
9192
"mocha": "^10.0.0",
9293
"mocha-chai-jest-snapshot": "^1.1.3",

0 commit comments

Comments
 (0)