implemented vite plugin to convert OOO thesaurus to regex on build

main
Inga 🏳‍🌈 10 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. 10
      vite.config.ts

@ -7,7 +7,7 @@
"start-release": "npm run build && serve dist", "start-release": "npm run build && serve dist",
"lint": "eslint \"{src,test}/**/*.{ts,tsx}\"", "lint": "eslint \"{src,test}/**/*.{ts,tsx}\"",
"typecheck": "tsc --noEmit", "typecheck": "tsc --noEmit",
"test": "tap run", "test": "tap run --allow-incomplete-coverage",
"prebuild": "npm run lint && npm run typecheck && npm run test", "prebuild": "npm run lint && npm run typecheck && npm run test",
"preview": "vite preview" "preview": "vite preview"
}, },

@ -4,7 +4,7 @@ import { extractWordsFromFile } from './oooThesaurusParser.js';
void t.test('extractWordsFromFile', async (t) => { void t.test('extractWordsFromFile', async (t) => {
const words = await extractWordsFromFile( 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); //console.log(words);
t.equal(words.length, 24691); t.equal(words.length, 24691);

@ -4,7 +4,7 @@ import { getRegexStringForThesaurusPath } from './viteThesaurusRegexPlugin.js';
void t.test('extractWordsFromFile', async (t) => { void t.test('extractWordsFromFile', async (t) => {
const regexString = await getRegexStringForThesaurusPath( 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'); const regex = new RegExp(regexString, 'gi');

@ -1,10 +1,13 @@
import type { PathLike } from 'node:fs'; 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'; import { extractWordsFromFile } from './oooThesaurusParser.js';
/*type ViteThesaurusRegexPluginOptions = { type ViteThesaurusRegexPluginOptions = {
inputOooThesaurusPath: PathLike; sourceUrl: URL;
outputRegexPath: string; thesaurusPath: string;
};*/ };
const WORD_CHARACTER_REGEX_FRAGMENT = "[a-zA-Z']"; const WORD_CHARACTER_REGEX_FRAGMENT = "[a-zA-Z']";
@ -17,9 +20,18 @@ export const getRegexStringForThesaurusPath = async (
)}))`; )}))`;
}; };
/*export const viteThesaurusRegexPlugin = ({ export const viteThesaurusRegexPlugin = ({
inputOooThesaurusPath, thesaurusPath,
outputRegexPath, sourceUrl,
}: ViteThesaurusRegexPluginOptions) => { }: 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 { render } from 'preact';
import { SpellChecker } from './spellChecker';
import './style.css'; import './style.css';
export function App() { export function App() {
return <h1>Hello world</h1>; return (
<>
<h1>Spell checker</h1>
<SpellChecker />
</>
);
} }
const app = document.getElementById('app'); 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 { defineConfig } from 'vite';
import preact from '@preact/preset-vite'; import preact from '@preact/preset-vite';
import { viteThesaurusRegexPlugin } from './src/build-scripts/viteThesaurusRegexPlugin';
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
base: './', base: './',
plugins: [preact()], assetsInclude: ['**/*.ooo-thesaurus'],
plugins: [
viteThesaurusRegexPlugin({
sourceUrl: new URL('src/', import.meta.url),
thesaurusPath: 'assets/th-en-x-basic.ooo-thesaurus',
}),
preact(),
],
}); });

Loading…
Cancel
Save