Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r.js build fails when trying to evaluate a plugin that shouldn't be evaluated #972

Closed
jslegers opened this issue Nov 22, 2017 · 2 comments
Closed

Comments

@jslegers
Copy link

jslegers commented Nov 22, 2017

I have an AMD plugin named MapLoader.

When importing samples/template/MapLoader!, this will either return the module luciad/view/Map or the module luciad/view/WebGLMap, depending on the URL query parameter "webgl".

Here's the code of the plugin :

define({
  load: function(na, require, onload, config) {
    if (window.location.search.indexOf('webgl') > 0 && 
        window.location.search.indexOf('webgl=false' < 0)) {
      map = "luciad/view/WebGLMap";
    } else {
      map = "luciad/view/Map";
    }

    require([map], function(value) {
      onload(value);
    });
  }
});

Now, I'm trying to use r.js to pack my project, but it doesn't know how to deal with this module, because it tries to evaluate the code. Hence, it produces the following error :

{ Error: ReferenceError: window is not defined
In module tree:
    samples/balloons/main
      samples/template/sample
        samples/template/MapLoader
  ...
}

My current config looks like this :

require('requirejs').optimize({
  baseUrl : moveFrom,
  modules: [
    { name: "./samples/template/MapLoader" }, 
    { name: "./samples/balloons/main" }
  ],
  packages: [
    { name: "loader", location: "./samples/lib/requirejs" },
    { name: "luciad", location: "./luciad" },
    { name: "samples", location: "./samples" }
  ],
  paths: {
    jquery: "./samples/lib/jquery/jquery-1.12.4"
  },
  dir : moveTo,
  stubModules: ['./samples/template/MapLoader'],
  optimize : "none",
  uglify2 : {
    output: {
      beautify: false
    },
    compress: {},
    warnings: true,
    mangle: true
  }
}, function (buildResponse) {
  console.log(buildResponse);
});

What am I missing?

What is the correct way to add this plugin to my build?

@jrburke
Copy link
Member

jrburke commented Nov 22, 2017

Loader plugins participate in the r.js tracing, so that loader-dependent modules can be inlined in bundles. To fix, the plugin's load method should be aware of the non-build context. Something like this is probably enough:

define({
  load: function(na, require, onload, config) {
    // Running in r.js, just inform the loader this load is finished,
    // does not need a real module value in the r.js build case.
    if (typeof window === 'undefined') {
        onload();
    }

    if (window.location.search.indexOf('webgl') > 0 && 
        window.location.search.indexOf('webgl=false' < 0)) {
      map = "luciad/view/WebGLMap";
    } else {
      map = "luciad/view/Map";
    }

    require([map], function(value) {
      onload(value);
    });
  }
});

If you wanted to include both luciad/ modules in the build, then the r.js if statement could call require(['luciad/view/WebGLMap', 'luciad/view/Map'], function() { onload(); }) instead, to get those modules into the r.js-generated bundles.

Closing as a discussion ticket, but feel free to continue discussion here.

@jrburke jrburke closed this as completed Nov 22, 2017
@jslegers
Copy link
Author

jslegers commented Nov 22, 2017

Closing as a discussion ticket, but feel free to continue discussion here.

Thanks a lot for your swift answer.

The following code - which is based on your answer - seems to do the trick :

define({
  load: function(na, require, onload, config) {
    var WebGLMap = "luciad/view/WebGLMap";
    var RegularMap = "luciad/view/Map";
    // Require both maps when running the loader in r.js
    // The parameter passed to "onload" is not important here
    if (typeof window === 'undefined') {
      return require([WebGLMap, RegularMap], function() {
        onload();
      });
    }

    if (window.location.search.indexOf('webgl') > 0 && 
        window.location.search.indexOf('webgl=false' < 0)) {
      map = WebGLMap;
    } else {
      map = RegularMap;
    }
    require([map], function(value) {
      onload(value);
    });
  }
});

There's another issue that I'm struggling with, though...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants