diff --git a/gulpfile.js b/gulpfile.js index 51db1e235e695e6b151f4eb14f47fe96fc35231e..93b216644d362aa24496b554c9de69e9694c415c 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,26 +1,27 @@ -/* eslint-disable one-var */ -const autoprefixer = require('autoprefixer'), - chalk = require('chalk'), - concat = require('gulp-concat'), - gulp = require('gulp'), - log = require('fancy-log-levels'), - noop = require('gulp-noop'), - postcss = require('gulp-postcss'), - shell = require('gulp-shell'), - sort = require('gulp-sort'), - sourcemaps = require('gulp-sourcemaps'), - stripCssJSComments = require('gulp-strip-comments'), - childProcess = require('child_process'), - which = require('which'), - fs = require('fs'), - path = require('path'), - os = require('os'), - yargs = require('yargs'), - cfg = require('./build.config.json'); +import {config as minifyConfig, file as minifyFile} from '@tdewolff/minify'; +import yargs from 'yargs'; +import {hideBin} from 'yargs/helpers'; +import autoprefixer from "autoprefixer"; +import chalk from "chalk"; +import gulp from "gulp"; +import concat from "gulp-concat"; +import log from 'fancy-log-levels'; +import noop from "gulp-noop"; +import postcss from "gulp-postcss"; +import shell from "gulp-shell"; +import sort from "gulp-sort"; +import sourcemaps from "gulp-sourcemaps"; +import stripCssJSComments from "gulp-strip-comments"; +import childProcess from "child_process"; +import which from "which"; +import fs from "fs"; +import path from "path"; +import os from "os"; +import cfg from './build.config.json' assert {type: "json"}; // defines build options which can be supplied from the command line // example: npx gulp --minify --verbosity 6 -const args = yargs.options({ +const args = yargs(hideBin(process.argv)).options({ verbosity: {type: 'number', default: 1}, release: {type: 'boolean', default: false}, ci: {type: 'boolean', default: false}, // assumes gitlab CI environment @@ -29,6 +30,8 @@ const args = yargs.options({ sourcemapsincludecontent: {type: 'boolean', default: false} }).argv; +minifyConfig({"html-keep-comments": true, "js-keep-var-names": true}); + /** Filename for the temporary output. Tweego will write here and then it will be moved into the output dir */ const htmlOut = "tmp.html"; @@ -60,30 +63,6 @@ function tweeCompilerExecutable() { return res; } -/** - * @summary Selects minify executable - * - * Similar to tweeCompilerExecutable(), but returns path to the minify executable. However, since - * there are executables only for amd64, it will report error and throw (which will in turn stop - * the build) on other architectures. - */ -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; -} - /** * @summary Composes tweego invocation command * @@ -296,10 +275,9 @@ function moveHTMLInPlace(cb) { fs.rename(path.join(cfg.dirs.intermediate, htmlOut), path.join(cfg.dirs.output, cfg.output), cb); } -// If we were asked to minify the file, instead of moving invoke the minify utility with output in the final dir -if (args.minify) { // do not touch minifierExecutable(), which throws on errors, unconditionally - 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)}"`, {verbose: args.verbosity >= 3})); +function moveAndMinifyHTML(cb) { + minifyFile('text/html', path.join(cfg.dirs.intermediate, htmlOut), path.join(cfg.dirs.output, cfg.output)); + cb(); } /** Removes intermediate compilation files if any */ @@ -315,16 +293,18 @@ function removeIntermediateFiles(cb) { gulp.task('prepare', gulp.parallel(prepareComponent("module"), prepareComponent("story"))); // Create the main build and clean tasks, which include writing Git commit hash if we are working in a Git repo +let cleanOp = noop(); if (isGitCheckout()) { - exports.clean = gulp.parallel(cleanupGit, removeIntermediateFiles); - gulp.task('buildHTML', gulp.series(exports.clean, injectGitCommit, 'prepare', 'compileStory', cleanupGit)); + cleanOp = gulp.parallel(cleanupGit, removeIntermediateFiles); + gulp.task('buildHTML', gulp.series(cleanOp, injectGitCommit, 'prepare', 'compileStory', cleanupGit)); } else if (args.ci) { // CI environment is already clean gulp.task('buildHTML', gulp.series(injectGitCommit, 'prepare', 'compileStory')); } else { - exports.clean = gulp.parallel(removeIntermediateFiles); - gulp.task('buildHTML', gulp.series(exports.clean, 'prepare', 'compileStory')); + cleanOp = gulp.parallel(removeIntermediateFiles); + gulp.task('buildHTML', gulp.series(cleanOp, 'prepare', 'compileStory')); } +export const clean = cleanOp; // Creates theme build task for each subdirectory in the 'themes' dir const themeTasks = fs.readdirSync('themes') @@ -332,13 +312,13 @@ const themeTasks = fs.readdirSync('themes') .map(entry => makeThemeCompilationTask(entry)); // Create user-invocable targets for building HTML and themes -exports.html = gulp.series('buildHTML', args.minify ? 'moveAndMinifyHTML' : moveHTMLInPlace); -exports.themes = gulp.parallel(themeTasks); +export const html = gulp.series('buildHTML', args.minify ? moveAndMinifyHTML : moveHTMLInPlace); +export const themes = gulp.parallel(themeTasks); // Set the default target which is invoked when no target is explicitly provided by user -exports.default = exports.html; +export default html; // Convenient shortcut to build everything (HTML and themes) -exports.all = gulp.parallel(exports.html, exports.themes); +export const all = gulp.parallel(html, themes); // legacy tasks @@ -354,4 +334,4 @@ gulp.task('twineJS', function() { .pipe(gulp.dest('devNotes')); }); -exports.twine = gulp.parallel('twineCSS', 'twineJS'); +export const twine = gulp.parallel('twineCSS', 'twineJS'); diff --git a/package.json b/package.json index 7a0c99af1660088612e905c08ef2de5dcbc7fb8e..314e56032d7e33760bac74039bbf9f1dffa8c3f8 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "free-cities", "description": "The Free Cities game", "version": "1.0.0", + "type": "module", "repository": { "type": "git", "url": "https://gitgud.io/pregmodfan/fc-pregmod.git" @@ -24,8 +25,9 @@ "typescript": "^4.4.0" }, "dependencies": { + "@tdewolff/minify": "^2.12.5", "autoprefixer": "^10.0.0", - "chalk": "^4.1.0", + "chalk": "^5.2.0", "fancy-log-levels": "^1.0.0", "gulp": "^4.0.2", "gulp-concat": "^2.6.1", @@ -35,8 +37,8 @@ "gulp-sort": "^2.0.0", "gulp-sourcemaps": "^3.0.0", "gulp-strip-comments": "^2.5.2", - "postcss": "^8.1.0", - "which": "^2.0.2", - "yargs": "^16.0.0" + "postcss": "^8.4.21", + "which": "^3.0.0", + "yargs": "^17.7.1" } }