From 6ab44e9aeaeeab87290b461b039efa2bf534608a Mon Sep 17 00:00:00 2001 From: Martin Packman Date: Sat, 3 Apr 2021 17:57:11 +0100 Subject: [PATCH] Allow private class members to be mangled Openlayers classes use an underscore suffix convention to mark class members as private. These are undocumented implementation details and presumably unsafe for code outside the class to rely on. Allowing javascript minimisation to change these names gives 5% bundle size reduction to the library. --- config/webpack-config-legacy-build.js | 12 ++++++++++++ examples/webpack/config.js | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/config/webpack-config-legacy-build.js b/config/webpack-config-legacy-build.js index a7fb3a7977..92d32bfa00 100644 --- a/config/webpack-config-legacy-build.js +++ b/config/webpack-config-legacy-build.js @@ -1,4 +1,6 @@ const path = require('path'); +const TerserPlugin = require('terser-webpack-plugin'); + module.exports = { entry: './build/index.js', devtool: 'source-map', @@ -30,6 +32,16 @@ module.exports = { ol: path.resolve('./build/ol'), }, }, + optimization: { + minimizer: [ + new TerserPlugin({ + terserOptions: { + // Mangle private members convention with underscore suffix + mangle: {properties: {regex: /_$/}}, + }, + }), + ], + }, output: { path: path.resolve('./build/legacy'), filename: 'ol.js', diff --git a/examples/webpack/config.js b/examples/webpack/config.js index 7f7692715f..e3a1108a51 100644 --- a/examples/webpack/config.js +++ b/examples/webpack/config.js @@ -61,6 +61,10 @@ module.exports = { // Do not minify examples that inject code into workers exclude: [/(color-manipulation|region-growing|raster)\.js/], extractComments: false, + terserOptions: { + // Mangle private members convention with underscore suffix + mangle: {properties: {regex: /_$/}}, + }, }), ], runtimeChunk: {