Skip to content

Does not work when imported as a module. #202

@MicahZoltu

Description

@MicahZoltu

If you attempt to load the webextension-polyfill as a module, you get a runtime failure in both Chrome and Firefox due to the utilization of this as the global parameter. this is undefined in the context of an ES module, so when the root UMD module function is called and this is passed to the global parameter, it ends up passing undefined. Then when global.browser is referenced you get a type error.

The same problem occurs with the minified source.

Error message in Firefox:

TypeError: global is undefined

Recommendation

Use globalThis || window || this instead of this as the first parameter. I believe any browser that supports ES modules also supports either globalThis or window, so this will only be hit in the rare case.

Repro Case

<!DOCTYPE html>
<html lang='en'>
	<head>
		<meta charset='utf-8'>
	</head>
	<body>
		<script type='module' src='browser-polyfill.js'></script>
	</body>
</html>
{
	"manifest_version": 2,
	"name": "ModuleFailureRepro",
	"version": "1.0",
	"description": "Repro case for loading a polyfill as an ES module",
	"background": {
		"page": "background.html"
	}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions