Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ES6 `import` with CSS/HTML files in Meteor project: bug or feature?

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
like image 406
pgpb.padilla Avatar asked Mar 19 '16 17:03

pgpb.padilla


1 Answers

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.,

== myapp.html ==

<body>
  <h1>Welcome to Meteor!</h1>
  {{> hello}}
</body>

results in the following JS code:

Template.body.addContent((function () {                                                                       
  var view = this;                                                                                          
  return [
     HTML.Raw("<h1>Welcome to Meteor!</h1>\n\n  "),      
     Spacebars.include(view.lookupTemplate("hello"))
  ];
}));                                                                                                        

Which is wrapped in a function with the name of the file as it's key:

"myapp.html": function (require, exports, module) {

     Template.body.addContent((function () {                                                                       
          var view = this;                                                                                          
          return [
             HTML.Raw("<h1>Welcome to Meteor!</h1>\n\n  "),   
             Spacebars.include(view.lookupTemplate("hello"))]; 
     })); 

     Meteor.startup(Template.body.renderToDocument);                                                              

     Template.__checkName("hello");                                                                               
     Template["hello"] = new Template("Template.hello", (
         function () {                                            
           var view = this;                                                                                         
           return [
               HTML.Raw("<button>Click Me</button>\n  "), 
               HTML.P("You've pressed the button ", 
                      Blaze.View("lookup:counter", 
                      function () {
                        return Spacebars.mustache(view.lookup("counter"));                                                   
                      }), " times.")
          ];                                                                                         
     }));                                                                                                         

},

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.

== myapp.css ==

/* CSS declarations go here */

body {
    background-color: lightblue;
}

Gets transformed into:

"myapp.css": ["meteor/modules", function (require, exports, module) {
    module.exports = require("meteor/modules").addStyles("/* CSS declarations go here */\n\nbody {\n    background-color: lightblue;\n}\n");

}]

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:

import '../imports/hello/myapp.html';
import '../imports/hello/myapp.css';

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.

like image 86
pgpb.padilla Avatar answered Oct 05 '22 16:10

pgpb.padilla