关于moment.js:如何使用-webpack-优化-momentjs

(1)荡涤moment语言环境文件默认状况下,当您编写var moment = require('moment')代码并应用 webpack 打包时,捆绑文件的大小会变得很重,因为webpack 会捆绑所有Moment.js 所有语言环境文件(在 Moment.js 2.18.1 中,压缩后的 KB 为 160)。 要去除不必要的语言环境并仅捆绑应用的语言环境,请增加moment-locales-webpack-plugin: // webpack.config.jsconst MomentLocalesPlugin = require('moment-locales-webpack-plugin');module.exports = { plugins: [ // To strip all locales except “en” new MomentLocalesPlugin(), // Or: To strip all locales except “en”, “es-us” and “ru” // (“en” is built into Moment and can’t be removed) new MomentLocalesPlugin({ localesToKeep: ['es-us', 'ru'], }), ],};为了优化大小,还能够应用两个 webpack 插件传送门: IgnorePluginContextReplacementPluginIgnorePlugin您能够应用IgnorePlugin. const webpack = require('webpack');module.exports = { //... plugins: [ // Ignore all locale files of moment.js new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), ],};而且您依然能够在代码中加载一些语言环境。 ...

September 2, 2022 · 2 min · jiezi