Last active
April 26, 2024 23:01
-
-
Save juliandescottes/169f3a57e1441542565b to your computer and use it in GitHub Desktop.
Devtools file loading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
β How to load/import a file in Firefox Devtools ? β | |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
ββββββββββββββββββββββββββ | |
β SYNC FILE LOADING β | |
ββββββββββββββββββββββββββ | |
β β β β β β β β β β β β β βββββββββββββββββββββββββββββββββ | |
Is it a JSM ? βββyesβββΆβ Use require() β (see [1]) | |
β β β β β β β β β β β β β βββββββββββββββββββββββββββββββββ | |
β | |
βno | |
βΌ | |
β β β β β β β β β β β β β βββββββββββββββββββββββββββββββββ | |
Is it a Service ? βββyesβββΆβ Use Cc["name"].getService() β | |
β β β β β β β β β β β β β βββββββββββββββββββββββββββββββββ | |
β | |
βno | |
βΌ | |
β β β β β β β β β β β β β βββββββββββββββββββββββββββββββββ | |
Need browser βββyesβββΆβ Use BrowserLoader's β | |
β environment ? β require() β (see [2]) | |
β β β β β β β β β β β β β βββββββββββββββββββββββββββββββββ | |
β | |
βno | |
βΌ | |
ββββββββββββββββββββββββββ | |
β Use require() β (see [1]) | |
ββββββββββββββββββββββββββ | |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
ββββββββββββββββββββββββββ | |
β LAZY FILE LOADING β | |
ββββββββββββββββββββββββββ | |
β β β β β β β β β β β β β ββββββββββββββββββββββββββββββββββ | |
Is it a JSM ? βββyesββΆβ Use loader.lazyImporter() β | |
β β β β β β β β β β β β β ββββββββββββββββββββββββββββββββββ | |
β | |
βno | |
βΌ | |
β β β β β β β β β β β β β ββββββββββββββββββββββββββββββββββ | |
Is it a Service ? βββyesββΆβ Use loader.lazyServiceGetter() β | |
β β β β β β β β β β β β β ββββββββββββββββββββββββββββββββββ | |
β | |
βno | |
βΌ | |
β β β β β β β β β β β β β ββββββββββββββββββββββββββββββββββ | |
Need browser β β Use BrowserLoader's β | |
β environment ? ββyesββΆβ lazyRequireGetter() β (see [2]) | |
β β β β β β β β β β β β β ββββββββββββββββββββββββββββββββββ | |
β | |
βno | |
βΌ | |
ββββββββββββββββββββββββββ | |
β Use loader. β | |
β lazyRequireGetter() β | |
ββββββββββββββββββββββββββ | |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
What is loader, require, lazyRequireGetter, etc... ? | |
Loaders | |
======== | |
Two loaders are available in DevTools : | |
* DevToolsLoader (devtools/shared/Loader.jsm) | |
* BrowserLoader (devtools/client/shared/browser-loader.js) | |
Both provide a `require()` method to load dependencies synchronously. BrowserLoader | |
can load files in a browser environment (window available etc...). | |
They also provide various helpers for lazy loading/getting : | |
* `lazyRequireGetter()`: lazy load a js/jsm using the loader's require. | |
Dependency is loaded first time the corresponding property is accessed. | |
* `lazyGetter()`: helper to replace a property by the return value of a provided | |
callback | |
* `lazyImporter()`: helper to lazy load a module relying on Cu.import (not | |
sure how useful this is compared to lazyRequireGetter) | |
* `lazyServiceGetter()`: helper to lazily retrieve a Service | |
Depending on how the file was loaded, it may have access to the loader API directly | |
or not. | |
File loaded via require() | |
========================= | |
If the file was loaded via require(), the following globals will be available: | |
* require: mapped to the current loader's require. Synchronously loads a js/jsm. | |
* loader: NOT the loader instance, but has all the lazySomething helpers : | |
loader.lazyRequireGetter(), loader.lazyImporter(), etc... | |
You should be able to rely on the helpers above to load any dependency needed (see | |
chart below). | |
Files NOT loaded using require() | |
================================ | |
In this case, `require` and `loader` will not be available straight away. The | |
easiest is probably to import the DevToolsLoader : | |
> const { loader } = Components.utils.import( | |
> "resource://devtools/shared/Loader.jsm", {}); | |
This loader is an instance of `DevToolsLoader`. | |
If a browser environment is needed, instantiate a `BrowserLoader` instead: | |
> const { BrowserLoader } = Components.utils.import( | |
> "resource://devtools/client/shared/browser-loader.js", {}); | |
> const browserLoader = BrowserLoader( | |
> "resource://{base/uri/for/browser/modules}", window); | |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
βNote [1] β | |
β β | |
βAs explained in the introduction, `require()` is either available as a global, β | |
βor has to be used on a loader instance: `loader.require()`. β | |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
βNote [2] β | |
β β | |
βBrowserLoader's `require` and `lazyRequireGetter`. If you are in a file or β | |
βmodule loaded via `BrowserLoader`, you can use the global require and β | |
βloader.lazyRequireGetter. β | |
β β | |
βIf not, a BrowserLoader has to be loaded and instantiated. β | |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
βNote [3]: Some other loading methods found in devtools code : β | |
β β | |
β* Cu.import or Component.utils.import : sometimes used to load synchronously β | |
βjs/jsm files. If a loader is already available, this can normally be replaced β | |
βusing this loader's require() β | |
β β | |
β* XPCOMUtils.defineLazyModuleGetter : same as loader.lazyImporter β | |
β* XPCOMUtils.defineLazyServiceGetter : same as loader.lazyServiceGetter β | |
β* XPCOMUtils.defineLazyGetter : same as loader.lazyGetter β | |
β β | |
βPrefer the loader methods over XPCOMUtils. β | |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment