Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parcel WebExtension fails to bundle modules under some conditions (build + mv2, or mv3 & --no-scope-hoist) #8071

Closed
Stvad opened this issue May 9, 2022 · 23 comments

Comments

@Stvad
Copy link

Stvad commented May 9, 2022

🐛 bug report

Under some conditions/configurations Parcel fails to package dependecies into bundles and fails in runtime.
Here is a set of conditions and results I've discovered so far (see configs below for price command definitions)

🎛 Configuration (.babelrc, package.json, cli command)

.parcelrc

{
  "extends": "@parcel/config-webextension",
  "validators": {
	"*.{ts,tsx}": [
	  "@parcel/validator-typescript"
	]
  }
}

package.json parts

"watch": "parcel watch source/mv2/manifest.json --dist-dir build/dev --no-cache",
"build:chrome": "parcel build source/mv3/manifest.json --no-content-hash --no-source-maps --dist-dir build/chrome --no-cache --detailed-report 0",
"build:firefox": "parcel build source/mv2/manifest.json --no-content-hash --no-source-maps --dist-dir build/firefox --no-cache --detailed-report 0",

🤔 Expected Behavior

When I run either of the build commands above - all the dependencies are bundled

😯 Current Behavior

Some (all?) dependencies are missing from the bundle and the extension fails at load time with the error like:

Uncaught Error: Cannot find module 'lbnQL'
    at parcelRequire (background.c6210b5c.js:15:15)
    at background.c6210b5c.js:19:57
    at background.c6210b5c.js:33:3

🔦 Context

Building https://github.com/transclude-me/extension

I've noticed this problem after migrating to Parcel 2.5.0 from 2.3.2. One noteable change that accompanied that is having to use 2 manifests so both FF and Chrome can be supported. So my directory stracture is as follows now

src/
  mv2/manifest.json
  mv3/manifest.json

And code in manifest points to files up level (e.g. "service_worker": "../background.ts",).

I've tried reverting to src/manifest.json structure in case this was relevant, but it didn't make the difference.


I've also tried adding the following in package.json, in case it got reset somehow, it didn't make any difference:

	"targets": {
		"default": {
			"includeNodeModules": true
		}
	}

💻 Code Sample

transclude-me/extension@78b529e I discovered issue after this commit

🌍 Your Environment

Software Version(s)
Parcel 2.5.0
Node 17.5.0 (also tried 16.4.2)
Yarn 1.22.17
Operating System macOS 12.3.1
@Stvad
Copy link
Author

Stvad commented May 9, 2022

Manifest v3 with --no-scope-hoist is an interesting case, as in it content script is broken, but background is fine, and content script is failing to import ../options/options-storage module, which is what background script fails to import in the other broken cases 🤔

@Kjoep
Copy link
Contributor

Kjoep commented May 11, 2022

I have the same problem (at least it looks like it) and did some digging. Turns out the WebExtensionPackager is not compatile wit the code-splitting feature. If bundles get split off, it does not add the split-off bundles to the manifest. From what it looks like it only does a naive find-and-replace.

I have a simple workaround here: https://github.com/Kjoep/parcel/tree/prevent_losing_webextension_deps (use it for config-webextension). This turns off the code-splitting for web extensions.

@Stvad
Copy link
Author

Stvad commented May 12, 2022

I think it's the same issue, yeah. Looking at my broken builds - I get split bundles, and only one of the scripts makes it to manifest.

Also it seems that parcel is trying to reuse the code between background and content, which doesn't work.

@Stvad
Copy link
Author

Stvad commented May 12, 2022

Is there a way to disable code-splitting without forking parcel? 😛

tiqwab added a commit to tiqwab/scrapbox-diagramsnet-extension that referenced this issue May 20, 2022
@calclavia
Copy link

Getting the same issue here as @Stvad. Seems like web extension production build is not robust (it works well in parcel watch dev mode)?

I followed the changes from @Kjoep and simply added this to package.json and my production build seems to work.

"@parcel/bundler-default": {
  "minBundles": 10000000,
  "minBundleSize": 3000,
  "maxParallelRequests": 20
}

Command:
parcel build manifest.json --no-content-hash

Without no content hash, I get this error: Error: Cannot read properties of undefined (reading 'hashReferences')

@101arrowz
Copy link
Member

If someone could provide a minimal reproduction that would be helpful, otherwise I'll try to make one in the next few weeks.

@lukaw3d
Copy link
Contributor

lukaw3d commented Jun 7, 2022

@101arrowz here's my reproduction: https://github.com/lukaw3d/reproduce-parcel-webextension-codesplitting-broken

  • uses parcel@2.6.0 and @parcel/config-webextension@2.6.0
  • manifest v2 with two entry points: background.ts and popup.html
  • both entry points import something large (lodash)
  • users browserslist to target an old browser (Chrome 60)

background page throws Cannot find module when initializing

@101arrowz
Copy link
Member

Thanks, I've reproduced the issue and am working on fixing it now.

@Kjoep
Copy link
Contributor

Kjoep commented Jun 13, 2022

To be fair, the codesplitting does not seem very important for webextensions, as they are loaded locally, not over the network. It might be acceptable to automatically turn the feature off.

@lukaw3d
Copy link
Contributor

lukaw3d commented Jun 13, 2022

Individual files have to be smaller than 4MB when publishing Firefox extensions:

Stvad added a commit to transclude-me/extension that referenced this issue Jul 10, 2022
janstuemmel added a commit to janstuemmel/aws-role-switch that referenced this issue Jul 11, 2022
@avalanche1
Copy link

To be fair, the codesplitting does not seem very important for webextensions, as they are loaded locally, not over the network. It might be acceptable to automatically turn the feature off.

I'd say PREFERABLE

fregante added a commit to fregante/browser-extension-template that referenced this issue Jul 20, 2022
@everdimension
Copy link

@101arrowz Thank so much for starting work on the issue!
Is there any way I can help to speed up the process? Do you have a solution in mind?

@101arrowz
Copy link
Member

I haven't been able to properly look at this. For now we should probably just document the issue and add the code-splitting removal fix into the docs.

@everdimension
Copy link

@101arrowz Thank you for replying, great, that's something!

Can you please share here for now how to remove code-splitting?

@101arrowz
Copy link
Member

This comment seems to work: #8071 (comment)

@akash07k
Copy link

akash07k commented Aug 18, 2022

@101arrowz
I'm using Parcel 2.7.0 and below config doesn't work for me:

    "@parcel/bundler-default": {
        "minBundles": 10000000,
        "minBundleSize": 3000,
        "maxParallelRequests": 20
    }

I still get only 1 file: manifest-firefox.js
#8404

@jisbruzzi-velocity
Copy link

This started happening to us when we moved from 2.4.1 to 2.7.0.
We fixed it by adding the above snippet to the root of our package.json

@tdriley
Copy link

tdriley commented Sep 27, 2022

@calclavia's solution above fixed this for me in parcel v2.7.0. I did not also need to use --no-content-hash to prevent the error.

Compile-Time added a commit to Compile-Time/yt-quick-actions that referenced this issue Nov 12, 2022
When parcel bundles the extension not all modules are correctly included.
For more information see parcel-bundler/parcel#8071.
Compile-Time added a commit to Compile-Time/yt-quick-actions that referenced this issue Nov 13, 2022
When parcel bundles the extension not all modules are correctly included.
For more information see parcel-bundler/parcel#8071.
@github-actions github-actions bot added the Stale Inactive issues label Mar 27, 2023
@Stvad
Copy link
Author

Stvad commented Mar 27, 2023

probably not stale

@github-actions github-actions bot removed the Stale Inactive issues label Mar 27, 2023
@akash07k
Copy link

akash07k commented Mar 27, 2023 via email

@101arrowz
Copy link
Member

Possibly fixed by #9068 - can someone please test? I encountered a variant of this issue locally and my changes in that PR fixed it.

@fregante
Copy link
Contributor

@101arrowz I tried the repro in #8071 (comment) and this appears to have been fixed

2.6.0

built with 2.6.0.zip

dist/manifest.json                 261 B    158ms
dist/popup.ad5381fc.html           227 B    5.13s
dist/popup.8dfc8189.js             960 B    4.97s
dist/popup.be2dcf72.js         970.06 KB    4.97s
dist/popup.b32da9f1.js         972.16 KB    4.97s
dist/background.58f028a8.js        972 B    4.97s

2.10.0

built with 2.10.0.zip

dist/manifest.json                   261 B    273ms
dist/popup.e9f20549.html             150 B    565ms
dist/popup.13528c49.js         ⚠️  4.06 MB    1.25s
dist/popup.51ff9da5.js         ⚠️  4.06 MB    1.25s
dist/background.2b2318af.js    ⚠️  4.06 MB    1.24s

The output is not ideal because it generates two heavy popup.js files, but that's attributable to the module/no-module variants, which I think should just be disabled in extensions if possible.

@101arrowz
Copy link
Member

Thanks for verifying! Let's close this if the bundling failure is fixed; the module/nomodule behavior change can be handled in a separate issue.

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

No branches or pull requests