SeverinDK / moment-timer

Timer plugin for Moment.js that allows creation of setInterval and setTimeout-like timers.
MIT License
110 stars 33 forks source link

ES6 Import #4

Closed Alxmerino closed 7 years ago

Alxmerino commented 7 years ago

Hi thanks for building this amazing plugin!

Right now I'm having an issue where I try to import this using Browserify with the ES2015 preset and the plugin is not registering with moment.

This currently doesn't work

import moment from 'moment'
import 'moment-timer'

This throws a TypeError

var timer  moment.duration(1, 'seconds').timer()

Uncaught TypeError: _moment2.default.duration(...).timer is not a function

I'm using React as well

Thanks!

SeverinDK commented 7 years ago

Hi. Thank you for the feedback! I will look into this as soon as possible. If you come up with a solution for it, feel free to create a pull request.

HyperSimon commented 7 years ago

+1

freddydumont commented 7 years ago

I get the same error when using require("moment-timer");

moment-duration-format works this way but not moment-timer for some reason. They both have the same general structure so it's hard to figure out the problem at my current skill level.

@SeverinDK @Alxmerino Did you guys find a solution?

Cryde commented 7 years ago

It's because this plugin isn't wrap into a 'module' (I mean a module to be usable by require() or import)

Have a look here : https://github.com/umdjs/umd

Cryde commented 7 years ago

Probably something like this I guess

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['exports', 'moment'], factory);
    } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
        // CommonJS
        factory(exports, require('moment'));
    } else {
        // Browser globals
        factory((root.yourPlugin = {}), root.moment);
    }
}(this, function (exports, moment) {
   // your code here
}));
Alxmerino commented 7 years ago

sorry for the late response guys, I did come up with a response I will post a PR shortly 😄