diff --git a/gulpfile.js b/gulpfile.js index 814539d4b0f420b4ec33e0d6390114f98b414823..86f466cbaa9e5c793b7a066ba201341cc36a929d 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 57d6a17b50e78d3d3b8d5a5529808231750d9c54..705bf5272664a2e4ab994b3236fe75095273bb6f 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",