Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically generate WebStaticData.h from source files #1671

Closed
Wandmalfarbe opened this issue Aug 22, 2018 · 6 comments
Closed

Automatically generate WebStaticData.h from source files #1671

Wandmalfarbe opened this issue Aug 22, 2018 · 6 comments
Labels
Category: Build Related to building/IDE/releases Type: Enhancement Improve something already present
Projects

Comments

@Wandmalfarbe
Copy link

It would be beneficial to store the static assets in their original file format (e.g. favicon.ico or github-icon.svg) and use a script to convert them to C code needed for WebStaticData.h. This way the assets can be maintained without poking around in C code or forgetting a null terminator after editing the CSS.

The added bonus would be that one can optimize the files with external plugins like svgo or jsmin before writing them to the file. One can take a look at WI-PWN for inspiration although I would use a simple (bash/python/...) script to generate the file.

@Grovkillen Grovkillen added Type: Enhancement Improve something already present Category: Build Related to building/IDE/releases labels Aug 22, 2018
@Grovkillen
Copy link
Member

This is a good suggestion and we'll need to do something like this for the future. Thanks for making an issue. 👍

@TD-er
Copy link
Member

TD-er commented Aug 22, 2018

That's one thing that will be added in the future.
It will also allow users to add new icons themselves.

Only reason it hasn't yet is because I wasn't sure yet about the format and I was also thinking about loading those assets as files to the SPIFF.

Thanks for the links though!

@Wandmalfarbe
Copy link
Author

Just for completeness: I created a script for this task a while ago. It is basically a copy of a script by Erick B. Tedeschi and spacehuhn. The repo for the esp8266_deauther also contains another (python) script for this task.

#!/bin/bash

#
# This script walks through the assets folder and minifys all JS, HTML, CSS and SVG files. It also generates
# the corresponding constants that are added to the data.h file on esp8266_deauther folder.
#
# @Author Erick B. Tedeschi < erickbt86 [at] gmail [dot] com >
# @Author Wandmalfarbe https://github.com/Wandmalfarbe
#

outputfile="$(pwd)/data_h_temp"

rm $outputfile

function minify_html_css {
	file=$1
	curl -X POST -s --data-urlencode "input@$file" http://html-minifier.com/raw > /tmp/converter.temp
}

function minify_js {
	file=$1
	curl -X POST -s --data-urlencode "input@$file" https://javascript-minifier.com/raw > /tmp/converter.temp
}

function minify_svg {
	file=$1
	svgo -i /Users/User/Desktop/icons/tools.svg -o - > /tmp/converter.temp
}

function ascii2hexCstyle {
	file_name=$(constFileName $1)
	result=$(cat /tmp/converter.temp | hexdump -ve '1/1 "0x%.2x,"')
	result=$(echo $result | sed 's/,$//')
	echo "const char DATA_${file_name}[] PROGMEM = {$result};"
}

function constFileName {
	extension=$(echo $1 | egrep -io "(json|svg|css|js|html)$" | tr "[:lower:]" "[:upper:]")
	file=$(echo $1 | sed 's/\.json//' | sed 's/\.svg//' | sed 's/\.css//' | sed 's/\.html//' | sed 's/\.js//' | sed 's/\.\///' | tr '/' '_' | tr '.' '_' | tr '-' '_' | tr "[:lower:]" "[:upper:]")
	underscore="_"
	echo $file$underscore$extension
}


cd assets
file_list=$(find . -type f)

for file in $file_list; do
	echo "Processing: $file"
	file_name=$(constFileName $file)
	echo "  Array Name: $file_name"

	if [[ "$file" == *.min.js ]]; then
		echo "  JS already minified"
		cat $file > /tmp/converter.temp
		ascii2hexCstyle $file >> $outputfile
	elif [[ "$file" == *.js ]]; then
		echo "  JS minify"
		minify_js $file
		ascii2hexCstyle $file >> $outputfile
	elif [[ "$file" == *.min.css ]]; then
		echo "  CSS already minified"
		cat $file > /tmp/converter.temp
		ascii2hexCstyle $file >> $outputfile
	elif [[ "$file" == *.html ]] || [[ "$file" == *.css ]]; then
		echo "  HTML and CSS minify"
		minify_html_css $file
		ascii2hexCstyle $file >> $outputfile
	elif [[ "$file" == *.svg ]]; then
		echo "  SVG minify"
		minify_svg $file
		ascii2hexCstyle $file >> $outputfile
	else
		echo "  without minifier"
		cat $file > /tmp/converter.temp
		ascii2hexCstyle $file >> $outputfile
	fi
	echo ""
	sleep 1
done

@TD-er TD-er added this to Needs triage in Clean up via automation Aug 22, 2018
@TD-er
Copy link
Member

TD-er commented Oct 31, 2018

I will try to do this, so we can minify all embedded JavaScript.
Maybe it is also a good idea to leave the code readable for builds which have 4 MB flash.

@TD-er TD-er moved this from Needs triage to In Progress in Clean up Oct 31, 2018
@Grovkillen
Copy link
Member

Ooooh, sweet music to my ears @TD-er :)

@tonhuisman
Copy link
Contributor

This seems to be solved, so can be closed.

@TD-er TD-er closed this as completed Sep 4, 2023
Clean up automation moved this from In Progress to Closed Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Build Related to building/IDE/releases Type: Enhancement Improve something already present
Projects
Clean up
  
Closed
Development

No branches or pull requests

4 participants