danasilver/tiny-human-time

publish es5-compatible version on npm

Opened this issue · 2 comments

Hi, thanks for the package.

I am using it as a dependency on a code that gets compressed using uglify-js, and as it is it doesnt support es6 and the process fail because of the let keyword and because the use of string literals `:

Errors:

Parse error at dist/www/bundle.js:38333,6
  let milli = Math.abs(isNaN(+t2) ? t1 : t2 - t1);
      ^
SyntaxError: Unexpected token: name (milli)
    at JS_Parse_Error.Object.defineProperty.get (eval at <anonymous> (/Users/fabricio/20k/road/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:86:23)
    at /Users/fabricio/20k/road/node_modules/uglify-js/bin/uglifyjs:384:39
    at time_it (/Users/fabricio/20k/road/node_modules/uglify-js/bin/uglifyjs:620:15)
    at /Users/fabricio/20k/road/node_modules/uglify-js/bin/uglifyjs:345:9
    at tryToString (fs.js:449:3)
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:436:12)
Parse error at dist/www/bundle.js:38354,9
  return `${n + (mode === 'short' ? '' : ' ')}${unit}${mode === 'short' || n ===
         ^
SyntaxError: Unexpected character '`'
    at JS_Parse_Error.Object.defineProperty.get (eval at <anonymous> (/Users/fabricio/20k/road/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:86:23)
    at /Users/fabricio/20k/road/node_modules/uglify-js/bin/uglifyjs:384:39
    at time_it (/Users/fabricio/20k/road/node_modules/uglify-js/bin/uglifyjs:620:15)
    at /Users/fabricio/20k/road/node_modules/uglify-js/bin/uglifyjs:345:9
    at tryToString (fs.js:449:3)
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:436:12)

Maybe we can either just replace those two lines with var and regular strings, or add a npm script to run the lib through babel with es2020 preset before publishing to npm.

I can create a PR for either option if you want :)

one workaround for people creating bundles with browserify is to use viralify to inject your es6 transform(es2020, es2040, babelify) in the package, something like this should work:

`yarn bin`/viralify . -t es2040 -p tiny-human-time

Hi @fczuardi! Glad you're using Tiny Human Time!

Thanks for bring this to my attention. Sorry it took me a while to respond. It seems like publishing ES5 code to npm is still the norm. Since converting a few declarations and strings is pretty easy, there are no dependencies, and it'll be compatible with the newer Node versions we already support, I think we should convert to both ES5 source and published code (I would like to keep those the same).

If you're still up for it, I'd be happy to review a PR!