Internal API: onResourceLoad

Josh Goldberg edited this page Dec 18, 2016 · 59 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.


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:


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


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.


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 (
Ryan Leach (
Rob Eisenberg (
Guy Bedford (
Burkhard Reffeling (
Duncan Wong (
Jason Strimpel (
Ean Schuessler (
Steven Devijver (sdevijver [ta] gmail [tod] com)
Pekka Lampila (
Mariusz Linkiewicz (
Denes Papp (
Brian Wyant (
Steve Mason (
Jason Mobley (
Andrew Wheelwright (
iThome Taiwan (
Dave Varon (
Mateo San Roman (
Facundo Cabrera (
Andrew Smith (
Bill Trikalinos (
James Whitney (
Ben Babics (
Ryan Gray (
Matthew Garber (
Geoff Jacobsen (
Lee Goddard (
Andriy Mykulyak (
Anup Bishnoi (
Arvind Bhardwaj (
Xavier Zwirtz(
Michael Roth(
Philip Plekhanov(
Alexey Babik (
Fahim Farook (
Kirill Berezin (
Rob Simpson (
Evgeny Vyushin (
Thomas Sörensen ((scaryxited [ta] gmail [tod] com))
Chetan Raj (
Pablo Sande (
Emil Ajdyna (
Dmitry Grechka (
Pwn (
Josh Goldberg (

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.