paritytech / txwrapper-core

Tools for FRAME chain builders to publish chain specific offline transaction generation libraries.
https://paritytech.github.io/txwrapper-core/
Apache License 2.0
77 stars 28 forks source link

fix: prevent error in service worker environment #384

Closed chidg closed 4 months ago

chidg commented 4 months ago

In Chromium extensions under Manifest Version 3, the project throws an error:

Screenshot 2024-06-18 at 3 27 00 PM

This is because in service workers, window is not defined, so isBrowser is false, but process is also not defined by default.

This could not be fixed by webpack's DefinePlugin because of the optional chaining; the optionally-chained expression actually gets transpiled to something quite different in the built package - typeof process?.env?.TXWRAPPER_METADATA_CACHE_MAX !== 'undefined' becomes typeof ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.TXWRAPPER_METADATA_CACHE_MAX) !== 'undefined', which DefinePlugin can't parse.

Currently in Talisman as we transition to MV3 we're dealing with this by monkey patching a process global variable into the service worker, however we'd like to avoid this if possible.

TarikGul commented 4 months ago

@chidg Just needs yarn lint:fix ran on it.

chidg commented 4 months ago

Thanks for the quick review and merge!