0

I am using the @parcel/transformer-webextension transformer and having good success with it. I have one issue I'm trying to resolve. Firefox is not happy.

I reference jquery and bootstrap from the index page of my pop-up and options pages. I reference them both from npm, e.g.:

Parceljs is processing them into minimized (again) files with generated names. It all works properly in other browsers, but Firefox does not like this, because

  1. It doesn't realize this is jquery because the generated file's hash doesn't match; and
  2. It uses innerHtml to set values in the DOM.

If I can pass through the minimized jquery file intact, it will accept it because it will apply the hash correctly. See:

JQuery not supported anymore?

How can I configure Parceljs to leave the jquery.min.js file intact, deliver it to dist directory, and update the reference in HTML?

Thanks in advance - jlo

jlo
  • 1,121
  • 1
  • 11
  • 16
  • Can you link to a simplified reproduction, or provide enough details that I could try it out on my side? – Andrew Stegmaier Oct 19 '21 at 14:49
  • @AndrewStegmaier Here is a shell that represents what I am packaging. Both the pop-up page and the options page reference minimized jquery and bootstrap. When packaged these files are processed into indexxxxxxx.js files. I would like to deliver them both intact so the Firefox store will not complain. It checks the hash of the file and will ignore issues in minimized jquery and bootstrap files that are unchanged. https://github.com/odelljl/parcel-web-ext-example – jlo Oct 21 '21 at 13:35

1 Answers1

0

There are two ways to solve this problem.

  1. You can have your <script> tags import from a cdn, like this:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.8/jquery.min.js"></script>
    

    Parcel will leave anything that begins with "http(s)" untouched (see documentation). You'll have to set up your manifest.json so that the browser will permit an external resource to be loaded. I'm not an expert here, but I think this is a solvable problem (see, for example this question).

  2. You can set up a named pipeline that bypasses js transformer and optimizers for assets in that pipeline. For example, your .parcelrc file might look like this:

    {
      "extends": "@parcel/config-default",
      "transformers": {
        "manifest.json": ["@parcel/transformer-webextension"],
        "raw:*": ["@parcel/transformer-raw"]
      },
      "optimizers": {
        "raw:*": []
      },
      "packagers": {
        "manifest.json": "@parcel/packager-raw-url",
        "raw:*": "@parcel/packager-raw"
      }
    }
    
    

    Then, you could import jquery and bootstrap like this:

    <script src="raw:../../node_modules/jquery/dist/jquery.min.js"></script>
    <script src="raw:../../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    

    Note that the script imports do not have type: "module" set. In my tests, this is important, because if parcel sees type="module", it will try to scope-hoist, leading to errors. I think this is also correct from the browser's perspective since these are not module bundles.

    In my tests, the above configuration will give you two files (bootstrap.bundle.min.[hash].js and jquery.min.[hash].js in your dist directory. The files have identical contents with the corresponding ones in node_modules. I don't think Firefox will complain about parcel adds a [hash] to their name, as long as the contents are the same (see here).

Andrew Stegmaier
  • 3,429
  • 2
  • 14
  • 26
  • 1. I thought of this approach - but I had the extension loading clean in all the other app stores, including Apple which is very finicky, and I didn't want to deal with getting all the headers correct. That is an option but I'd prefer to keep this otherwise self-contained extension self-contained. 2. All I was missing was the raw prefix in the NTML in my example - thanks for that! However, it seems changing the name still trips up Firefox. The bootstrap _Unsafe assignment to innerHTML ... _webext-prod/bootstrap.bundle.min.1d0f0483.js line 6 column 64112_ – jlo Oct 21 '21 at 17:53
  • Ah shoot - I just noticed that parcel adds these lines at the end of `bootstrap.bundle.min.[hash].js`: `//# sourceMappingURL=bootstrap.bundle.min.js.map`. That probably changes the hash of the file - I wonder if that's actually the problem, and not the name - can you test? – Andrew Stegmaier Oct 21 '21 at 18:41
  • You read my mind - I did just test this. When I replace the jquery file with the minified file directly downloaded by npm, it accepts it with a minor complaint: Known JS library detected Warning: JavaScript libraries are discouraged for simple add-ons, but are generally accepted. jquery.min.js – jlo Oct 21 '21 at 18:54
  • I figured out how to remove those source map lines - just add `"raw:*": "@parcel/packager-raw"` to the `packages` section of `.parcelrc` (I updated the answer to include this). Does that work to make firefox happy? – Andrew Stegmaier Oct 21 '21 at 18:55
  • Scratch that - I goofed again. The problem is actually that the sourcemap comment is _present_ in the original `bootstrap.bundle.min.js.map` file, but _not_ in the parcel output. I'm trying to figure out why parcel does this, and if it's possible to tell it not to... – Andrew Stegmaier Oct 21 '21 at 19:15
  • Yes - it still did not work. I have no issue with running a post-process to copy the files directly from npm download ... is there a way to effectively tell parcel not to process the file at all and leave my script tags intact? – jlo Oct 21 '21 at 19:23
  • In one of the support posts ... they claim: "If you copy the code and paste it in the new file, it will change its hash." I wonder if copying the original file is the only solution. – jlo Oct 21 '21 at 19:29
  • I think I'm stumped - maybe it's best to file a github issue asking for either (1) a way to tell parcel to just ignore a particular ` – Andrew Stegmaier Oct 21 '21 at 19:47
  • I guess as a workaround, you could build with a url that references the bundle on the cdn (which parcel will leave untouched), then write a script that copies over the original files, and does a find/replace of the CDN url to point to the copied files. – Andrew Stegmaier Oct 21 '21 at 19:50
  • Thanks for investigating - I think I have a solution around using parcel-reporter-static-files-copy to copy the files and parcel-resolver-ignore to leave my HTML script references alone. I'm close just trying to get the configuration squared away. – jlo Oct 21 '21 at 20:32