gilmoreorless / moment-timezone-data-webpack-plugin

Reduce moment-timezone data size for a webpack build
MIT License
92 stars 7 forks source link

Build fails due to missing data #27

Closed galabra closed 4 years ago

galabra commented 4 years ago

I've added the following object to the plugins array in webpack.config:

// `currentYear` is obviously defined above:
// const currentYear = new Date().getFullYear();
new MomentTimezoneDataPlugin({
    startYear: currentYear - 1,
    endYear: currentYear + 1,
    matchCountries: ['US', 'CA'],
}),

When I try to start my project, I get the following error stack:

TypeError: Cannot read property 'length' of undefined
    at findStartAndEndIndex (.../node_modules/moment-timezone/moment-timezone-utils.js:249:18)
    at filterYears (.../node_modules/moment-timezone/moment-timezone-utils.js:282:16)
    at Function.filterLinkPack (.../node_modules/moment-timezone/moment-timezone-utils.js:308:21)
    at filterData (.../node_modules/moment-timezone-data-webpack-plugin/src/index.js:100:34)
    at .../node_modules/moment-timezone-data-webpack-plugin/src/index.js:190:34
    at .../node_modules/webpack/lib/NormalModuleReplacementPlugin.js:36:8
    at AsyncSeriesWaterfallHook.eval [as callAsync] (eval at create (.../node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:19:16)
    at .../node_modules/webpack/lib/NormalModuleFactory.js:138:29
    at .../node_modules/webpack/lib/NormalModuleFactory.js:346:9

The faulty line is endI = untils.length + 1, so I tried printing the faulty source that's missing untils and apparently it's this one, which contains only the name key:

 { name: 'America/Indiana/Indianapolis' }

Couldn't find any previous occurrence to this error, so I hope you can help. Thanks a lot!

gilmoreorless commented 4 years ago

Ooh yep, looks like a bug in this plugin, passing the wrong data into moment-timezone. When I try your configuration I get the same failure, but if I try using matchZones: 'America/Indiana/Indianapolis' instead of matchCountries, it works fine.

I'll take a look and get a fix ready.

gilmoreorless commented 4 years ago

The bug was a hidden assumption in my code that the list of zones for each country would not contain any links to other zones, because that's how the IANA source files define them. This turned out not to be true: https://github.com/moment/moment-timezone/issues/835

I've added a workaround and released version 1.3.0.