Issue with Drupal.t() in js

Created on 3 October 2024, 4 months ago

Hello,

There is an issue with Drupal.t() function when `vite: true` is enable in libraries.

_locale_parse_js_file() function does'nt use manifest to find js file to parse.

I fixed this issue locally with an extra lib js without vite that declares the translation strings. Ideally, this function should be able to go through the manifest.

Thanks,

๐Ÿ› Bug report
Status

Active

Version

1.2

Component

Code

Created by

๐Ÿ‡ซ๐Ÿ‡ทFrance myriam_b

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @myriam_b
  • I don't know if it's your problem but Vite may uglify Drupal variable in your function, so you get a.t() in your uglified JS.

    And Drupal.t() needs to remain Drupal.t() even uglified
    https://drupal.stackexchange.com/a/284198/106294

  • ๐Ÿ‡ฌ๐Ÿ‡ทGreece axioteo

    This issue doesn't appear to be caused by the Drupal module or Vite independently but rather seems to be a "side-effect" of using the two together. Specifically, as @daymoz pointed out, the problem seems related to how Vite performs JavaScript uglification and thus not respecting Drupal.t as a reserved function name. I was under the impression that Vite could be configured (using mangleProps etc in esbuild configurations) to exclude certain expressions or patterns from uglification, but any attempts to achieve this have not been successful so far.

    With this plugin, thereโ€™s no need to extract locale variables into a standalone library excluded from Vite. Instead, you can continue to use Drupal.t() as you would in any other scenario. While the plugin hasnโ€™t been extensively tested yet, it is functioning as expected in several local setups where weโ€™ve integrated the Drupal Vite module. Feel free to check it out, and let me know if any changes need to be made.

    Example usage and installation process:
    https://www.npmjs.com/package/vite-plugin-preserve-drupal-t

Production build 0.71.5 2024