Skip to content

Commit

Permalink
refactor build
Browse files Browse the repository at this point in the history
  • Loading branch information
Vap0r1ze committed Jan 9, 2023
1 parent 0b4e284 commit 86aad68
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 136 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
"match": "*://*/*"
},
"scripts": {
"prepare": "tsx scripts/fetchDataset.ts",
"build": "tsx scripts/build.ts",
"dev": "tsx scripts/buildDev.ts"
"build": "tsx scripts/build/build.ts",
"dev": "tsx scripts/build/dev.ts"
},
"devDependencies": {
"@types/node": "^18.11.18",
Expand Down
25 changes: 0 additions & 25 deletions scripts/build.ts

This file was deleted.

23 changes: 23 additions & 0 deletions scripts/build/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import assert from "assert"
import { build } from "esbuild"
import { basename } from "path"
import { commonOptions, writeOutput } from "./common"

build({
...commonOptions,
}).then(({ outputFiles: [buildOutput] }) => {
const meta = {}
const { GITHUB_JOB, GITHUB_REF_NAME, GITHUB_REPOSITORY } = process.env

if (GITHUB_JOB === "do_release") {
assert(GITHUB_REF_NAME && GITHUB_REPOSITORY)
meta["version"] = GITHUB_REF_NAME.slice(1)
meta[
"updateURL"
] = `https://github.com/${GITHUB_REPOSITORY}/releases/latest/download/${basename(
commonOptions.outfile!
)}`
}

writeOutput(buildOutput, meta)
})
2 changes: 2 additions & 0 deletions scripts/buildCommon.ts → scripts/build/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BuildOptions, OutputFile } from "esbuild"
import { mkdirSync, readFileSync, writeFileSync } from "fs"
import { dirname } from "path"
import { wordsPlugin } from "./plugins/words"

type ScriptMeta = Record<string, string | string[]>

Expand All @@ -21,6 +22,7 @@ export const commonOptions: BuildOptions & { write: false } = {
loader: {
".css": "text",
},
plugins: [wordsPlugin],
}

// Userscript Header
Expand Down
7 changes: 1 addition & 6 deletions scripts/buildDev.ts → scripts/build/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import assert from "assert"
import { build, BuildResult } from "esbuild"
import { writeFileSync } from "fs"
import { pathToFileURL } from "url"
import {
commonOptions,
getHeaderMeta,
scriptMeta,
writeOutput,
} from "./buildCommon"
import { commonOptions, getHeaderMeta, scriptMeta, writeOutput } from "./common"

function onBuild(result: BuildResult) {
assert(result.outputFiles, "no output files")
Expand Down
43 changes: 43 additions & 0 deletions scripts/build/plugins/words.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Plugin } from "esbuild"
import fetch from "node-fetch"
import type { LinkuWord, Word } from "../../../src/data"

export const wordsPlugin: Plugin = {
name: "linku-words",
setup: ({ onResolve, onLoad }) => {
const filter = /^~words$/
onResolve({ filter }, ({ path }) => ({
namespace: "linku-words",
path,
}))
onLoad({ filter, namespace: "linku-words" }, async () => {
const { data: linkuData } = await fetch(
"https://linku.la/jasima/data.json"
).then(
(res) => res.json() as Promise<{ data: Record<string, LinkuWord> }>
)

const wordData: Record<string, Word> = {}

for (const key of Object.keys(linkuData)) {
const word = linkuData[key]
wordData[key] = {
word: word.word,
def: word.def.en,
usage_category: word.usage_category,
recognition: word.recognition,
book: word.book,
coined_year: word.coined_year,
sitelen_pona: word.sitelen_pona,
ucsur: word.ucsur,
}
}

return {
contents: `const words = JSON.parse(${JSON.stringify(
JSON.stringify(wordData)
)})\nexport default words\n`,
}
})
},
}
26 changes: 0 additions & 26 deletions scripts/fetchDataset.ts

This file was deleted.

82 changes: 8 additions & 74 deletions src/data.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,14 @@
import { wordsData } from "../data/words"
import words from "~words"
export { words }

export const words = wordsData as Record<string, Word>
export const wordList = new Set(
Object.keys(words).map((word) => word.toLowerCase())
export const wordLookup = new Map(
Object.entries(words).map(([key, value]) => [key.toLowerCase(), value])
)

export function isWord(word: string) {
return wordList.has(word.toLowerCase())
return wordLookup.has(word.toLowerCase())
}

export type UsageCategory =
| "obscure"
| "rare"
| "uncommon"
| "common"
| "widespread"
| "core"
export type BookName = "pu" | "ku suli" | "ku lili" | "none"
export type CoinedEra = "pre-pu" | "post-pu" | "post-ku"
export type Tag = "pre-pu ALT" | "nimi nanpa"

export interface Word {
word: string
/**
* Maps from language code to definition
*/
def: Record<string, string>
book: BookName
commentary?: string
sitelen_pona?: string
sitelen_pona_etymology?: string

/**
* Maps from date to string of percentage used
*
* @example { '2022-08': '99' }
*/
recognition: Record<string, string>

/**
* Unicode
*/
ucsur?: string
/**
* Image URL
*/
sitelen_sitelen?: string
/**
* Emoji character
*/
sitelen_emosi?: string
luka_pona?: {
mp4: string
gif: string
}
/**
* URLs to audio files from different speakers
*/
audio?: Record<string, string>
coined_year?: string
coined_era?: CoinedEra
usage_category: UsageCategory
source_language?: string
etymology?: string
creator?: string
ku_data?: string

/**
* Other words, separated by commas
*/
see_also?: string

tags?: Tag

/**
* Only a few language codes (en, fr, de, eo)
*/
pu_verbatim?: Record<string, string>
}
// for (const [word, data] of wordLookup.entries()) {
// data.ucsur
// }
4 changes: 2 additions & 2 deletions src/display.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Word } from "data"
import type { Word } from "types"
import css from "./style.css"

let wordDisplay: HTMLElement | null = null
Expand Down Expand Up @@ -43,7 +43,7 @@ function WordDisplay(word: Word) {
.filter(Boolean)
.join(" · ")}
</div>
<div class="desc">{word.def.en}</div>
<div class="desc">{word.def}</div>
</div>
{word.sitelen_pona && (
<div class="image">
Expand Down
5 changes: 5 additions & 0 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ declare module "*.css" {
const source: string
export default source
}

declare module "~words" {
const words: Record<string, import("./types").Word>
export default words
}
83 changes: 83 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
export type UsageCategory =
| "obscure"
| "rare"
| "uncommon"
| "common"
| "widespread"
| "core"
export type BookName = "pu" | "ku suli" | "ku lili" | "none"
export type CoinedEra = "pre-pu" | "post-pu" | "post-ku"
export type Tag = "pre-pu ALT" | "nimi nanpa"

export interface Word
extends Pick<
LinkuWord,
| "word"
| "book"
| "sitelen_pona"
| "recognition"
| "coined_year"
| "usage_category"
| "ucsur"
> {
def: LinkuWord["def"]["en"]
}

export interface LinkuWord {
word: string
/**
* Maps from language code to definition
*/
def: Record<string, string>
book: BookName
commentary?: string
sitelen_pona?: string
sitelen_pona_etymology?: string

/**
* Maps from date to string of percentage used
*
* @example { '2022-08': '99' }
*/
recognition: Record<string, string>

/**
* Unicode
*/
ucsur?: string
/**
* Image URL
*/
sitelen_sitelen?: string
/**
* Emoji character
*/
sitelen_emosi?: string
luka_pona?: {
mp4: string
gif: string
}
/**
* URLs to audio files from different speakers
*/
audio?: Record<string, string>
coined_year?: string
coined_era?: CoinedEra
usage_category: UsageCategory
source_language?: string
etymology?: string
creator?: string
ku_data?: string

/**
* Other words, separated by commas
*/
see_also?: string

tags?: Tag

/**
* Only a few language codes (en, fr, de, eo)
*/
pu_verbatim?: Record<string, string>
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"strictNullChecks": true,
"noImplicitAny": true,
"esModuleInterop": true,
"target": "ES6",
"lib": [
"DOM",
"ES2022"
Expand Down

0 comments on commit 86aad68

Please sign in to comment.