you could do it probably with require("/client/templates/"+whatever + ‘.jsx’) but thats generally a bad idea.
If imports are not static, the bundler cannot know which files to take into the bundle and which files not. It’s working (at the moment) with require, because meteor bundles all the files together.
Even if the code would be lazy loaded whenever an import or require is called, this could not work, because how would the server know which files can be loaded by the client? (ok you could solve this with a manifest, but still…)
Imports are static, so better just declare some kind of map for the possible templates and pages:
import Home from '/client/pages/home.jsx';
import About from '/client/pages/about.jsx';
const Pages = {Home, About, ...};
// somewhere else
const Content = Pages[route]; // route == "Home", or "About, ...
But sometimes code just should be stupid. In your case, i would manually set all the routes that you need without magic dynamic loading:
No. I’m more-or-less trying to understand the patterning. I’m new to Meteor, and coming from a LAMP background, I’m trying to keep things DRY while simultaneously learning/constructing a basic file structure that would innately keep my code base smaller. (If that makes sense).
So, for instance, you could potentially use two to three directories, and the files they contain, to do all your routing and mounting dynamically. So you’d never have to write that code again.
Beeing DRY is always good, however there is also the saying “you ain’t gonna need it”
I think its always best to learn a new technology by solving a real problem with it. Maybe you one day really need to route 200 different files and layouts, but then you would build more like a framework, then an app.
You can’t use FS from the client either. I guess there might be some bundling systems out there that support this kind of totally dynamic importing but I’m not aware of any.
Wouldn’t using FS to import html conceivably remove what could be a large block of code, not only for the current project, but all projects you might undertake going forward? Like automating FlowRouter via the filesystem.
It seems foundational to me, but again I’m new to this, and might not be abstracting it properly. But it seems like if you could dynamically import, using the filesystem as your abstract, you could basically just start dropping files into directories and viola build out your app.
Wasn’t sure about reviving this topic, but this seems to be the only place I’ve seen conversation about “dynamic, dynamic imports”.
My use case was using Blaze.renderWithData to create some dialogs, which is done by passing in the name of the dialog template you want to use and the data. I also wanted to be able to dynamically import the templates, just based on the string being passed in: