Skip to content

Fix SW packaging in web extensions#8424

Merged
devongovett merged 2 commits into
parcel-bundler:v2from
olistic:fix-webextension-sw
Oct 24, 2022
Merged

Fix SW packaging in web extensions#8424
devongovett merged 2 commits into
parcel-bundler:v2from
olistic:fix-webextension-sw

Conversation

@olistic

@olistic olistic commented Aug 28, 2022

Copy link
Copy Markdown
Contributor

↪️ Pull Request

This PR fixes the packaging of Service Workers using ES Modules in the webextension config.

The JS transform does not support ES Modules for service workers:

context: 'service-worker',
sourceType: dep.source_type === 'Module' ? 'module' : 'script',
outputFormat: 'global', // TODO: module service worker support

Instead, it sets outputFormat to global (docs). For that reason, the type=module is removed when packaging the extension.

Related issue: #1747.

🚨 Test instructions

Here's a repo in which builds fail with 2.7.0 but pass with this PR:

https://github.com/olistic/parcel-webextension-repro

✔️ PR Todo

  • Added/updated unit tests for this change
  • Filled out test instructions (In case there aren't any unit tests)
  • Included links to related issues/PRs

@louisgv

louisgv commented Oct 3, 2022

Copy link
Copy Markdown
Contributor

@olistic how would we implement real module support when it comes to importing code into background sw? Or is that not necessary since parcel would be the "bundler" anyhow :-?... Just thinking outloud, since this PR is more of a hack to get over real-import and bundle them all into the output file (instead of using importScript/proper module import (?))

Comment thread packages/packagers/webextension/src/WebExtensionPackager.js Outdated
@olistic

olistic commented Oct 6, 2022

Copy link
Copy Markdown
Contributor Author

@olistic how would we implement real module support when it comes to importing code into background sw? Or is that not necessary since parcel would be the "bundler" anyhow :-?... Just thinking outloud, since this PR is more of a hack to get over real-import and bundle them all into the output file (instead of using importScript/proper module import (?))

Right now it's not necessary since Parcel is bundling the SW code anyhow, and since it leaves the type=module in the manifest, it's failing as what Parcel outputs isn't actually a module.

@louisgv

louisgv commented Oct 6, 2022

Copy link
Copy Markdown
Contributor

it's failing as what Parcel outputs isn't actually a module.

Yeah, so the way to actually fix that is to fix the js transformer to output service worker module... tbh I'm not sure how useful it will be, since bgsw are loaded in the background anyhow.

BTW, thanks for the patch - I incorporated it into my fork of this packager. (used in plasmo: https://github.com/PlasmoHQ/plasmo/)

@devongovett devongovett merged commit 59610c1 into parcel-bundler:v2 Oct 24, 2022
@olistic olistic deleted the fix-webextension-sw branch October 25, 2022 21:37
@olistic

olistic commented Oct 25, 2022

Copy link
Copy Markdown
Contributor Author

@devongovett Thanks for merging. Any idea when this will be released?

@devongovett

Copy link
Copy Markdown
Member

Working on testing and preparing a release soon (next few weeks hopefully). In the meantime, you could try out the nightly releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants