Problem/Motivation

It looks like aggregating several JavaScript files can cause a conflict in the name space, as was seen in #3410672: Multilayer map breaks with JavaScript aggregation when using Maplibre libraries, which this issue is based on:

After Leaflet 10.2.6 where maplibre-gl-js & maplibre-gl-leaflet libraries js and css files was embedded in the Leaflet module itself, if you use the Maplibre libraries for example for Vector tiles, and you aggregate JavaScript files, you get a white page, and this message in the console:

Uncaught SyntaxError: redeclaration of const a
note: Previously declared at line 585, column 124988 
js_36Cc25ySfc_bCPZPMGkgRO6_2OjFQNpZD9P35kGaiS8.js:585:125091
js_36Cc25ySfc_bCPZPMGkgRO6_2OjFQNpZD9P35kGaiS8.js:585:124988

Steps to reproduce

NOTE: This issue was resolved in the Leaflet module version 10.2.7 by excluding the maplibre-gl.js from aggregation.

To replicate the problem:

  1. Make the module always load the problematic Maplibre GL library, by changing this line:

    $attached_libraries = ['leaflet/general', 'leaflet/leaflet-drupal'];

    To this:

    $attached_libraries = ['leaflet/general', 'leaflet/leaflet-drupal', 'leaflet/maplibre-gl-leaflet'];

    https://git.drupalcode.org/project/leaflet/-/blob/10.2.x/src/LeafletServ...
  2. To force aggregating the Maplibre library, remove

    { preprocess: false }

    https://git.drupalcode.org/project/leaflet/-/commit/3bd02997151a551f293a...

... and then:

  1. Add a Geofield to a content type, and use Leaflet to show the map
  2. Toggle JavaScript aggregation
  3. See that the map doesn't render with aggregation enabled

Proposed resolution

Figure out how to allow aggregating all JavaScript files.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

ressa created an issue. See original summary.

ressa’s picture

Issue summary: View changes

Add a step, which may not be obvious to non-Leaflet users: "Add a Geofield to a content type, and use Leaflet to show the map".

ressa’s picture

Issue summary: View changes

Make it simpler to replicate, by not requiring a custom module.

catch’s picture

Component: aggregator.module » asset library system

fwiw aggregator.module is the feed reader that's no longer in Drupal core.

catch’s picture

Excluding those files from aggregation seems like the right fix to me. I'm not sure there's a way for the aggregation system to fix this if they're both declaring global constants.

ressa’s picture

Title: Aggregation of JavaScript files can cause redeclaration » Document that aggregation of JavaScript files can cause redeclaration
Category: Bug report » Task
Status: Active » Closed (works as designed)

That was also my hunch, since starting to rename constants or variables can easily end up being messy. I added a note in Checklist for launching a site. Ideally, a dedicated Performance page under https://www.drupal.org/docs/administering-a-drupal-site/managing-site-pe... would be best.

PS. Thanks for correcting the category. Wouldn't it be great if each Component had a short description, or page which listed the content under that Component? I'll create an issue.

ressa’s picture

ressa’s picture

Status: Closed (works as designed) » Needs work

I created a new Aggregate CSS and JS files in Drupal core page under "Performance", adding a workaround to the JS aggregation problem.

I hope someone can add a menu point to it, and maybe place it at the top, before the "Blazy" contrib module solution? (Changing status, to hopefully get this done)

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.