1

I have a pretty basic question and I've read through the Meteor Application Structure but this is still a little confusing:

In meteor chef's understanding the imports directory, it says that:

The index.js file is implied by not specifying a filename on the end. This is also known as an "entry point" file.

When I ran meteor create testproject --full to create a new project, in /client/main.js it writes import '/imports/startup/client';

Why doesn't main.js include the index.js file directly?
Why does import '/imports/startup/client' automatically include the index.js file only?

In Meteor's official documentation, index.js is not a reserved word.

  1. HTML template files are always loaded before everything else
  2. Files beginning with main. are loaded last
  3. Files inside any lib/ directory are loaded next
  4. Files with deeper paths are loaded next
  5. Files are then loaded in alphabetical order of the entire path

Quoted from another question.

user2587676
  • 117
  • 1
  • 11
  • 2
    Possible duplicate of [ES6 (EcmaScript 2015) modules: import index.js](https://stackoverflow.com/questions/37159533/es6-ecmascript-2015-modules-import-index-js) – Styx Oct 17 '17 at 18:39
  • I am not at that level of complexity which the link describes. In fact, I do not quite understand what ES6 standard is and how it really impacts the js world. I'm still quite new to Meteor...but I'm having difficulty structuring my app properly, and the way which it packages files gets confusing at times. E.g. CSS files and LESS files are treated differently. I did not have to write import statements for CSS files, all I have to do is put them in /lib or /client/lib and Meteor handles it for me, but LESS files needs to be imported in /client/main.less ? – user2587676 Oct 18 '17 at 02:16

1 Answers1

0

As Styx's comment points to, importing the index.js file is a characteristic of the CommonJS module system, which Node uses a version of and Meteor uses under the hood on the client.

The scaffold elects to not specify the index.js file for brevity.

It's also worth noting that the load order you've quoted does not apply when using the imports directory and ES6 imports. Files will be loaded in the order that they are referenced by the code.

coagmano
  • 5,542
  • 1
  • 28
  • 41
  • Thanks! From the link above and your comments I have deduced that `index.js` is implicitly referred to as the entry point file by ES6 and used by Node, hence Meteor inherits this behavior. Is this correct? – user2587676 Oct 18 '17 at 02:21
  • Technically no. the ECMAscript standard doesn't specify the loading behaviour. Which means it's silent about what to do with `index.js` or directories. It's also unaware of the concept of an entry-point (except as a way to talk about the first module that is evaluated). The behaviour is entirely a community standard established by CommonJs and Node – coagmano Oct 18 '17 at 03:45
  • So the bottom line is that Meteor implicitly references index.js as the entry point; this is a result of Node / CommonJs community standards. Can Meteor overwrite this behaviour? Also, is there a better way to control the file imports? e.g. we `import '/imports/startup/client/index.js'` in `/client/main.js`. Will this be a better practice? – user2587676 Oct 18 '17 at 05:33
  • If Meteor did overwrite the behaviour, it would make it incompatible with Node and much of the wider javascript community. It would also break a lot of existing Meteor apps, so It's not something that would ever happen. So feel free to use whichever you prefer without fear that it will change – coagmano Oct 18 '17 at 21:42