【问题标题】:moment-timezone optimization with webpack使用 webpack 进行时刻时区优化
【发布时间】:2018-11-07 20:22:27
【问题描述】:

我尝试排除 moment-timezone 使用的 latest.json 文件,并使用我自己的仅包含您需要的时区的文件。减少我的 webpack 输出。

在我尝试过的 webpack config.plugins 中:

new webpack.NormalModuleReplacementPlugin(
    /moment-timezone\/data\/packed\/latest\.json/,
    require.resolve('./timezones/timezone-definitions.json')
)

但没有成功。我仍然得到时刻时区使用的 latest.json 文件。我在控制台中没有收到任何错误。

有什么建议吗?

【问题讨论】:

标签: webpack momentjs


【解决方案1】:

我尝试使用 webpack.NormalModuleReplacementPlugin() 解决方案,但它不起作用,然后我从下面的链接中找到了评论并使用了别名。它奏效了。

https://github.com/webpack/webpack/issues/8105#issuecomment-425823174

resolve: {
        extensions: [".ts", ".js", ".svg", ".json"],
        alias: {
            [path.resolve(__dirname, "node_modules/moment-timezone/data/packed/latest.json")]:
                path.resolve(__dirname, "src/common/timezone-definitions.json")
        }
    }

确保将.json 添加到resolve.extensions 数组中。

【讨论】:

  • 以上的解决方法你试过了吗?如果它对您有用,请将其标记为答案。
【解决方案2】:

如果您使用 webpack,您可能有兴趣使用 moment-timezone-data-webpack-plugin 来优化 moment-timezone latest.json

您可以通过以下方式限制要从 latest.json 中包含的数据:

  • 选择年份范围
  • 选择地区、国家...

包含一组区域的示例

const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');

module.exports = {
  plugins: [
    new MomentTimezoneDataPlugin({
        matchZones: [/^Australia/, 'Pacific/Auckland', 'Etc/UTC'],
        startYear: 2000,
        endYear: 2030,
    }),
  ]
};

您可以找到更多信息和示例here

【讨论】:

    猜你喜欢
    • 2016-01-21
    • 2015-06-15
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2020-05-21
    • 2020-09-06
    相关资源
    最近更新 更多