From 0d6df10fbd55f30ca0583d28f0c3d30f91b90593 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Thu, 12 Jul 2018 16:47:19 +0200 Subject: [PATCH] Drop useless moment locales from built file --- build/webpack.prod.conf.js | 16 ++++++++++++++++ config/index.js | 4 ++-- package.json | 2 +- yarn.lock | 2 +- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/build/webpack.prod.conf.js b/build/webpack.prod.conf.js index fdfb39d..0bf8a23 100644 --- a/build/webpack.prod.conf.js +++ b/build/webpack.prod.conf.js @@ -1,4 +1,5 @@ 'use strict' +const fs = require('fs') const path = require('path') const svg2png = require('svg2png') const utils = require('./utils') @@ -15,6 +16,18 @@ const UglifyJsPlugin = require('uglifyjs-webpack-plugin') const env = require('../config/prod.env') +// List available locales, to fetch only the required locales from Moment.JS: +// Build a regexp that selects the locale's name without the JS extension (due +// to the way moment includes those) and ensure that's the last character to +// not include locale variants. See discussion in +// https://framagit.org/bnjbvr/kresus/merge_requests/448#note_130514 +const locales = fs.readdirSync('src/i18n').filter(x => x != 'index.js').map( + x => x.replace('.js', '') +); +const localesRegex = new RegExp( + '(' + locales.join('|') + ')$' +); + const webpackConfig = merge(baseWebpackConfig, { module: { rules: utils.styleLoaders({ @@ -126,6 +139,9 @@ const webpackConfig = merge(baseWebpackConfig, { minChunks: 3 }), + // Only keep the useful locales from Moment. + new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, localesRegex), + // copy custom static assets new CopyWebpackPlugin([ { diff --git a/config/index.js b/config/index.js index 52cab66..bdf02b8 100644 --- a/config/index.js +++ b/config/index.js @@ -69,9 +69,9 @@ module.exports = { // Run the build command with an extra argument to // View the bundle analyzer report after build finishes: - // `npm run build --report` + // `ANALYZE=true yarn build` // Set to `true` or `false` to always turn it on or off - bundleAnalyzerReport: process.env.npm_config_report, + bundleAnalyzerReport: process.env.ANALYZE, // OpenGraph-related variables ogURL: 'https://cyclo.phyks.me', diff --git a/package.json b/package.json index bfae6a4..2d65594 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "vue-style-loader": "^3.0.1", "vue-template-compiler": "^2.5.2", "webpack": "^3.6.0", - "webpack-bundle-analyzer": "^2.9.0", + "webpack-bundle-analyzer": "^2.13.1", "webpack-dev-server": "^2.9.1", "webpack-merge": "^4.1.0" }, diff --git a/yarn.lock b/yarn.lock index 896f97f..06f8a6d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6707,7 +6707,7 @@ wbuf@^1.1.0, wbuf@^1.7.2: dependencies: minimalistic-assert "^1.0.0" -webpack-bundle-analyzer@^2.9.0: +webpack-bundle-analyzer@^2.13.1: version "2.13.1" resolved "https://registry.yarnpkg.com/webpack-bundle-analyzer/-/webpack-bundle-analyzer-2.13.1.tgz#07d2176c6e86c3cdce4c23e56fae2a7b6b4ad526" dependencies: