I have a JSON file I'd like to load from a `@JsExp...
# javascript
q
I have a JSON file I'd like to load from a
@JsExport
annotated function. The function should return the content of the file. When I package my library though, the file, stored in
src/jsMain/resources/file.json
is not included in the bundle. Is there any way to include it?
t
Do you want to bundle json file inside resulted JS bundle?
q
it doesn't really matter, as long as the function I expose returns the content of the JSON file (I'm assuming I have to bundle it)
t
You can use
import
function to include JSON file in bundle
q
do you have any example? Not sure I can use
import
and then annotate the function with
@JsExport
t
importAsync
will be fine
Copy code
@JsExport
fun getJson(): Promise<Data> {
    return importAsync<Data>("./data.json")
}

// legacy
@JsExport
fun getJson(): Data {
    return require<Data>("./data.json")
}