-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: code for loading JSON files
Got this warning from Vite: > [vite] warning: invalid import "./${listName}.json". Variable > imports cannot import their own directory, place imports in a separate > directory or make the import filename more specific. For example: > import(`./foo/${bar}.js`). ...though I'm not sure why since it seemed to work fine anyway. This feels better in any case: only one file loading the default word list.
- Loading branch information
Showing
3 changed files
with
22 additions
and
16 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export default class JsonDataImporter<T> { | ||
#defaultData: T; | ||
|
||
constructor({ defaultData }: { defaultData: T }) { | ||
this.#defaultData = defaultData; | ||
} | ||
|
||
async import(listName: string): Promise<T> { | ||
let data = this.#defaultData; | ||
|
||
try { | ||
data = (await import(`../data/${listName}.json`)).default; | ||
} catch (error) { | ||
console.error("Failed to load the data", error); | ||
} | ||
|
||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters