fix: prevent error in service worker environment #384
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In Chromium extensions under Manifest Version 3, the project throws an error:
![Screenshot 2024-06-18 at 3 27 00 PM](https://private-user-images.githubusercontent.com/3326734/340900451-5d32f12d-a44c-4416-a0da-d7206a94d63f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIxMTg1NDMsIm5iZiI6MTcyMjExODI0MywicGF0aCI6Ii8zMzI2NzM0LzM0MDkwMDQ1MS01ZDMyZjEyZC1hNDRjLTQ0MTYtYTBkYS1kNzIwNmE5NGQ2M2YucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDcyNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA3MjdUMjIxMDQzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MmU5MmVmMDAyYTc2NzBkZWE5YTIxYWFmYWQzMDQzZTJmNWU2NWYzMWExNGIwNjRhM2I0ZmIwYjljYmQwODM0ZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.W9GHi2NGOvQJLIsXXuAqKO5qv_VmGe-r7oDnJgMdhx8)
This is because in service workers,
window
is not defined, soisBrowser
is false, butprocess
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'
becomestypeof ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.TXWRAPPER_METADATA_CACHE_MAX) !== 'undefined'
, whichDefinePlugin
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.