Commit 8eb8c854 authored by Nicolas Chevobbe's avatar Nicolas Chevobbe
Browse files

Bug 1672930 - [devtools] Handle Node environment in l10n module. r=ladybenko.

In most of our node tests (jest and mocha), we need to rely on fixtures for the
l10n modules because in its current shape, the module couldn't load the properties
files properly.
This patch fixes that by detecting if we're in a Node environment, and in such
case load the file using `readFileSync`.
We are also able to remove the specific code we had for webpack as it doesn't
seem to be used anymore.

The Spectrum.js file needed to be updated to fix the paths it was using.

Differential Revision: https://phabricator.services.mozilla.com/D94568
parent f9a841a3
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -22,9 +22,8 @@ loader.lazyRequireGetter(
);

const L10N = new MultiLocalizationHelper(
  "devtools/shared/locales/en-US/accessibility.properties",
  "devtools/client/locales/en-US/accessibility.properties",
  "devtools/client/locales/en-US/inspector.properties"
  "devtools/client/locales/accessibility.properties",
  "devtools/client/locales/inspector.properties"
);
const ARROW_KEYS = ["ArrowUp", "ArrowRight", "ArrowDown", "ArrowLeft"];
const [ArrowUp, ArrowRight, ArrowDown, ArrowLeft] = ARROW_KEYS;
+27 −56
Original line number Diff line number Diff line
@@ -8,45 +8,6 @@ const { sprintf } = require("devtools/shared/sprintfjs/sprintf");

const propertiesMap = {};

// We need some special treatment here for webpack.
//
// Webpack doesn't always handle dynamic requires in the best way.  In
// particular if it sees an unrestricted dynamic require, it will try
// to put all the files it can find into the generated pack.  (It can
// also try a bit to parse the expression passed to require, but in
// our case this doesn't work, because our call below doesn't provide
// enough information.)
//
// Webpack also provides a way around this: require.context.  The idea
// here is to tell webpack some constraints so that it can include
// fewer files in the pack.
//
// Here we introduce new require contexts for each possible locale
// directory.  Then we use the correct context to load the property
// file.  In the webpack case this results in just the locale property
// files being included in the pack; and in the devtools case this is
// a wordy no-op.
const reqShared = require.context(
  "raw!devtools/shared/locales/",
  true,
  /^.*\.properties$/
);
const reqClient = require.context(
  "raw!devtools/client/locales/",
  true,
  /^.*\.properties$/
);
const reqStartup = require.context(
  "raw!devtools/startup/locales/",
  true,
  /^.*\.properties$/
);
const reqGlobal = require.context(
  "raw!toolkit/locales/",
  true,
  /^.*\.properties$/
);

// Map used to memoize Number formatters.
const numberFormatters = new Map();
const getNumberFormatter = function(decimals) {
@@ -73,25 +34,35 @@ const getNumberFormatter = function(decimals) {
 */
function getProperties(url) {
  if (!propertiesMap[url]) {
    // See the comment above about webpack and require contexts.  Here
    // we take an input like "devtools/shared/locales/debugger.properties"
    // and decide which context require function to use.  Despite the
    // string processing here, in the end a string identical to |url|
    // ends up being passed to "require".
    const index = url.lastIndexOf("/");
    // Turn "mumble/locales/resource.properties" => "./resource.properties".
    const baseName = "." + url.substr(index);
    let reqFn;
    if (/^toolkit/.test(url)) {
      reqFn = reqGlobal;
    } else if (/^devtools\/shared/.test(url)) {
      reqFn = reqShared;
    } else if (/^devtools\/startup/.test(url)) {
      reqFn = reqStartup;
    let propertiesFile;
    let isNodeEnv = false;
    try {
      // eslint-disable-next-line no-undef
      isNodeEnv = process?.release?.name == "node";
    } catch (e) {}

    if (isNodeEnv) {
      // In Node environment (e.g. when running jest test), we need to prepend the en-US
      // to the filename in order to have the actual location of the file in source.
      const lastDelimIndex = url.lastIndexOf("/");
      const defaultLocaleUrl =
        url.substring(0, lastDelimIndex) +
        "/en-US" +
        url.substring(lastDelimIndex);

      const path = require("path");
      // eslint-disable-next-line no-undef
      const rootPath = path.join(__dirname, "../../");
      const absoluteUrl = path.join(rootPath, defaultLocaleUrl);
      const { readFileSync } = require("fs");
      // In Node environment we directly use readFileSync to get the file content instead
      // of relying on custom raw loader, like we do in regular environment.
      propertiesFile = readFileSync(absoluteUrl, { encoding: "utf8" });
    } else {
      reqFn = reqClient;
      propertiesFile = require("raw!" + url);
    }
    propertiesMap[url] = parsePropertiesFile(reqFn(baseName));

    propertiesMap[url] = parsePropertiesFile(propertiesFile);
  }

  return propertiesMap[url];