Skip to content

Commit

Permalink
Enable conditional JS and CSS minification based on environment variable
Browse files Browse the repository at this point in the history
No changes for normal build, only clearer output for the diffs.
  • Loading branch information
paskal committed Nov 1, 2024
1 parent aeec21b commit 317c32b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/ci-build-site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
- name: Build previous version
run: |
git checkout ${{ steps.get-comparison-commit.outputs.compare_sha }}
docker build -t radio-t/site:previous .
docker build -t radio-t/site:previous --build-arg DO_NOT_MINIFY=true .
# Create directories for previous build
mkdir -p ${{ steps.set-dirs.outputs.old_dir }}/hugo
Expand All @@ -78,7 +78,7 @@ jobs:
- name: Build current version
run: |
git checkout ${{ github.event.pull_request.head.sha || github.sha }}
docker build -t radio-t/site:current .
docker build -t radio-t/site:current --build-arg DO_NOT_MINIFY=true .
# Create directories for current build
mkdir -p ${{ steps.set-dirs.outputs.new_dir }}/hugo
Expand Down Expand Up @@ -194,7 +194,7 @@ jobs:
-d "api_paste_format=diff" \
-d "api_paste_private=0" \
-d "api_paste_name=${paste_name}" \
--data-urlencode "api_paste_code@changes.diff" || echo "curl call to pastebin failed")
-d "api_paste_code=$(< changes.diff)" || echo "curl call to pastebin failed")
# Check if the output is a valid URL, otherwise print an error
if [[ "$paste_output" == "https://"* ]]; then
Expand Down Expand Up @@ -222,6 +222,12 @@ jobs:
id: get-comment-body
run: |
SUMMARY=$(cat changes.md)
# Truncate SUMMARY if it exceeds 20,000 characters
if [ ${#SUMMARY} -gt 20000 ]; then
SUMMARY="${SUMMARY:0:20000}... (truncated)"
fi
echo "body<<EOF" >> $GITHUB_OUTPUT
echo "### Site Build Comparison" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ WORKDIR /app
COPY hugo/package.json hugo/package-lock.json ./
RUN npm ci

ARG DO_NOT_MINIFY

ENV NODE_ENV=production
# needed only for old webpack version
ENV NODE_OPTIONS=--openssl-legacy-provider
Expand Down
2 changes: 1 addition & 1 deletion exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ echo " === copy frontend ==="
cp -rf /app/static/build /srv/hugo/static/
cp -rf /app/data/manifest.json /srv/hugo/data/
echo " === generate pages ==="
if [ -z "$DO_NOT_MINIFY_HUGO" ]; then
if [ "$DO_NOT_MINIFY_HUGO" != "true" ]; then
echo " === build with minify ==="
hugo --minify
else
Expand Down
10 changes: 7 additions & 3 deletions hugo/webpack.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const mix = require('laravel-mix');
const babel = require('@babel/core');
const PurgeCSSPlugin = require('purgecss-webpack-plugin');
const purgecssHtml = require('purgecss-from-html');
const process = require('process');

const shouldMinify = process.env.DO_NOT_MINIFY !== 'true';

mix.disableNotifications();

Expand All @@ -19,13 +22,14 @@ mix
.ts('src/js/app.js', '.')
.version();

// Process CSS with conditional minification
['app', 'vendor'].forEach((style) => {
mix
.sass(`src/scss/${style}.scss`, '.')
.options({ postCss: [require('cssnano')] });
.options({ postCss: shouldMinify ? [require('cssnano')] : [] }); // Conditionally include cssnano
mix
.sass(`src/scss/${style}-dark.scss`, '.')
.options({ postCss: [require('cssnano')] });
.options({ postCss: shouldMinify ? [require('cssnano')] : [] }); // Conditionally include cssnano
});

mix.webpackConfig({
Expand Down Expand Up @@ -86,7 +90,7 @@ if (mix.inProduction()) {
mix.extract();
mix.then(() => {
const { code } = babel.transformFileSync('src/js/inline.js', {
minified: true,
minified: shouldMinify, // Conditionally set minification for JS
presets: [
[
'@babel/preset-env',
Expand Down

0 comments on commit 317c32b

Please sign in to comment.