From b0499be73e9c83ec93ed254f7ed636e8dba1567c Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Wed, 30 Jun 2021 14:34:55 +0100 Subject: [PATCH] Get SHA'ed path via regexp, not file contents The JavaScript files produced by rollup have SHAs in their names that are not just generated from their final contents*. Because of this, it is tricky to determine their filename. This proposes a different approach to the file_fingerprint filter, changing it so it gets the SHA'ed filename by regexp rather than using the same hashing as the gulp task. *This issue contains some useful information on how rollup generates its hashes: https://github.com/rollup/rollup/issues/2839 --- gulpfile.js | 11 +++-------- lib/utils.py | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index ea48569c..e167009b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -199,14 +199,9 @@ const defaultTask = parallel( copy.html5shiv, copy.images, scss.compile, - series( - parallel( - javascripts.details, - javascripts.sharingButton, - javascripts.relativeDates - ), - restoreOriginalJavascriptFiles, - ) + javascripts.details, + javascripts.sharingButton, + javascripts.relativeDates ); exports.default = defaultTask; diff --git a/lib/utils.py b/lib/utils.py index bd707404..26efcf47 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -1,5 +1,5 @@ -import hashlib import os +import re from pathlib import Path from jinja2 import Markup, escape @@ -20,7 +20,14 @@ def paragraphize(value, classes="govuk-body-l govuk-!-margin-bottom-4"): def file_fingerprint(path, root=DIST): - contents = open(str(root) + path, 'rb').read() - hash = hashlib.sha256(contents).hexdigest()[:8] - filename, extension = os.path.splitext(path) - return f'{filename}-{hash}{extension}' + path = Path(path).relative_to('/') # path comes in as absolute, rooted to the dist folder + path_regex = re.compile(f'^{path.stem}-[0-9a-z]{{8}}{path.suffix}$') # regexp based on the filename + a 8 char hash + matches = [ + filename for filename + in os.listdir(str(root / path.parent)) + if path_regex.search(filename)] + + if len(matches) == 0: + raise OSError(f'{str(root / path.parent / path.stem)}-[hash]{path.suffix} referenced but not available') + + return f'/{path.parent}/{matches[0]}'