I am currently developing a Vue application using Vite.
Within the content
folder, I have numerous files (ranging from 10 to 100) located as follows:
content/block/paragraph.json
content/block/theorem.json
content/inliner/link.json
...
My goal is to create navigation data for my app and utilize this data within the app to generate a sidebar <nav>
.
In order to accomplish this, I need to extract specific data (the title
property) from each individual json
file.
Here's an example of the calculated nav
structure that I aim to integrate into my app:
const nav = {
"block/paragraph": "Paragraph", // Retrieved from 'content/block/paragraph.json'
"block/theorem": "Theorem", // Extracted from 'content/block/theorem.json'
// ...
}
The challenge lies in the fact that performing these operations within the Vue app itself is not ideal, as it may introduce delays and result in unnecessary network traffic due to loading all json
files.
Is there a way to construct the nav
object (or function) prior to initiating the Vue app, and then utilize the pre-generated data within the app?
In this scenario, three potential solutions come to mind, yet I'm uncertain which approach is optimal:
- Implement a separate script for generating the navigation data (although this method lacks the convenience of utilizing Vite features like advanced imports and necessitates running the script independently before
npm run dev
, while also creating a.temp
folder for storing the nav data) - Virtual Module. While promising, this solution entails converting the module into a string to function properly, a process I struggle to grasp completely...
- Consider some form of Vite plugin other than Virtual Module?
What represents the optimal strategy for resolving this predicament?
On a broader note, is there a convenient methodology for preparing data and conducting computations prior to launching the application, followed by employing said data within the app?