Browse Source

Reduse http requests, use cache

Volodymyr Tkach 2 years ago
parent
commit
0a68a4b169
1 changed files with 40 additions and 27 deletions
  1. 40 27
      assets.sh

+ 40 - 27
assets.sh

@@ -1,53 +1,66 @@
 #!/bin/bash
 
+CURRENT_DIR="$(dirname "$0")"
+
 SOURCE_FILE=${1}
 TARGET_FILE=${2}
 
 # Check if source file is set
 if [[ "${SOURCE_FILE}" = "" ]]; then
-    echo "Source file is not set"
-    exit 1
+	echo "Source file is not set"
+	exit 1
 fi
 
 # Check if target file is set
 if [[ "${TARGET_FILE}" = "" ]]; then
-    # Try to generate from source file name
-    TARGET_FILE=$(echo "${SOURCE_FILE}" | sed 's/\.dev\.\(css\|js\)$/.\1/')
-    if [[ "${TARGET_FILE}" = "" ]]; then
-        echo "Target file is not set"
-        exit 1
-    fi
+	# Try to generate from source file name
+	TARGET_FILE=$(echo "${SOURCE_FILE}" | sed 's/\.dev\.\(css\|js\)$/.\1/')
+	if [[ "${TARGET_FILE}" = "" ]]; then
+		echo "Target file is not set"
+		exit 1
+	fi
 fi
 
 # Check if source file exists
 if [[ ! -f "${SOURCE_FILE}" ]]; then
-    echo "Source file does not exists: ${SOURCE_FILE}"
-    exit 1
+	echo "Source file does not exists: ${SOURCE_FILE}"
+	exit 1
 fi
 
 # Re-create empty target file
 if [[ -f "${TARGET_FILE}" ]]; then
-    rm ${TARGET_FILE}
+	rm ${TARGET_FILE}
 fi
 touch ${TARGET_FILE}
 
 IFS=""
 while read line; do
-    if [[ ${line} == "/*"* ]]; then
-        if [[ ${line} == *"*/" ]]; then
-            if [[ ${line} == *"import("* ]]; then
-                FILE_TO_IMPORT=$(echo "${line}" | grep -oP "/*\s?import\(\K[^)]+")
-                DATA_TO_IMPORT=$(curl -s "${FILE_TO_IMPORT}")
-                echo "${DATA_TO_IMPORT}" >> ${TARGET_FILE}
-            else
-                echo "${line}" >> ${TARGET_FILE}
-            fi
-        else
-            echo "${line}" >> ${TARGET_FILE}
-        fi
-    else
-        echo "${line}" >> ${TARGET_FILE}
-    fi
+	if [[ ${line} == "/*"* ]]; then
+		if [[ ${line} == *"*/" ]]; then
+			if [[ ${line} == *"import("* ]]; then
+				FILE_TO_IMPORT=$(echo "${line}" | grep -oP "/*\s?import\(\K[^)]+")
+				CACHE_NAME=$(echo "${FILE_TO_IMPORT}" | md5sum | cut -f1 -d" ")
+				CACHE_NAME=$(echo "${CACHE_NAME}-$(basename ${FILE_TO_IMPORT})")
+				CACHE_FILE="${CURRENT_DIR}/.cache/${CACHE_NAME}"
+
+				DATA_TO_IMPORT=""
+				if [[ ! -f "${CACHE_FILE}" ]]; then
+					DATA_TO_IMPORT=$(curl -s "${FILE_TO_IMPORT}")
+					echo "${DATA_TO_IMPORT}" > ${CACHE_FILE}
+				else
+					DATA_TO_IMPORT=$(cat ${CACHE_FILE})
+				fi
+
+				echo "${DATA_TO_IMPORT}" >> ${TARGET_FILE}
+			else
+				echo "${line}" >> ${TARGET_FILE}
+			fi
+		else
+			echo "${line}" >> ${TARGET_FILE}
+		fi
+	else
+		echo "${line}" >> ${TARGET_FILE}
+	fi
 done < ${SOURCE_FILE}
 
 # Minify target file (CSS, JS)
@@ -55,5 +68,5 @@ done < ${SOURCE_FILE}
 # sudo apt-get install yui-compressor
 CHECK_YUI_COMPRESSOR=$(command -v yui-compressor 2> /dev/null)
 if [[ "${CHECK_YUI_COMPRESSOR}" != "" ]]; then
-    yui-compressor ${TARGET_FILE} -o ${TARGET_FILE}
+	yui-compressor ${TARGET_FILE} -o ${TARGET_FILE}
 fi