I am currently learning Meteor and I found out something that intrigued me.
I can load HTML and CSS assets from a JS file using the import statement.
import '../imports/hello/myapp.html';
import '../imports/hello/myapp.css';
import * as myApp from '../imports/hello/myapp.js';
This was a surprise to me so I ran to google but could not find this behavior documented in the specification for ES6 import
or in Meteor's Docs.
So my questions are:
- Can I rely on this behavior to build my apps?
- Will my app will break when Meteor gets around to fix it -- if it's a bug --?
Notes
- I am using Meteor v1.3, not sure if this works also with previous versions.
- You can download the app to see this behavior from Github
One of the features in Meteor 1.3 is lazy-loading where you place your files in the
/imports
folder and will not be evaluated eagerly.Quote from Meteor Guide:
So you can lazy load your css files by importing them from the
/imports
folder. I would say it's a feature.ES6 export and import functionally are available in Meteor 1.3. You should not be importing HTML and CSS files if you are using Blaze, the current default templating enginge. The import/export functionality is there, but you may be using the wrong approach for building your views.
After going through the implementation of the built files for my app I found out why this works.
HTML
Files are read from the file system and their contents added to the global Template object, e.g.,
results in the following JS code:
Which is wrapped in a function with the name of the file as it's key:
So all of our HTML is now pure JS code which will be included by using
require
like any other module.CSS
The files are also read from the file system and their contents are embedded also in JS functions, e.g.
Gets transformed into:
So all of our CSS is also now a JS module that's again imported later on by using
require
.Conclusion
All files are in one way or another converted to JS modules that follow similar rules for inclusion as AMD/CommonJS modules. They will be included/bundled if another module refers to them. And since all of them are transformed to JS code there's no magic behind the deceitful syntax:
They both are transpiled to their equivalent forms with
require
once the assets have been transformed to JS modules.Whereas the approach of placing static assets in the
imports
directory is not mentioned in the official documentation, this way of importing static assets works.This seems to be at the core of how Meteor works so I'd bet this functionality is going to be there for a long while.
I don't know if to call this a feature maybe a more appropriate description is unexpected consequence but that would only be true from the user's perspective, I assume the people who wrote the code understood this would happen and perhaps even designed it purposely this way.