From 8b650feca39c91c6befbe7d91e292479262b0664 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 6 Jul 2018 17:45:04 -0700 Subject: [PATCH] Preserve cache-busting URL prefix on js/css - closes #11 Also ensure we ship the .map files for better browser debugging. --- datasette_vega/__init__.py | 22 ++++++++++++++++++++-- setup.py | 9 +++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/datasette_vega/__init__.py b/datasette_vega/__init__.py index ea03f63..4173ee0 100644 --- a/datasette_vega/__init__.py +++ b/datasette_vega/__init__.py @@ -1,11 +1,29 @@ from datasette import hookimpl +import glob +import os + +cache = {} +static_dir = os.path.join( + os.path.dirname(__file__), 'static' +) + +def cached_filepaths_for_extension(extension): + pattern = os.path.join(static_dir, '*.{}'.format(extension)) + if pattern not in cache: + cache[pattern] = [ + "/-/static-plugins/datasette_vega/{}".format( + os.path.basename(g) + ) + for g in glob.glob(pattern) + ] + return cache[pattern] @hookimpl def extra_css_urls(): - return ["/-/static-plugins/datasette_vega/datasette-vega.css"] + return cached_filepaths_for_extension('css') @hookimpl def extra_js_urls(): - return ["/-/static-plugins/datasette_vega/datasette-vega.js"] + return cached_filepaths_for_extension('js') diff --git a/setup.py b/setup.py index f4e7ded..e2bb267 100644 --- a/setup.py +++ b/setup.py @@ -34,8 +34,8 @@ class BuildStatic(Command): check_output(['npm', 'install'], cwd=ROOT) check_output(['npm', 'run', 'build'], cwd=ROOT) check_output(['mkdir', '-p', 'datasette_vega/static'], cwd=ROOT) - check_output("mv build/static/js/*.js datasette_vega/static/datasette-vega.js", shell=True, cwd=ROOT) - check_output("mv build/static/css/*.css datasette_vega/static/datasette-vega.css", shell=True, cwd=ROOT) + check_output("mv build/static/js/* datasette_vega/static/", shell=True, cwd=ROOT) + check_output("mv build/static/css/* datasette_vega/static/", shell=True, cwd=ROOT) setup( @@ -55,8 +55,9 @@ setup( }, package_data={ 'datasette_vega': [ - 'static/datasette-vega.js', - 'static/datasette-vega.css', + 'static/*.js', + 'static/*.css', + 'static/*.map', ], }, cmdclass={