I’ve come across a compatibility issue when importing the solclient.js
library as an ES Module:
import './path/to/solclient.js';
Problem:
Importing the UMD build of solclient.js
into an ES Module Service Worker (e.g., Chrome extension MV3 with "type": "module"
set in the manifest.json
file) fails. The worker won’t register, often showing TypeError: Cannot set properties of undefined (setting 'solace')
originating from solclient.js
.
Cause:
The library’s UMD wrapper uses this
to find the global object. In ES Modules, top-level this
is undefined
, breaking the UMD’s global fallback logic (root["solace"] = factory();
fails because root
is undefined
).
Workaround (Patch solclient.js
):
- Locate UMD Invocation: Find the code that passes
this
as the global object (root
) to the UMD wrapper.- solclient-full.js (non-minified) (4th line):
/*! For license information please see solclient-full.js.LICENSE.txt */
!function(root, factory) {
if ("object" === typeof exports && "object" === typeof module) module.exports = factory(); else if ("function" === typeof define && define.amd) define("solace", [], factory); else if ("object" === typeof exports) exports["solace"] = factory(); else root["solace"] = factory();
}(this, () => {
return (() => {
- solclient.js (minified):
exports.solace=t():e.solace=t()}(this,(()=>(()=>{var e
-
Modify: Change the final
this
in that specific spot toself
.- solclient-full.js (non-minified):
!function(root, factory) {
if ("object" === typeof exports && "object" === typeof module) module.exports = factory(); else if ("function" === typeof define && define.amd) define("solace", [], factory); else if ("object" === typeof exports) exports["solace"] = factory(); else root["solace"] = factory();
}(self, () => {
return (() => {
- solclient.js (minified):
exports.solace=t():e.solace=t()}(self,(()=>(()=>{var e
- Import & Use in Worker:
- In your module worker (or modules it imports), trigger the script execution:
import '../lib/solclient.js';
- Access the API via the worker global
self
:const factoryProps = new self.solace.SolclientFactoryProperties(); let session = self.solace.SolclientFactory.createSession(...); session.on(self.solace.SessionEventCode.UP_NOTICE, ...);
- In your module worker (or modules it imports), trigger the script execution:
Why it works: This patch makes the UMD correctly identify the worker’s global scope (self
), allowing its fallback mechanism to attach the API object globally, where your module code can then access it.