Webpack


npm install moment
var moment = require('moment');
moment().format();

默认情况下,webpack 会打包所有的 Moment.js 语言环境(在 Moment.js 2.18.1 中,最小为 160 KB)。 若要剥离不必要的语言环境且仅打包使用的语言环境,则添加 moment-locales-webpack-plugin

// webpack.config.js
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');

module.exports = {
    plugins: [
        // 剥离除 “en” 以外的所有语言环境。
        new MomentLocalesPlugin(),

        // 或者:剥离除 “en”、“es-us” 和 “ru” 以外的所有语言环境。
        //(“en” 内置于 Moment 中,无法移除)
        new MomentLocalesPlugin({
            localesToKeep: ['es-us', 'ru'],
        }),
    ],
};

还有其他资源可以使用 webpack 优化 Moment.js,例如该资源

npm install moment
var moment = require('moment');
moment().format();

Note: By default, webpack bundles all Moment.js locales (in Moment.js 2.18.1, that’s 160 minified KBs). To strip unnecessary locales and bundle only the used ones, add moment-locales-webpack-plugin:

// webpack.config.js
const 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'],
        }),
    ],
};

There are other resources to optimize Moment.js with webpack, for example this one.