`Import` root path in a Firefox extension

I have the following extension folder structure.

.
├── background
│   ├── page.html
│   └── src
│       ├── other_script.js
│       └── script.js
└── manifest.json

In manifest.json, I'm loading page.html:

{
    // ...

    "background":
    {
        "page": "background/page.html"
    }
}

page.html itself loads a script:

<script type="module" src="src/script.js"></script>

And that script then imports other files in my directory structure:

// Inside script.js
import data from "/background/src/other_script.js";

Here's what I am wondering. The path prefixed by / in the import statement above seems to be relative to my extension directory (that is, where manifest.json is located). I am curious if:

  1. I can change what directory / is relative to, for example to /background/src, so that the following is valid:
import data from "/other_script.js";

or,

  1. I can define an extension-wide variable such as SRC so that I can rewrite the import as follows:
import data from `${SRC}/other_script.js`;


Read more here: https://stackoverflow.com/questions/67014802/import-root-path-in-a-firefox-extension

Content Attribution

This content was originally published by Lua at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: