Continue Discussion 74 replies
Mar '16

faysal

i have some confusion regarding the application structure guide. As far as i understand from the guide that, anything under import folder will be lazy loaded or you can say will load when called via import. and ouside of import folder will be loaded as soon as application starts running.

when i’m installing FlowRouter, my app need to have home ( / ) router.
My question is

  1. what do i need to write in my main.js file and what exactly is entry point meaning.
    will I import my route file and all layout, templates in main.js file?
  2. do imports/startup/ directory files will be executed automatically after app starts to run? or do i need to enclose it within Meteor.startup function.
1 reply
Mar '16

hwillson

I’ll break down how the Guide’s todos reference app is handling this, as an example.

When the todos app starts up client/main.js is one of the eagerly loaded files (since it’s not under the imports directory). This means its contents are evaluated right away:

import '/imports/startup/client';

This import statement then loads the defined imports/startup/client/index.js file (since only the directory was specified in the import statement). The index.js file then includes an import statement to load the routes.js file from the same directory:

import './routes.js';

The routes.js file defines your FlowRouter routes, but also imports any templates referenced by the route config (such as your layouts, top level pages, etc.).

No they aren’t executed automatically - in the case of the todos app for example, you’re telling your app to load the startup files by referencing them when your app starts up, via the eagerly loaded client/main.js and server/main.js files.

3 replies
Mar '16

faysal

so i need to actually import all my templates and their js files and layouts files in the routes.js file. am I right?

1 reply
Mar '16 ▶ faysal

hwillson

No, not all of them. Just the ones that FlowRouter needs to know about to initialize properly. For example, in imports/startup/client/routes.js, the app-body.js layout is imported since it’s needed by the defined FlowRouter config directly. app-body.js itself imports the imports/ui/components/loading.js component; this file doesn’t need to be imported in the routes.js file directly.

Apr '16

skirunman

I can no longer find any documentation on the default eager load order based on file names and structure. This used to be in Meteor docs, but was removed. In fact, it is still referenced in the docs in modules package with a redirect to this section of the Meteor Guide.

I think some of this info needs to be added back to this section of the guide as 1) not everyone will or wants to adopt 100% import structure, and 2) there are still some special directories such as /public, /private, and mobile /resources, etc.

Here is original file in github.

If others agree I’ll make a pull request.

Apr '16

sashko MDG Staff

Yeah that’s a good point, looks like that information just disappeared! Let’s add it as the last section in the application structure article!

Thanks for noticing.

1 reply
Apr '16 ▶ sashko

skirunman

Only noticed as I was trying to find some info for one of our developers. :grinning:

Apr '16 ▶ hwillson

tab00

Are the index.js files really necessary when all they contain are import statements that could instead be written in client/main.js and server/main.js?

i.e. could these be moved out of imports/startup/client/index.js

import './useraccounts-configuration.js';
import './routes.js';

into client/main.js?

1 reply
Apr '16 ▶ tab00

hwillson

They could be, but if you’re then referencing routes.js and useraccounts-configuration.js in the same directory like your example shows, that means they’re in /client (outside of the imports directory), and will be eagerly loaded. If you wanted to update /client/main.js with something like:

import '/imports/startup/client/useraccounts-configuration.js';
import '/imports/startup/client/routes.js';

then no problem at all. Or any directory within imports really, like:

import '/imports/config/useraccounts-configuration.js';
import '/imports/config/routes.js';

The Guide/todos app are just suggesting a structure, and recommend keeping the contents of the default eagerly loaded files (like /client/main.js and /server/main.js) to a minimum, to help better control load order.

1 reply
Apr '16 ▶ hwillson

trajano

Oh this is interesting, I didn’t know you can do

import '/imports/...'

That would’ve made my life a bit easier since I don’t have to count how far up I have to go to load the import.