From 23f2f16715893775ec71122d2c8471b26370add1 Mon Sep 17 00:00:00 2001 From: Dominik Perpeet Date: Tue, 25 Jul 2017 12:08:13 +0200 Subject: [PATCH] Automatically minify js files for distribution --- Makefile | 6 ++++-- webpack.config.js | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index f079298..197fe87 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ all: - npm run build + NODE_ENV=$(NODE_ENV) npm run build clean: rm -rf dist/ @@ -9,7 +9,9 @@ install: all mkdir -p /usr/share/cockpit/subscription-manager cp -r dist/* /usr/share/cockpit/subscription-manager -dist-gzip: all +# when building a distribution tarball, call webpack with a 'production' environment +dist-gzip: NODE_ENV=production +dist-gzip: clean all mkdir -p _install/usr/share/cockpit cp -r dist/ _install/usr/share/cockpit/subscription-manager mkdir -p _install/usr/share/metainfo/ diff --git a/webpack.config.js b/webpack.config.js index 19ad7d6..5cfdc22 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,6 +1,7 @@ const path = require("path"); const copy = require("copy-webpack-plugin"); const fs = require("fs"); +const webpack = require("webpack"); var externals = { "cockpit": "cockpit", @@ -13,6 +14,9 @@ const distdir = builddir + path.sep + "dist"; const section = process.env.ONLYDIR || null; const nodedir = path.resolve((process.env.SRCDIR || __dirname), "node_modules"); +/* A standard nodejs and webpack pattern */ +var production = process.env.NODE_ENV === 'production'; + var info = { entries: { "index": [ @@ -73,9 +77,27 @@ info.files.forEach(function(value) { info.files = files; var plugins = [ + new webpack.DefinePlugin({ + 'process.env': { + 'NODE_ENV': JSON.stringify(production ? 'production' : 'development') + } + }), new copy(info.files) ]; +/* Only minimize when in production mode */ +if (production) { + plugins.unshift(new webpack.optimize.UglifyJsPlugin({ + beautify: true, + compress: { + warnings: false + }, + })); + + /* Rename output files when minimizing */ + output.filename = "[name].min.js"; +} + module.exports = { entry: info.entries, externals: externals,