From 8d5f3c8babe04556dde0b7975a249d5a9fc28da8 Mon Sep 17 00:00:00 2001 From: Hans5958 Date: Thu, 28 Apr 2022 13:10:22 +0000 Subject: [PATCH] Add build script Download all the jsDelivr stuff, and use Parcel to compact it --- .gitignore | 9 ++++++- netlify.toml | 3 +++ tools/ci/build-prod.sh | 16 +++++++++++++ tools/ci/cdn-to-local.py | 48 ++++++++++++++++++++++++++++++++++++++ tools/ci/postcss.config.js | 22 +++++++++++++++++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tools/ci/build-prod.sh create mode 100644 tools/ci/cdn-to-local.py create mode 100644 tools/ci/postcss.config.js diff --git a/.gitignore b/.gitignore index 72d3a65d..18d1395a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,11 @@ combined.js .vscode/ web/atlas-before-ids-migration.json *.pyc -tools/read-ids-temp.txt \ No newline at end of file +tools/read-ids-temp.txt +.venv/ +node_modules/ +dist*/ +package.json +package-lock.json +yarn.lock +.parcel-cache/ \ No newline at end of file diff --git a/netlify.toml b/netlify.toml index e69de29b..f7cc1051 100644 --- a/netlify.toml +++ b/netlify.toml @@ -0,0 +1,3 @@ +[build] + publish = "dist/" + command = "FILE=tools/ci/build-prod.sh; rm -rf dist/; if [ -f $FILE ]; then bash $FILE; else cp -r web/ dist/; fi" \ No newline at end of file diff --git a/tools/ci/build-prod.sh b/tools/ci/build-prod.sh new file mode 100644 index 00000000..f008403d --- /dev/null +++ b/tools/ci/build-prod.sh @@ -0,0 +1,16 @@ +# This command should be run on CI/Netlify enviroment! +# If you really wanted to run it, run it on the root. + +rm -rf dist-temp +rm -rf dist +cp -r web/ dist-temp/ +cp tools/ci/postcss.config.js ./ +cp tools/ci/package.json ./ +npm i +python tools/ci/cdn-to-local.py +npx parcel build dist-temp/index.html dist-temp/**.html --dist-dir "dist" --no-source-maps --no-content-hash +mkdir dist/_img +cp -r dist-temp/_img/canvas/ dist/_img/canvas/ +cp dist-temp/atlas.json dist +rm -rf dist-temp +rm -rf postcss.config.js \ No newline at end of file diff --git a/tools/ci/cdn-to-local.py b/tools/ci/cdn-to-local.py new file mode 100644 index 00000000..3181bf2c --- /dev/null +++ b/tools/ci/cdn-to-local.py @@ -0,0 +1,48 @@ +import glob +import string +import re +import hashlib +import os +import urllib.request + +cdns = [] + +def join_rel_path(path1, path2): + path = os.path.join(path1, path2) + path = re.sub(r"\/[^\/]+?\/\.", "", path) + return path + +for name in glob.glob("web/**.html"): + with open(name, 'r', encoding='utf-8') as file: + file_string = file.read() + urls = re.findall('"(https:\/\/cdn.jsdelivr.net\/(.+?))"', file_string) + for url_groups in urls: + url: string = url_groups[0] + os.makedirs("dist-temp/cdn/" + hashlib.md5(url.encode()).hexdigest(), exist_ok=True) + new_url = "cdn/" + hashlib.md5(url.encode()).hexdigest() + "/" + os.path.basename(url) + print(url) + urllib.request.urlretrieve(url, "dist-temp/" + new_url) + file_string = file_string.replace(url, new_url) + cdns.append((url, new_url, hashlib.md5(url.encode()).hexdigest())) + # print(file_string).replace("\?.+$", "") + name = name.replace('web/', 'dist-temp/') + with open(name, 'w', encoding='utf-8') as file: + file.write(file_string) + +for cdn in cdns: + parent_url, parent_new_url, hash = cdn + name = "dist-temp/" + parent_new_url + with open(name, 'r', encoding='utf-8') as file: + file_string = file.read() + urls = re.findall('\("(.\/(.+?))"\)', file_string) + for url_groups in urls: + url_orig = url_groups[0] + url: string = join_rel_path(parent_url, url_groups[0]) + url = re.sub("\?.+$", "", url) + os.makedirs("dist-temp/cdn/" + hashlib.md5(url.encode()).hexdigest(), exist_ok=True) + new_url = "cdn/" + hashlib.md5(url.encode()).hexdigest() + "/" + os.path.basename(url) + print(url) + urllib.request.urlretrieve(url, "dist-temp/" + new_url) + file_string = file_string.replace(url_orig, new_url.replace("cdn/", "../")) + with open(name, 'w', encoding='utf-8') as file: + file.write(file_string) \ No newline at end of file diff --git a/tools/ci/postcss.config.js b/tools/ci/postcss.config.js new file mode 100644 index 00000000..f15106b6 --- /dev/null +++ b/tools/ci/postcss.config.js @@ -0,0 +1,22 @@ +const purgecss = require("@fullhuman/postcss-purgecss"); + +const plugins = []; + +if (process.env.NODE_ENV === "production") { + plugins.push( + purgecss({ + content: [ + './dist-temp/*.html', + './dist-temp/**/*.html', + './dist-temp/*.js', + './dist-temp/**/*.js', + './dist-temp/*.svg', + './dist-temp/**/*.svg' + ] + }) + ); +} + +module.exports = { + plugins: plugins +}; \ No newline at end of file