parcel-bundler / parcel

The zero configuration build tool for the web. 📦🚀
https://parceljs.org
MIT License
43.38k stars 2.27k forks source link

Web Extensions: duplicate bundles when importing the same file via `import` and `manifest.json` #9446

Open fregante opened 9 months ago

fregante commented 9 months ago

🐛 bug report

In my extension, I mention the same file in the manifest and in a JS file. This causes Parcel to bundle the same file twice with two outputs. This in turn causes nearly every sub-import to also be duplicated. In this case the duplicate file was source/index.html (note that this screenshot was made after trash distribution && parcel build)

Screenshot

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

{
    "extends": "@parcel/config-webextension",
    "transformers": {
        "*.svelte": ["parcel-transformer-svelte"]
    }
}
{
    "@parcel/resolver-default": {
        "packageExports": true
    }
}

🤔 Expected Behavior

The file should not be duplicated.

😯 Current Behavior

Two html files appear in dist

💻 Code Sample

{
    "name": "name",
    "description": "desc",
    "version": "0.0.0",
    "manifest_version": 3,
    "action": {
        "default_popup": "index.html"
    }
    "background": {
        "service_worker": "background.js",
        "type": "module"
    },
}
// background.js

import appUrl from 'url:./index.html';
console.log(appUrl);

Full repro can be found in this PR (until commit 1009154)

Note that in my case, the manifest actually mentions index.html?type=popup, which is valid and could further cause issues, but the same bug appears even without the search parameter.

🌍 Your Environment

Software Version(s)
Parcel 2.10.3
Node v18.18.2
npm/Yarn 9.8.1
Operating System macOS
fregante commented 9 months ago

I also tried using the same exact path in both, to no avail:

import appUrl from 'url:./index.html';
    "action": {
        "default_icon": "logo.png",
        "default_popup": "./index.html"
    },
mischnic commented 9 months ago

Does new URL("index.html", import.meta.url) cause the same problem?

fregante commented 9 months ago

Yes. I also tried matching the exact string in both ways: index.html, ./index.html

fregante commented 2 months ago

Still reproducible with today's canary. Sub-dependencies are also duplicated:

contents are 100% match except these import paths.

fregante commented 1 month ago

I'm seeing the same issue with just manifest.json in the latest canary after this was fixed:

The repro:

{
    "background": {
        "type": "module"
        "service_worker": "background.js",
        "scripts": [
            "background.js"
        ]
    },
}

The generated background.29017fde.js and background.e1bfa0d5.js are identical except the internal module IDs