Cropster / ember-l10n

A GNU gettext based localization workflow for Ember
MIT License
15 stars 7 forks source link

Ensure it works with Fastboot #60

Closed mydea closed 4 years ago

mydea commented 4 years ago

This PR restores FastBoot compatibility. It is heavily inspired by https://github.com/adopted-ember-addons/ember-cli-ifa/pull/57/files.

There are basically two main issues that need to be worked around for FastBoot:

  1. We cannot load the asset map via document.querySelector(...), as document is not available in Node
  2. We cannot load the locale files (e.g. en.json) via XMLHttpRequest, as that is not available in Node

For 1.), I used the same approach as the one in the references PR to ember-cli-ifa, to build a custom module for Node which can be requested in FastBoot environment (this is encapsulated in the new utils/get-locale-asset-map.js file).

For 2.), we embed the actual locale file content in the asset map, instead of the path to the asset map, for the FastBoot module. This way, we avoid needing to make any Ajax requests in FastBoot environment.

FastBoot will also generate a package.json file in the output, which needs to be excluded from fingerprinting. This PR will automatically adjust the fingerprinting options to work with ember-l10n now, including adding json to fingerprint.extensions for you. The user is informed about any changes made to the configuration in the console, e.g.:

ember-l10n automatically adjusted the fingerprinting settings to work properly:
* added 'json' to fingerprint.extensions
* added 'package.json' to fingerprint.exclude

Note that it does not actually add FastBoot as a dev dependency, and does not add any FastBoot tests for now. I did install FastBoot locally and tried it, to ensure it works as expected (which it did for me).

Fixes #59