我应该如何在webpack中使用moment-timezone? [英] How should I use moment-timezone with webpack?

查看:207
本文介绍了我应该如何在webpack中使用moment-timezone?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用webpack构建我的项目时,我通常需要来自npm模块的CommonJS中的模块。我需要在我的项目中使用时间 - 时区,但是在构建软件包时,您还必须构建来自时刻 - 时区的所有数据,这可能非常多。

In using webpack to build my project, I typically require modules in CommonJS from npm modules. I need moment-timezone in my project, however in building the package you must also build all the data from moment-timezone, which can be quite a lot.

此外,构建失败并出现以下错误:

Additionally the build is failing with the following error:

ERROR in ./~/moment-timezone/data/packed/latest.json
Module parse failed: /site/node_modules/moment-timezone/data/packed/latest.json Line 2: Unexpected token :
You may need an appropriate loader to handle this file type.
| {
|   "version": "2015a",
|   "zones": [
|       "Africa/Abidjan|LMT GMT|g.8 0|01|-2ldXH.Q",
@ ./~/moment-timezone/index.js 4:15-51

此时我并不关心构建失败,因为我关于构建的大小,如果它实际上成功了。虽然,显然失败的构建也需要在某些时候得到解决。

At this point I am not as concerned with the build failing, as I am about the size of the build if it actually succeeds. Though, obviously the failing build will need to be addressed too at some point.

我会很感激如何处理这个,特别是如果你们中的任何人遇到过这个同样的问题使用webpack(或者也可能是browserify)。

I would appreciate any pointers on how to handle this, especially if any of you have encountered this same issue using webpack (or browserify too, probably).

推荐答案

您可以通过将JSON加载程序添加到webpack配置来解决此问题。

You can fix this by adding the JSON loader to your webpack configuration.

$npm install json-loader

并将其添加到webpack.config.js中的加载器中。不要忘记添加扩展名。

And add it to your loaders in webpack.config.js. Don't forget to add the extension as well.

{
  module: {
    loaders: [
        {include: /\.json$/, loaders: ["json-loader"]}
    ]
  },
  resolve: {
    extensions: ['', '.json', '.jsx', '.js']
  }
}

这篇关于我应该如何在webpack中使用moment-timezone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆