Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localization using ES6 features leads to minify failure #1796

Closed
afelicioni opened this issue May 20, 2020 · 6 comments
Closed

Localization using ES6 features leads to minify failure #1796

afelicioni opened this issue May 20, 2020 · 6 comments

Comments

@afelicioni
Copy link

Hello, using date-fns for an existing project built via https://create-react-app.dev/

At build stage I am facing a failure as described on https://create-react-app.dev/docs/troubleshooting/#npm-run-build-fails-to-minify since used react-script version in project is less than 2.

Current affected version is v2.14.0 that merged code from #1765 two days ago; my temporary solution is to freeze to v2.13.0 package version, but since just one file raises the failure, it could be just written in a legacy flavor.

@afelicioni
Copy link
Author

afelicioni commented May 20, 2020

Just to provide an hint, the file is src/locale/hu/_lib/formatDistance/index.js; avoiding the computed property names in a object initialization, minification would be successful with the following code variant

var translations = {
  about: 'körülbelül',
  over: 'több mint',
  almost: 'majdnem',
  lessthan: 'kevesebb mint'
};
var withoutSuffixes = {
  xseconds: ' másodperc',
  halfaminute: 'fél perc',
  xminutes: ' perc',
  xhours: ' óra',
  xdays: ' nap',
  xweeks: ' hét',
  xmonths: ' hónap',
  xyears: ' év'
};
var withSuffixes = {
  xseconds: {
    '-1': ' másodperccel ezelőtt',
    '1': ' másodperc múlva',
    '0': ' másodperce'
  },
  halfaminute: {
    '-1': 'fél perccel ezelőtt',
    '1': 'fél perc múlva',
    '0': 'fél perce'
  },
  xminutes: {
    '-1': ' perccel ezelőtt',
    '1': ' perc múlva',
    '0': ' perce'
  },
  xhours: {
    '-1': ' órával ezelőtt',
    '1': ' óra múlva',
    '0': ' órája'
  },
  xdays: {
    '-1': ' nappal ezelőtt',
    '1': ' nap múlva',
    '0': ' napja'
  },
  xweeks: {
    '-1': ' héttel ezelőtt',
    '1': ' hét múlva',
    '0': ' hete'
  },
  xmonths: {
    '-1': ' hónappal ezelőtt',
    '1': ' hónap múlva',
    '0': ' hónapja'
  },
  xyears: {
    '-1': ' évvel ezelőtt',
    '1': ' év múlva',
    '0': ' éve'
  }
};

function translate(number, addSuffix, key, comparison) {
  var translated = addSuffix ? withSuffixes[key][String(comparison)] : withoutSuffixes[key];

  if (key === 'halfaminute') {
    return translated;
  }

  return number + translated;
}

export default function formatDistance(token, count, options) {
  options = options || {};
  var adverb = token.match(/about|over|almost|lessthan/i);
  var unit = token.replace(adverb, '');
  var result;
  result = translate(count, options.addSuffix, unit.toLowerCase(), options.comparison);

  if (adverb) {
    result = translations[adverb[0].toLowerCase()] + ' ' + result;
  }

  return result;
}

@JMartelot
Copy link

JMartelot commented May 27, 2020

Hi, I'm facing same issue using last version of date-fns with IE11.
Seems that negative indices in array are not well support.

Fixing temporarily my date-fns dependency to v2.13.0 solved the problem.

@lolu-decerno-gbg
Copy link

lolu-decerno-gbg commented Jun 2, 2020

@jmate0321 same issue for me with IE11, was about to poly-fill but the downgrade to v2.13.0 solved my problems as well.

@vbraun
Copy link

vbraun commented Jun 19, 2020

Also breaks uglify-js

The { [-1]: "foo" } syntax is definitely ES6, thats rather cutting edge for a basic library. I looked but didn't find any clear documentation for what JS spec is required by date-fns.

@afelicioni
Copy link
Author

@gottsohn published PR #1842 that shall fix this.

@afelicioni
Copy link
Author

since this issue has been solved in released v2.15.0, it hasn't any reason to be open anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants