lasso-js / lasso

Advanced JavaScript module bundler, asset pipeline and optimizer
581 stars 75 forks source link

lasso-loader throws error for already loaded resources #175

Closed schetnikovich closed 7 years ago

schetnikovich commented 7 years ago

When using require('lasso-loader').async(...) for already loaded resources (JS, CSS etc), Lasso throws error similar to: Loader metadata missing for "_0".

The minimal page setup that reproduces this behavior is the following.

Content of browser.json:

{
  "dependencies": [
    "require-run: ./client.js"
  ]
}

Content of client.js (the only client script):

window.onload = function() {
  require("lasso-loader").async(function(err) {
    var client = require('./client.js'); // we are loading "ourselves"
  });
};

The following error is thrown on page load:

Loader metadata missing for "_0"
    at Object.async (http://localhost:8080/static/page.js:1183:15)
    at window.onload (http://localhost:8080/static/page.js:1212:62)

But it works when you add not yet loaded resource

The same code will work when you add some not yet loaded resource. Here is client.js with single line added:

window.onload = function() {
  require("lasso-loader").async(function(err) {
    var empty = require('./empty.js');    // ← File "empty.js" can be just empty
    var client = require('./client.js');
  });
};

In this case, no errors are thrown.


I have faced with this error in a context when two bundles load asynchronously each other. Bundle1 successfully loads Bundle2, but then Bundle2 fails to load Bundle1. It seems that the fact that Bundle1 was already loaded, somehow prevents this operation to complete. Error message is the same as the one shown above.

Let me know if I'm doing something wrong. Thank you!

Environment:

lasso2.6.1
node6.5.0
npm3.10.3
OSMacOS 10.12
patrick-steele-idem commented 7 years ago

Looks like a bug, but I can see why it is happening. It looks like client.js is already part of the initial page load and because of that there is nothing to asynchronously load. Because there is nothing to asynchronously load there is no writing of the metadata required for async loading. As a result of no metadata, you are seeing an error.

I'll look into a fix. Thank you for reporting the problem!

schetnikovich commented 7 years ago

Thank you, Patrick! Just a small comment to help you understand my "real" problem. Minimal page setup presented above is not a "real-world" problem. What i'm trying to do is the following:

file1.js:

exports.load = function() {
  require("lasso-loader").async(function(err) {
    var client = require('./file2.js');
  });
};

file2.js:

exports.load = function() {
  require("lasso-loader").async(function(err) {
    var client = require('./file1.js');
  });
};

Both file1.js and file2.js can be loaded asynchronously by each other or as part of initial page load. Hope that this is possible with Lasso!

philidem commented 7 years ago

I fixed this issue and published lasso@2.7.2

schetnikovich commented 7 years ago

Just checked new lasso@2.7.2 and it works as expected! Thank you, Phillip and Patrick!