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

clarify doc on load()'s parentRequire parameter #1445

Closed
wkeese opened this issue Oct 30, 2015 · 4 comments
Closed

clarify doc on load()'s parentRequire parameter #1445

wkeese opened this issue Oct 30, 2015 · 4 comments

Comments

@wkeese
Copy link

wkeese commented Oct 30, 2015

The parentRequire parameter passed to a plugin's load() method (and also writeFile() method?) seems to run in the context of the code that called the plugin, rather than in the plugin's context. I presume that's intentional. However, the documentation simply defines parentRequire as:

A local "require" function to use to load other modules.

So, you should probably update the doc.

You can confirm the current behavior by sticking a relative MID in your plugin. For example:

load: function (mid, require, onload, loaderConfig) {
    require(["./foo"], function () {
        ...
},

This tries to load foo from the directory of the code that called the plugin, rather from the directory where the plugin lives.

I also tested that a map configuration on the code that calls the plugin affects how that parentRequire() method works.

@jcrben
Copy link

jcrben commented Nov 15, 2015

Seems reasonable to tweak the docs. End users of plugins are thinking more in terms of the baseUrl of their main.js file so it seems intuitive to load relative to that.

@chrisknoll
Copy link

The thing that bothers me about the current behavior is that the same plugin will load a different file based on the module that called the plugin. This means in some cases the require(["./foo"]) will pull one module to load from one location, and another plugin call will cause require(["./foo"]) to load a different one. If a plugin is going to load a module, shouldn't it always know what module it is going to load, and not base it on the module context that called it?

jrburke added a commit that referenced this issue Nov 15, 2015
@jrburke
Copy link
Member

jrburke commented Nov 15, 2015

Mentioned in doc that the relative IDs are relative to the module referring to the ID, updated the site.

If the loader plugin wants a require that is relative to its own ID, then it should use the require it can get from its own define() definition:

define(function(require) {
  return {
    load: function(id, parentRequire, onload, config) {
        // use require() for plugin-relative IDs
    }
  };
});

@chrisknoll
Copy link

Ah, that outer require that is now scoped to the inner load function makes more sense now! Thanks!

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

4 participants