diff --git a/package.json b/package.json index b223ab1..72ede99 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start-release": "npm run build && serve dist", "lint": "eslint \"{src,test}/**/*.{ts,tsx}\"", "typecheck": "tsc --noEmit", - "test": "tap run", + "test": "tap run --allow-incomplete-coverage", "prebuild": "npm run lint && npm run typecheck && npm run test", "preview": "vite preview" }, diff --git a/build-resources/th-en-x-basic.dat b/src/assets/th-en-x-basic.ooo-thesaurus similarity index 100% rename from build-resources/th-en-x-basic.dat rename to src/assets/th-en-x-basic.ooo-thesaurus diff --git a/src/build-scripts/oooThesaurusParser.test.ts b/src/build-scripts/oooThesaurusParser.test.ts index 617fbb2..53a3062 100644 --- a/src/build-scripts/oooThesaurusParser.test.ts +++ b/src/build-scripts/oooThesaurusParser.test.ts @@ -4,7 +4,7 @@ import { extractWordsFromFile } from './oooThesaurusParser.js'; void t.test('extractWordsFromFile', async (t) => { const words = await extractWordsFromFile( - new URL('../../build-resources/th-en-x-basic.dat', import.meta.url), + new URL('../assets/th-en-x-basic.ooo-thesaurus', import.meta.url), ); //console.log(words); t.equal(words.length, 24691); diff --git a/src/build-scripts/viteThesaurusRegexPlugin.test.ts b/src/build-scripts/viteThesaurusRegexPlugin.test.ts index ec2952b..a860823 100644 --- a/src/build-scripts/viteThesaurusRegexPlugin.test.ts +++ b/src/build-scripts/viteThesaurusRegexPlugin.test.ts @@ -4,7 +4,7 @@ import { getRegexStringForThesaurusPath } from './viteThesaurusRegexPlugin.js'; void t.test('extractWordsFromFile', async (t) => { const regexString = await getRegexStringForThesaurusPath( - new URL('../../build-resources/th-en-x-basic.dat', import.meta.url), + new URL('../assets/th-en-x-basic.ooo-thesaurus', import.meta.url), ); const regex = new RegExp(regexString, 'gi'); diff --git a/src/build-scripts/viteThesaurusRegexPlugin.ts b/src/build-scripts/viteThesaurusRegexPlugin.ts index 15491b5..c535966 100644 --- a/src/build-scripts/viteThesaurusRegexPlugin.ts +++ b/src/build-scripts/viteThesaurusRegexPlugin.ts @@ -1,10 +1,13 @@ import type { PathLike } from 'node:fs'; +import fs from 'node:fs/promises'; +import path from 'node:path'; +import { Plugin } from 'vite'; import { extractWordsFromFile } from './oooThesaurusParser.js'; -/*type ViteThesaurusRegexPluginOptions = { - inputOooThesaurusPath: PathLike; - outputRegexPath: string; -};*/ +type ViteThesaurusRegexPluginOptions = { + sourceUrl: URL; + thesaurusPath: string; +}; const WORD_CHARACTER_REGEX_FRAGMENT = "[a-zA-Z']"; @@ -17,9 +20,18 @@ export const getRegexStringForThesaurusPath = async ( )}))`; }; -/*export const viteThesaurusRegexPlugin = ({ - inputOooThesaurusPath, - outputRegexPath, -}: ViteThesaurusRegexPluginOptions) => { -}; -*/ +export const viteThesaurusRegexPlugin = ({ + thesaurusPath, + sourceUrl, +}: ViteThesaurusRegexPluginOptions): Plugin => ({ + name: 'vite-thesaurus-regex-plugin', + async writeBundle(options) { + if (!options.dir) { + throw new Error('dir is empty'); + } + const regexString = await getRegexStringForThesaurusPath( + new URL(thesaurusPath, sourceUrl), + ); + await fs.writeFile(path.join(options.dir, thesaurusPath), regexString); + }, +}); diff --git a/src/index.tsx b/src/index.tsx index 16b5ec1..2dc4f9c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,9 +1,15 @@ import { render } from 'preact'; +import { SpellChecker } from './spellChecker'; import './style.css'; export function App() { - return

Hello world

; + return ( + <> +

Spell checker

+ + + ); } const app = document.getElementById('app'); diff --git a/src/spellChecker/index.tsx b/src/spellChecker/index.tsx new file mode 100644 index 0000000..5c91954 --- /dev/null +++ b/src/spellChecker/index.tsx @@ -0,0 +1,17 @@ +import { useEffect, useState } from 'preact/hooks'; + +export const SpellChecker = () => { + const [regex, setRegex] = useState(); + useEffect(() => { + if (!regex) { + setRegex(new URL(`./assets/img/abc.def`, import.meta.url).href); + } + }, [regex]); + return ( +
+ +
+ ); +}; diff --git a/vite.config.ts b/vite.config.ts index b1ef9e2..fc47bce 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,8 +1,16 @@ import { defineConfig } from 'vite'; import preact from '@preact/preset-vite'; +import { viteThesaurusRegexPlugin } from './src/build-scripts/viteThesaurusRegexPlugin'; // https://vitejs.dev/config/ export default defineConfig({ - base: './', - plugins: [preact()], + base: './', + assetsInclude: ['**/*.ooo-thesaurus'], + plugins: [ + viteThesaurusRegexPlugin({ + sourceUrl: new URL('src/', import.meta.url), + thesaurusPath: 'assets/th-en-x-basic.ooo-thesaurus', + }), + preact(), + ], });