Skip to content

Instantly share code, notes, and snippets.

@juliandescottes
Last active April 26, 2024 23:01
Show Gist options
  • Save juliandescottes/169f3a57e1441542565b to your computer and use it in GitHub Desktop.
Save juliandescottes/169f3a57e1441542565b to your computer and use it in GitHub Desktop.
Devtools file loading
╔═════════════════════════════════════════════════════════════════════════════════╗
β•‘ 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