5

I am using this GULP plugin which converts HTML files into ES6 exports so I could load them on the browser in my MVC (using rollup bundler).

Basically I have page controllers which are exported as modules.

Then, in my main JS file, I just import all the page controllers, once by one, like so (simplified):

import * as page__home from './pages/page1';
import * as page__home from './pages/page2';
...

Since this is an SPA, I would think it would be easier to somehow import all the page controllers into some object, so then when a controller is to be called, I could check if that name exist in that object which holds all the imported controllers, or something like that.

Or maybe there is a way to check if a module was imported somehow? Is there any smarter way of doing this? Thanks

vsync
  • 118,978
  • 58
  • 307
  • 400
  • This reminds me of [import modules from files in directory](http://stackoverflow.com/q/29722270/1048572), but I'm not sure whether it's a duplicate or just an XY problem. – Bergi Oct 25 '15 at 21:48
  • @Bergi - I know how to import all files from a directory, that's not that, I mean, lets say somewhere in the code I expect some module to be available, how can I check that it was imported? lets say the module name is dynamic. so it's `"page__" + someName` – vsync Oct 25 '15 at 22:06
  • If the module name is really dynamic, you will need to use dynamic imports and dynamic module loading. If the imported modules are static, as your usage of the `import` syntax implies, there should be no reason to check that a module is available - all imported modules are available by definition (and when not, the complete module would fail). – Bergi Oct 25 '15 at 22:10
  • I think I got it actually, I will combine all the controllers files using gulp, then import that one file, and it will all be under that namespace, like so `import * as pages from './pages/bundle';` then I could check `if( pages["xxx"] )...` – vsync Oct 25 '15 at 22:12
  • Yes, that would've been my suggestion as well - you actually want a single static import, and export a collection of your files (either as an object or just as multiple named exports). – Bergi Oct 25 '15 at 22:14

1 Answers1

2

As noted above:

I think I got it actually, I will combine all the controllers files using gulp, then import that one file, and it will all be under that namespace, like so import * as pages from './pages/bundle'; then I could check if( pages["xxx"] )

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
  • @vsync Thanks for asking. For context, see https://meta.stackexchange.com/questions/170641/unanswered-questions-with-just-comments – Paul Sweatte Oct 11 '16 at 15:07