javascript 我应该如何在 webpack 中使用时刻时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29548386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:42:34  来源:igfitidea点击:

How should I use moment-timezone with webpack?

javascriptnode.jstimezonemomentjswebpack

提问by jaredkwright

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.

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

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.

在这一点上,我并不关心构建失败,因为我关心构建的大小,如果它真的成功了。尽管如此,显然在某个时候也需要解决失败的构建。

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).

我很感激有关如何处理此问题的任何指示,特别是如果你们中的任何人在使用 webpack(或也可能使用 browserify)时遇到过同样的问题。

回答by Jeroen Coumans

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

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

$npm install json-loader

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

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

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

回答by William S

If you're using webpack 2.x (currently in beta)

如果您使用的是 webpack 2.x(目前处于测试阶段)

npm install json-loader

then include this in your rules

然后将其包含在您的 rules

{
    test: /\.json$/,
    loader: "json-loader"
}