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.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this commit fixes the issue with firefox's dynamic importation problem: Support Firefox #3.
- add `content_script_extension_adapter.ts`, which now takes the role of being the actual loaded `content_script`, and it dynamically imports `content_script.ts` through the use of `browser.runtime.getURL`, which is the only acceptable way of importing in firefox, as firefox does not allow the use of relative url constant string import. the imported url has to be absolute.
- make `content_script.ts` import statically, since dynamic import is no longer required by it, as it is no longer the direct entry point of the actual `content_script`.
- this also reduces our code-splitting-chunk output files from 3 to just 1
- in `manifest.json`:
- reduce the `web_accessible_resources` matches url to just github, since we do not want other websites to be able to fetch `GET` and sniff our `".js"` files from untrusted domains, should they somehow be able to figure out our extension's UUID.
- add `browser_specific_settings`, which is the only way to get storage access permission in firefox when it is loaded as a development extension. I don't know if this key is necessary for publication though.
/** `content_script`s cannot import through esm module import syntax.
4
+
* thus, we have to resort to dynamic `import()` in an async context, in addition to awaiting for it.
5
+
*
6
+
* furthermore, the imported script MUST have external-resource security clearance to be imported here.
7
+
* for that, we have to specify in "manifest.json": `web_accessible_resources = [{resources: ["*.js"], matches: "<all_urls>"}]`.
8
+
* which is basically saying: javascript within "<all_urls>" in this extension can load the resource url_pattern "*.js" (all javascript files).
9
+
*
10
+
* finally, firefox cannot dynamically import with a relative url (relative to this script's location in the extension's root path).
11
+
* moreover, you cannot use `import.meta.url` to figure out this script's url, because `content_script`s are not loaded as esm modules, but rather as classical scripts.
12
+
* so you have to use `browser.runtime.getURL("/your/module/to_load.js")` with the absolute path of the module (relative to the extension's root url) to get its url.
13
+
* in chromium, this isn't an issue, and you can do relative imports and the browser will understand that it's relative to this script's location in the extension.
14
+
* to summarize here's what's valid:
15
+
* - in chromium: `import("./content_script.ts")`
16
+
* - esbuild understands dynamic imports when they are a constant string expression, so `./content_script.ts` will also get bundled and transformed automatically
17
+
* - in firefox and chromium: `import(chrome.runtime.getURL("/js/content_script.js"))`
18
+
* - esbuild cannot evaluate this kind of dynamic string expression, so it will ignore it and leave it as is.
19
+
* that's why we'll have to reference it through its compiled name instead of the source file name.
0 commit comments