From 722271ab4ac30bca8addce8556ff7ce2d562c9ef Mon Sep 17 00:00:00 2001 From: ezsh <ezsh.junk@gmail.com> Date: Thu, 18 Mar 2021 00:07:41 +0100 Subject: [PATCH] Gulp: add minification Optionally minify HTML file using the bundled minifier. --- gulpfile.js | 32 ++++++++++++++++++++++++++++---- package.json | 3 ++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 814539d4b0f..86f466cbaa9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,5 +1,8 @@ -const gulp = require('gulp'), +/* eslint-disable one-var */ +const autoprefixer = require('autoprefixer'), + chalk = require('chalk'), concat = require('gulp-concat'), + gulp = require('gulp'), git = require('gulp-git'), log = require('fancy-log-levels'), noop = require('gulp-noop'), @@ -8,7 +11,6 @@ const gulp = require('gulp'), sort = require('gulp-sort'), sourcemaps = require('gulp-sourcemaps'), stripCssJSComments = require('gulp-strip-comments'), - autoprefixer = require('autoprefixer'), which = require('which'), fs = require('fs'), path = require('path'), @@ -19,6 +21,7 @@ const gulp = require('gulp'), const args = yargs.options({ verbosity: {type: 'number', default: 0}, release: {type: 'boolean', default: false}, + minify: {type: 'boolean', default: false}, embedsourcemaps: {type: 'boolean', default: false}, sourcemapsincludecontent: {type: 'boolean', default: false} }).argv; @@ -45,6 +48,23 @@ function tweeCompilerExecutable() { return res; } +function minifierExecutable() { + if (os.arch() !== 'x64') { + log.error(chalk.red("Minification only available on amd64 systems.")); + throw "Build interrupted"; + } + const archSuffix = 'amd64'; + const platformSuffix = { + 'Darwin': 'darwin', + 'Linux': 'linux', + 'Windows_NT': 'win' + }[os.type()]; + const extension = os.type() === 'Windows_NT' ? '.exe' : ''; + const res = path.join('.', 'devTools', 'minify', `minify_${platformSuffix}_${archSuffix}${extension}`); + log.info('Using minifier at ', res); + return res; +} + function tweeCompileCommand() { let sources = [path.join(cfg.dirs.intermediate, "story")]; sources.push(...cfg.sources.story.media); @@ -183,6 +203,11 @@ function moveHTMLInPlace(cb) { fs.rename(path.join(cfg.dirs.intermediate, htmlOut), path.join(cfg.dirs.output, cfg.output), cb); } +if (args.minify) { // do not touch minifierExecutable() which throws on errors + gulp.task('moveAndMinifyHTML', shell.task( + `${minifierExecutable()} --html-keep-comments --js-keep-var-names -o "${path.join(cfg.dirs.output, cfg.output)}" "${path.join(cfg.dirs.intermediate, htmlOut)}"`)); +} + function clean(cb) { if (fs.existsSync(cfg.gitVersionFile)) { fs.unlinkSync(cfg.gitVersionFile); @@ -206,8 +231,7 @@ const themeTasks = fs.readdirSync('themes') .map(entry => makeThemeCompilationTask(entry)); - -exports.html = gulp.series('buildHTML', moveHTMLInPlace); +exports.html = gulp.series('buildHTML', args.minify ? 'moveAndMinifyHTML' : moveHTMLInPlace); exports.themes = gulp.parallel(themeTasks); exports.clean = clean; diff --git a/package.json b/package.json index 57d6a17b50e..705bf527266 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ "@types/lodash": "^4.14.159", "@types/mousetrap": "^1.6.3", "@types/twine-sugarcube": "^2.33.0", - "autoprefixer": "^10.0.0", "eslint": "^7.6.0", "eslint-plugin-jsdoc": "^32.0.0", "eslint-plugin-sonarjs": "^0.6.0", @@ -25,6 +24,8 @@ "typescript": "^4.2.0" }, "dependencies": { + "autoprefixer": "^10.0.0", + "chalk": "^4.1.0", "fancy-log-levels": "^1.0.0", "gulp": "^4.0.2", "gulp-concat": "^2.6.1", -- GitLab