implemented vite plugin to convert OOO thesaurus to regex on build

main
Inga 🏳‍🌈 11 months ago
parent 06d2c2de91
commit dccbf7cde8
  1. 2
      package.json
  2. 0
      src/assets/th-en-x-basic.ooo-thesaurus
  3. 2
      src/build-scripts/oooThesaurusParser.test.ts
  4. 2
      src/build-scripts/viteThesaurusRegexPlugin.test.ts
  5. 32
      src/build-scripts/viteThesaurusRegexPlugin.ts
  6. 8
      src/index.tsx
  7. 17
      src/spellChecker/index.tsx
  8. 12
      vite.config.ts

@ -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"
},

@ -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);

@ -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');

@ -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);
},
});

@ -1,9 +1,15 @@
import { render } from 'preact';
import { SpellChecker } from './spellChecker';
import './style.css';
export function App() {
return <h1>Hello world</h1>;
return (
<>
<h1>Spell checker</h1>
<SpellChecker />
</>
);
}
const app = document.getElementById('app');

@ -0,0 +1,17 @@
import { useEffect, useState } from 'preact/hooks';
export const SpellChecker = () => {
const [regex, setRegex] = useState<string>();
useEffect(() => {
if (!regex) {
setRegex(new URL(`./assets/img/abc.def`, import.meta.url).href);
}
}, [regex]);
return (
<section>
<textarea rows={10} cols={100}>
{regex}
</textarea>
</section>
);
};

@ -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(),
],
});

Loading…
Cancel
Save