Skip to content

Internal API: onResourceLoad

suspiciousfellow edited this page Aug 31, 2023 · 60 revisions

RequireJS has an internal hook that can be used to get notifications of when a module is created/has an exports value, so that it is possible to trace the modules and their dependencies.

This is considered a private API, subject to change at any time. If you build on it, it is best to contact the requirejs list, and add your name below so that you can be notified of changes.

Note that you will only be notified about changes to the call signature for onResourceLoad, and not about changes to the arguments that are passed to the function. By using this API, you are signing up for future maintenance and changes. It is best if you start watching the commits to the project to make sure they do not affect you.

This API is used by the requirejs optimizer to trace dependencies for a build.

Usage

Implement requirejs.onResourceLoad

Create a file that implements the following signature:

requirejs.onResourceLoad = function (context, map, depArray) {
}

You do not need to return any value from the function. The return value is not used by RequireJS.

Arguments to onResourceLoad:

context

Object. This is the internal state object used by RequireJS to store all exported values and loader state. You can mostly ignore it.

map

Object. This is information about the module ID for the module that just came into existence. Properties on this object:

  • prefix: String. The name/module ID of any loader plugin that was used for this resource. It will be a normalized name -- an absolute ID, not a relative ID. This value will be null for a plain JS module.
  • name: String. The normalized name (it will be an absolute ID, not a relative ID) for this module. If this is a plugin resource, it is just the plugin resource name.
  • parentMap: Object. The map object used to resolve a relative ID for this module. Can be null, but if it exists it has the same structure as this map object.
  • url: String. The URL used to retrieve the resource. It may be a relative path.
  • originalName: String. The original name of the module as it was loaded. For the module ID of a newly created module, this is likely the same as fullName.
  • fullName: String. This is prefix!name if there is a prefix value, otherwise just name. In RequireJS 2.0 this property is called id.

depArray

Array. An array of dependencies that were specified for the module. Each item of the array is a string that is the normalized name (the absolute module ID, not a relative ID) of the dependency. In RequireJS 2.0 each item of the array is a map object.

Load after require.js

Load this file after require.js has been loaded. You can either do that via a script tag, or make sure it is the first thing loaded before other modules are traversed:

That should be it. Your implementation of onResourceLoad should be triggered for each module that is loaded and executed.

In RequireJS 2.0 there is no priority config anymore. Just manually load it in your main file before loading the other modules:

require(['onResourceLoad'], function () {
    //Do normal main loading now.
    require([], function () {});
});

Notify me of changes

Add your name in this section if you want to be notified to changes in this API.

Taka Kojima (taka.kojima@ff0000.com)
Ryan Leach (rleach@constantcontact.com)
Rob Eisenberg (rob@bluespire.com)
Guy Bedford (guybedford@gmail.com)
Burkhard Reffeling (burkhard.reffeling@gmail.com)
Duncan Wong (baduncaduncan@gmail.com)
Jason Strimpel (jstrimpel@gmail.com)
Ean Schuessler (ean@brainfood.com)
Steven Devijver (sdevijver [ta] gmail [tod] com)
Pekka Lampila (pekka.lampila@iki.fi)
Mariusz Linkiewicz (linek98@gmail.com)
Denes Papp (pppdns@gmail.com)
Brian Wyant (wyantb@gmail.com)
Steve Mason (smason82@gmail.com)
Jason Mobley (jdmobley@gmail.com)
Andrew Wheelwright (wheelwright.tech@gmail.com)
iThome Taiwan (james@mail.ithome.com.tw)
Dave Varon (varontron@gmail.com)
Mateo San Roman (sanromanxp@gmail.com)
Facundo Cabrera (cabrerafacundo@gmail.com)
Andrew Smith (andy@robotlovesyou.com)
Bill Trikalinos (billtrik@skroutz.gr)
James Whitney (james@whitney.io)
Ben Babics (bbabics@gmail.com)
Ryan Gray (ryan.is.gray@gmail.com)
Matthew Garber (matthew.garber@gmail.com)
Geoff Jacobsen (geoffjacobsen@gmail.com)
Lee Goddard (leegee@gmail.com)
Andriy Mykulyak (mykulyak@gmail.com)
Anup Bishnoi (pixelsallover@gmail.com)
Arvind Bhardwaj (bhardwajsonheight@gmail.com)
Xavier Zwirtz(me@xavierzwirtz.com)
Michael Roth(mroth@highbridgecreative.com)
Philip Plekhanov(philip@highground.com)
Alexey Babik (alexey.babik@gmail.com)
Fahim Farook (fahim@fahimfarook.me)
Kirill Berezin (enelar@exsul.net)
Rob Simpson (rsimpson2@me.com)
Evgeny Vyushin (e@vyushin.ru)
Thomas Sörensen ((scaryxited [ta] gmail [tod] com))
Chetan Raj (chetan.narian@gmail.com)
Pablo Sande (psande@gmail.com)
Emil Ajdyna (emil@ajdyna.com)
Dmitry Grechka (dmitryg@itis.cs.msu.ru)
Pwn (hi@pwn.buzz)
Josh Goldberg (joshuakgoldberg@outlook.com)
David Jordan (david.jordan@elementsuite.com)