ws4kp-linhanced/gulp/publish-frontend.mjs

167 lines
4.5 KiB
JavaScript
Raw Normal View History

2025-05-14 09:42:31 -05:00
import 'dotenv/config';
2024-07-07 22:21:53 -05:00
import {
src, dest, series, parallel,
} from 'gulp';
import concat from 'gulp-concat';
import terser from 'gulp-terser';
import ejs from 'gulp-ejs';
import rename from 'gulp-rename';
2025-05-14 09:02:08 -05:00
import htmlmin from 'gulp-html-minifier-terser';
2024-07-07 22:21:53 -05:00
import { deleteAsync } from 'del';
import webpack from 'webpack-stream';
import TerserPlugin from 'terser-webpack-plugin';
import { readFile } from 'fs/promises';
2025-03-27 10:23:27 -05:00
import file from 'gulp-file';
2025-11-30 04:16:02 +00:00
import dartSass from 'sass';
import gulpSass from 'gulp-sass';
2025-05-23 22:14:48 -05:00
import OVERRIDES from '../src/overrides.mjs';
2026-04-08 12:49:21 -04:00
import { discoverThemes } from '../src/theme-discovery.mjs';
2020-09-04 13:02:20 -05:00
2021-01-04 11:58:58 -06:00
// get cloudfront
2025-03-27 10:23:27 -05:00
import reader from '../src/playlist-reader.mjs';
2024-07-07 22:21:53 -05:00
2025-11-30 04:16:02 +00:00
const sass = gulpSass(dartSass);
2025-03-24 18:24:49 -05:00
const clean = () => deleteAsync(['./dist/**/*', '!./dist/readme.txt']);
2021-01-04 11:58:58 -06:00
2024-07-07 21:49:21 -05:00
const RESOURCES_PATH = './dist/resources';
2021-01-04 11:58:58 -06:00
// Data is now served as JSON files to avoid redundancy
2022-12-07 10:53:18 -06:00
const webpackOptions = {
mode: 'production',
output: {
filename: 'ws.min.js',
},
resolve: {
2024-07-07 22:21:53 -05:00
roots: ['./'],
2022-12-07 10:53:18 -06:00
},
2025-11-30 04:16:02 +00:00
devtool: 'source-map',
2022-12-07 10:53:18 -06:00
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: {
// sourceMap: true,
format: {
comments: false,
},
},
}),
],
},
};
const jsVendorSources = [
'server/scripts/vendor/auto/nosleep.js',
2020-10-16 15:52:56 -05:00
'server/scripts/vendor/auto/swiped-events.js',
'server/scripts/vendor/auto/suncalc.js',
2020-09-04 13:02:20 -05:00
];
2022-12-07 10:53:18 -06:00
2024-07-07 22:21:53 -05:00
const compressJsVendor = () => src(jsVendorSources)
2022-12-07 10:53:18 -06:00
.pipe(concat('vendor.min.js'))
2021-01-04 11:58:58 -06:00
.pipe(terser())
2024-07-07 22:21:53 -05:00
.pipe(dest(RESOURCES_PATH));
2020-09-04 13:02:20 -05:00
2022-12-07 10:53:18 -06:00
const mjsSources = [
'server/scripts/modules/currentweatherscroll.mjs',
2022-12-14 16:31:11 -06:00
'server/scripts/modules/hazards.mjs',
2022-12-07 10:53:18 -06:00
'server/scripts/modules/currentweather.mjs',
'server/scripts/modules/latestobservations.mjs',
2022-12-07 10:53:18 -06:00
'server/scripts/modules/almanac.mjs',
2025-05-15 16:04:57 -05:00
'server/scripts/modules/spc-outlook.mjs',
2022-12-07 10:53:18 -06:00
'server/scripts/modules/icons.mjs',
'server/scripts/modules/extendedforecast.mjs',
'server/scripts/modules/hourly.mjs',
2022-12-07 15:36:02 -06:00
'server/scripts/modules/hourly-graph.mjs',
2022-12-07 10:53:18 -06:00
'server/scripts/modules/localforecast.mjs',
'server/scripts/modules/radar.mjs',
'server/scripts/modules/groundview.mjs',
2022-12-07 10:53:18 -06:00
'server/scripts/modules/regionalforecast.mjs',
'server/scripts/modules/travelforecast.mjs',
'server/scripts/modules/progress.mjs',
2025-03-22 15:10:06 +01:00
'server/scripts/modules/media.mjs',
'server/scripts/modules/custom-scroll-text.mjs',
2026-04-06 18:50:03 -04:00
'server/scripts/modules/serverobservations.mjs',
'server/scripts/modules/linuxnews.mjs',
2022-12-07 10:53:18 -06:00
'server/scripts/index.mjs',
];
2024-07-07 22:21:53 -05:00
const buildJs = () => src(mjsSources)
2022-12-07 10:53:18 -06:00
.pipe(webpack(webpackOptions))
2024-07-07 22:21:53 -05:00
.pipe(dest(RESOURCES_PATH));
2022-12-07 10:53:18 -06:00
2021-01-04 11:58:58 -06:00
const cssSources = [
2025-11-30 04:16:02 +00:00
'server/styles/scss/**/*.scss',
2020-09-08 10:05:46 -05:00
];
2025-11-30 04:16:02 +00:00
const buildCss = () => src(cssSources)
.pipe(sass({ style: 'compressed' }).on('error', sass.logError))
.pipe(rename({ suffix: '.min' }))
.pipe(dest(RESOURCES_PATH))
.pipe(dest('./server/styles'));
2020-09-04 13:02:20 -05:00
2021-01-04 11:58:58 -06:00
const htmlSources = [
2020-09-04 13:02:20 -05:00
'views/*.ejs',
];
const packageJson = await readFile('package.json');
const { version } = JSON.parse(packageJson);
2026-04-08 12:49:21 -04:00
const { themes, themeAssets } = await discoverThemes();
2020-09-04 13:02:20 -05:00
const compressHtml = async () => src(htmlSources)
.pipe(ejs({
production: version,
serverAvailable: false,
version,
2026-04-08 12:49:21 -04:00
themes,
themeAssets,
OVERRIDES,
query: {},
}))
.pipe(rename({ extname: '.html' }))
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(dest('./dist'));
2021-01-04 11:58:58 -06:00
const otherFiles = [
2020-09-04 13:02:20 -05:00
'server/robots.txt',
'server/manifest.json',
2026-04-09 23:10:04 -04:00
'server/alert/**/*.mp3',
2025-03-27 10:23:27 -05:00
'server/music/**/*.mp3',
2020-09-04 13:02:20 -05:00
];
2025-03-24 18:24:49 -05:00
const copyOtherFiles = () => src(otherFiles, { base: 'server/', encoding: false })
2024-07-07 22:21:53 -05:00
.pipe(dest('./dist'));
2020-09-04 13:02:20 -05:00
2026-04-08 12:49:21 -04:00
const copyThemes = () => src('themes/**', { base: '.', encoding: false })
.pipe(dest('./dist'));
// Copy JSON data files for static hosting
const copyDataFiles = () => src([
'datagenerators/output/travelcities.json',
'datagenerators/output/regionalcities.json',
'datagenerators/output/stations.json',
2026-04-07 18:08:38 -04:00
'datagenerators/output/radarcities.json',
]).pipe(dest('./dist/data'));
2022-12-09 11:58:47 -06:00
const imageSources = [
'server/fonts/**',
'server/images/**',
2025-05-15 09:02:49 -05:00
'!server/images/gimp/**',
2022-12-09 11:58:47 -06:00
];
2025-06-26 22:30:42 -05:00
const copyImageSources = () => src(imageSources, { base: './server', encoding: false })
.pipe(dest('./dist'));
2025-03-24 18:24:49 -05:00
const buildPlaylist = async () => {
const availableFiles = await reader();
const playlist = { availableFiles };
2025-03-27 10:23:27 -05:00
return file('playlist.json', JSON.stringify(playlist)).pipe(dest('./dist'));
};
2025-03-24 18:24:49 -05:00
2026-04-08 12:49:21 -04:00
const buildDist = series(clean, parallel(buildJs, compressJsVendor, buildCss, compressHtml, copyOtherFiles, copyThemes, copyDataFiles, copyImageSources, buildPlaylist));
2024-01-08 08:49:51 -06:00
export default buildDist;
2025-03-24 18:24:49 -05:00
export {
buildDist,
2026-04-08 12:49:21 -04:00
copyThemes,
2025-03-27 10:23:27 -05:00
};