ws4kp-linhanced/gulp/publish-frontend.mjs

192 lines
4.9 KiB
JavaScript
Raw Normal View History

2021-01-04 11:58:58 -06:00
/* eslint-disable import/no-extraneous-dependencies */
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';
import htmlmin from 'gulp-htmlmin';
import { deleteAsync } from 'del';
import s3Upload from 'gulp-s3-upload';
import webpack from 'webpack-stream';
import TerserPlugin from 'terser-webpack-plugin';
import { readFile } from 'fs/promises';
2025-03-24 18:24:49 -05:00
import reader from '../src/playlist-reader.mjs';
import file from "gulp-file";
2020-09-04 13:02:20 -05:00
2021-01-04 11:58:58 -06:00
// get cloudfront
2024-07-07 22:21:53 -05:00
import { CloudFrontClient, CreateInvalidationCommand } from '@aws-sdk/client-cloudfront';
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 cloudfront = new CloudFrontClient({ region: 'us-east-1' });
const RESOURCES_PATH = './dist/resources';
2021-01-04 11:58:58 -06:00
const jsSourcesData = [
2020-09-08 10:05:46 -05:00
'server/scripts/data/travelcities.js',
'server/scripts/data/regionalcities.js',
'server/scripts/data/stations.js',
];
2022-12-07 10:53:18 -06:00
const webpackOptions = {
mode: 'production',
// mode: 'development',
// devtool: 'source-map',
output: {
filename: 'ws.min.js',
},
resolve: {
2024-07-07 22:21:53 -05:00
roots: ['./'],
2022-12-07 10:53:18 -06:00
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: {
// sourceMap: true,
format: {
comments: false,
},
},
}),
],
},
};
2024-07-07 22:21:53 -05:00
const compressJsData = () => src(jsSourcesData)
2021-01-04 11:58:58 -06:00
.pipe(concat('data.min.js'))
.pipe(terser())
2024-07-07 22:21:53 -05:00
.pipe(dest(RESOURCES_PATH));
2020-09-08 10:05:46 -05:00
2022-12-07 10:53:18 -06:00
const jsVendorSources = [
'server/scripts/vendor/auto/jquery.js',
2020-09-17 14:37:54 -05:00
'server/scripts/vendor/jquery.autocomplete.min.js',
'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/almanac.mjs',
'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/latestobservations.mjs',
'server/scripts/modules/localforecast.mjs',
'server/scripts/modules/radar.mjs',
'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',
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 = [
2022-11-21 21:54:42 -06:00
'server/styles/main.css',
2020-09-08 10:05:46 -05:00
];
2024-07-07 22:21:53 -05:00
const copyCss = () => src(cssSources)
2021-01-04 11:58:58 -06:00
.pipe(concat('ws.min.css'))
2024-07-07 22:21:53 -05:00
.pipe(dest(RESOURCES_PATH));
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',
];
2024-07-07 22:21:53 -05:00
const compressHtml = async () => {
const packageJson = await readFile('package.json');
const { version } = JSON.parse(packageJson);
return src(htmlSources)
2020-09-04 13:02:20 -05:00
.pipe(ejs({
production: version,
2020-09-08 14:39:17 -05:00
version,
2020-09-04 13:02:20 -05:00
}))
2021-01-04 11:58:58 -06:00
.pipe(rename({ extname: '.html' }))
.pipe(htmlmin({ collapseWhitespace: true }))
2024-07-07 22:21:53 -05:00
.pipe(dest('./dist'));
};
2020-09-04 13:02:20 -05:00
2021-01-04 11:58:58 -06:00
const otherFiles = [
2020-09-04 13:02:20 -05:00
'server/robots.txt',
'server/manifest.json',
2025-03-24 18:24:49 -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
const s3 = s3Upload({
useIAM: true,
2021-01-04 11:58:58 -06:00
}, {
2020-09-04 13:02:20 -05:00
region: 'us-east-1',
});
2021-01-04 11:58:58 -06:00
const uploadSources = [
2020-09-04 13:02:20 -05:00
'dist/**',
2022-12-07 10:53:18 -06:00
'!dist/**/*.map',
2020-09-04 13:02:20 -05:00
];
2024-07-07 22:21:53 -05:00
const upload = () => src(uploadSources, { base: './dist' })
2021-01-04 11:58:58 -06:00
.pipe(s3({
Bucket: 'weatherstar',
StorageClass: 'STANDARD',
maps: {
CacheControl: (keyname) => {
if (keyname.indexOf('index.html') > -1) return 'max-age=300'; // 10 minutes
return 'max-age=2592000'; // 1 month
2020-09-04 13:02:20 -05:00
},
2021-01-04 11:58:58 -06:00
},
2024-07-07 22:21:53 -05:00
}));
2020-09-04 13:02:20 -05:00
2022-12-09 11:58:47 -06:00
const imageSources = [
'server/fonts/**',
'server/images/**',
];
2024-07-07 22:21:53 -05:00
const uploadImages = () => src(imageSources, { base: './server', encoding: false })
2022-12-09 11:58:47 -06:00
.pipe(
s3({
Bucket: 'weatherstar',
StorageClass: 'STANDARD',
}),
2024-07-07 22:21:53 -05:00
);
2022-12-09 11:58:47 -06:00
2024-07-07 22:21:53 -05:00
const invalidate = () => cloudfront.send(new CreateInvalidationCommand({
2021-01-04 11:58:58 -06:00
DistributionId: 'E9171A4KV8KCW',
InvalidationBatch: {
CallerReference: (new Date()).toLocaleString(),
Paths: {
Quantity: 1,
Items: ['/*'],
2020-09-04 13:02:20 -05:00
},
2021-01-04 11:58:58 -06:00
},
2024-07-07 22:21:53 -05:00
}));
2020-09-04 13:02:20 -05:00
2025-03-24 18:24:49 -05:00
const buildPlaylist = async () => {
const availableFiles = await reader();
const playlist = { availableFiles };
return file('playlist.json', JSON.stringify(playlist)).pipe(dest('./dist'))
}
const buildDist = series(clean, parallel(buildJs, compressJsData, compressJsVendor, copyCss, compressHtml, copyOtherFiles, buildPlaylist));
2024-01-08 08:49:51 -06:00
2022-12-14 11:22:55 -06:00
// upload_images could be in parallel with upload, but _images logs a lot and has little changes
// by running upload last the majority of the changes will be at the bottom of the log for easy viewing
2025-03-22 15:10:06 +01:00
const publishFrontend = series(buildDist, uploadImages, upload, invalidate);
2024-07-07 22:21:53 -05:00
export default publishFrontend;
2025-03-24 18:24:49 -05:00
export {
buildDist,
}