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

Check if a module exists before requiring it #984

Closed
jslegers opened this issue May 18, 2018 · 3 comments
Closed

Check if a module exists before requiring it #984

jslegers opened this issue May 18, 2018 · 3 comments

Comments

@jslegers
Copy link

jslegers commented May 18, 2018

I'm working on a plugin where I need to check if module namespace/A is available on the server. If it is available, I want to load namespace/A. If it is not available, I want to load namespace/B as a fallback.

Now, depending on whether my modules are bundled, I could use an Ajax call or require.specified() to check for availability when running my code in a browser. It's far from a perfect solution, but it kinda sorta works (See requirejs/requirejs#1737).

But what should I do when running this code in r.js?

How can I figure out whether a module exists before I try requiring it?

Is there a way I can just ignore non-existing modules in r.js without breaking the build?

@jrburke
Copy link
Member

jrburke commented May 21, 2018

You could use rawText to set up a simple define for that ID for the build so that the dependency resolves for a build, then use exclude to exclude that module from the build output.

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

@jrburke jrburke closed this as completed May 21, 2018
@jslegers
Copy link
Author

jslegers commented May 22, 2018

It's unclear to me how I could address my use case with rawText and exclude.

Let me clarify my use case...

I do R&D for a JavaScript library called LuciadRIA. The "pro" version of LuciadRIA has an optional component called "military symbology" that can be included by just unzipping the content of a zip file into the LuciadRIA root directory. This adds both additional library files and additional samples.

Two of our samples use a custom AMD plugin named ModuleLoad that loads a different module depending on whether the optional "military symbology" package is present. If it's not present, you get fallback behavior.

On Friday I worked out an architecture that allows the code of this plugin (as well as another custom plugin) to be used directly in the browser and to have it optimized by r.js. However, that architecture has quite a few flaws, including causing our Jenkins build to break. I raised this as a separate issue (#983).

Anyway, I'm using Ajax to detect the presence of a module when running my modules unbundled and require.specified() when running them as a bundle (which also feels subtoptimal to me - see requirejs/requirejs#1737 for that). However, I have not found a way to do the same check when building my bundle with r.js. For that, I logged the issue you're currently reading.

Because I'm unable to do this check in r.js, I currently have to assume either that "military symbology" is present or that it is not present at build time. If I assume the former, the build will break when it is not present. If I assume the latter, modules that should be bundled are not.

@jrburke
Copy link
Member

jrburke commented May 23, 2018

For build situations, instead of using an ajax call, the plugin can detect if it is in a build, and either just do nothing, by calling the load function passed to it, or it could use node APIs, like fs.readFileSync to read files locally.

The text plugin as an example of checking the isBuild flag:
https://github.com/requirejs/text/blob/master/text.js#L182

and it also will use node APIs, like readFileSync when running in node. The text plugin is a bit complicated since it handles a few different situations, but hopefully it could give ideas on how to branch work for build situations.

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