Vue.js Webpack Split moment-timezone Into Vendor Bundle

If you utilize moment-timezone in your project, you shall notice the size of app.jsbloated instead of vendor.js.

npm install moment --savenpm install moment-timezone --save
import moment from 'moment'import 'moment-timezone'

You can analyze bundle size by running npm run build --report and you will notice moment-timezone's latest.json is in app.js instead of vendor.js. If you observe carefully, you will notice moment-timezone js files is within vendor.js. Thus, the problem lies with .json file.

Edit build/, observe changes in // EDIT.

...const webpackConfig = merge(baseWebpackConfig, {  ...  plugins: [    ...    new webpack.optimize.CommonsChunkPlugin({      name: 'vendor',      minChunks: function (module) {        // any required modules inside node_modules are extracted to vendor        return (          module.resource &&          // EDIT: Enable .css and moment-timezone json file as vendor          /\.(js|css|sass|scss|less|json)$/.test(module.resource) &&          // /\.js$/.test(module.resource) &&          module.resource.indexOf(            path.join(__dirname, '../node_modules')          ) === 0        )      }    }),    ...  ],  ...})

The above changes will build moment-timezone's latest-json into vendor.js.

You can also reduce moment.js bundle size.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.