From a3ba72860cebe0861f511048640ea087b2b2264a Mon Sep 17 00:00:00 2001 From: Josue Ttito Date: Thu, 9 Nov 2017 12:11:25 +0100 Subject: [PATCH 1/7] Add gzip on configuration --- README.md | 11 +++++++++-- webpack_loader/config.py | 7 ++++++- webpack_loader/loader.py | 11 ++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d08e4374..c5776419 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# django-webpack-loader +# django-webpack-loader for gzip + +Different from the original repository, it appends an extension in the creation of the tags (as .gzip). The append is also dynamic, and can be done for css or js [![Join the chat at https://gitter.im/owais/django-webpack-loader](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/owais/django-webpack-loader?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build Status](https://travis-ci.org/owais/django-webpack-loader.svg?branch=master)](https://travis-ci.org/owais/django-webpack-loader) @@ -98,7 +100,12 @@ WEBPACK_LOADER = { 'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'), 'POLL_INTERVAL': 0.1, 'TIMEOUT': None, - 'IGNORE': ['.+\.hot-update.js', '.+\.map'] + 'IGNORE': ['.+\.hot-update.js', '.+\.map'], + 'APPEND_URL': { + 'ENABLE': not DEBUG, + 'CSS': '.gz', + 'JS': '.gz' + } } } ``` diff --git a/webpack_loader/config.py b/webpack_loader/config.py index 39ff2b1a..f00bb6cf 100644 --- a/webpack_loader/config.py +++ b/webpack_loader/config.py @@ -14,7 +14,12 @@ # FIXME: Explore usage of fsnotify 'POLL_INTERVAL': 0.1, 'TIMEOUT': None, - 'IGNORE': ['.+\.hot-update.js', '.+\.map'] + 'IGNORE': ['.+\.hot-update.js', '.+\.map'], + 'APPEND_URL': { + 'ENABLE': not settings.DEBUG, + 'CSS': '.gz', + 'JS': '.gz' + } } } diff --git a/webpack_loader/loader.py b/webpack_loader/loader.py index b366dc3a..d7647c10 100644 --- a/webpack_loader/loader.py +++ b/webpack_loader/loader.py @@ -47,6 +47,7 @@ def filter_chunks(self, chunks): yield chunk def get_chunk_url(self, chunk): + # print('config', self.config) public_path = chunk.get('publicPath') if public_path: return public_path @@ -54,6 +55,13 @@ def get_chunk_url(self, chunk): relpath = '{0}{1}'.format( self.config['BUNDLE_DIR_NAME'], chunk['name'] ) + + if self.config['APPEND_URL']['ENABLE']: + if relpath.endswith('.css'): + relpath += self.config['APPEND_URL']['CSS'] + if relpath.endswith('.js'): + relpath += self.config['APPEND_URL']['JS'] + return staticfiles_storage.url(relpath) def get_bundle(self, bundle_name): @@ -80,7 +88,8 @@ def get_bundle(self, bundle_name): if assets.get('status') == 'done': chunks = assets['chunks'].get(bundle_name, None) if chunks is None: - raise WebpackBundleLookupError('Cannot resolve bundle {0}.'.format(bundle_name)) + raise WebpackBundleLookupError( + 'Cannot resolve bundle {0}.'.format(bundle_name)) return self.filter_chunks(chunks) elif assets.get('status') == 'error': From d6836313a973aa22764f97f62fc0d4942267f01a Mon Sep 17 00:00:00 2001 From: Josue Ttito Date: Thu, 9 Nov 2017 12:12:13 +0100 Subject: [PATCH 2/7] cleaning prints --- webpack_loader/loader.py | 1 - 1 file changed, 1 deletion(-) diff --git a/webpack_loader/loader.py b/webpack_loader/loader.py index d7647c10..8ad31dbf 100644 --- a/webpack_loader/loader.py +++ b/webpack_loader/loader.py @@ -47,7 +47,6 @@ def filter_chunks(self, chunks): yield chunk def get_chunk_url(self, chunk): - # print('config', self.config) public_path = chunk.get('publicPath') if public_path: return public_path From 03066aa85fdad1b1041a77a793079d8cd65ec5d8 Mon Sep 17 00:00:00 2001 From: Josue Ttito Date: Wed, 13 Dec 2017 07:11:17 -0500 Subject: [PATCH 3/7] Move extension to template tag --- webpack_loader/config.py | 7 +------ webpack_loader/loader.py | 8 -------- webpack_loader/templatetags/webpack_loader.py | 8 ++++++-- webpack_loader/utils.py | 7 ++++--- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/webpack_loader/config.py b/webpack_loader/config.py index f00bb6cf..39ff2b1a 100644 --- a/webpack_loader/config.py +++ b/webpack_loader/config.py @@ -14,12 +14,7 @@ # FIXME: Explore usage of fsnotify 'POLL_INTERVAL': 0.1, 'TIMEOUT': None, - 'IGNORE': ['.+\.hot-update.js', '.+\.map'], - 'APPEND_URL': { - 'ENABLE': not settings.DEBUG, - 'CSS': '.gz', - 'JS': '.gz' - } + 'IGNORE': ['.+\.hot-update.js', '.+\.map'] } } diff --git a/webpack_loader/loader.py b/webpack_loader/loader.py index 8ad31dbf..337368de 100644 --- a/webpack_loader/loader.py +++ b/webpack_loader/loader.py @@ -50,17 +50,9 @@ def get_chunk_url(self, chunk): public_path = chunk.get('publicPath') if public_path: return public_path - relpath = '{0}{1}'.format( self.config['BUNDLE_DIR_NAME'], chunk['name'] ) - - if self.config['APPEND_URL']['ENABLE']: - if relpath.endswith('.css'): - relpath += self.config['APPEND_URL']['CSS'] - if relpath.endswith('.js'): - relpath += self.config['APPEND_URL']['JS'] - return staticfiles_storage.url(relpath) def get_bundle(self, bundle_name): diff --git a/webpack_loader/templatetags/webpack_loader.py b/webpack_loader/templatetags/webpack_loader.py index d1e87ce7..61bffde0 100644 --- a/webpack_loader/templatetags/webpack_loader.py +++ b/webpack_loader/templatetags/webpack_loader.py @@ -8,8 +8,12 @@ @register.simple_tag -def render_bundle(bundle_name, extension=None, config='DEFAULT', attrs=''): - tags = utils.get_as_tags(bundle_name, extension=extension, config=config, attrs=attrs) +def render_bundle( + bundle_name, extension=None, config='DEFAULT', suffix='', attrs=''): + tags = utils.get_as_tags( + bundle_name, extension=extension, config=config, + suffix=suffix, attrs=attrs + ) return mark_safe('\n'.join(tags)) diff --git a/webpack_loader/utils.py b/webpack_loader/utils.py index e7b7b2f3..0c3e1e91 100644 --- a/webpack_loader/utils.py +++ b/webpack_loader/utils.py @@ -31,7 +31,8 @@ def get_files(bundle_name, extension=None, config='DEFAULT'): return list(_get_bundle(bundle_name, extension, config)) -def get_as_tags(bundle_name, extension=None, config='DEFAULT', attrs=''): +def get_as_tags( + bundle_name, extension=None, config='DEFAULT', suffix='', attrs=''): ''' Get a list of formatted ' - ).format(chunk['url'], attrs)) + ).format(''.join([chunk['url'], suffix]), attrs)) elif chunk['name'].endswith(('.css', '.css.gz')): tags.append(( '' - ).format(chunk['url'], attrs)) + ).format(''.join([chunk['url'], suffix]), attrs)) return tags From c930dab73578f245afc38e1242b2c3b4cb5bdaf5 Mon Sep 17 00:00:00 2001 From: Josue Ttito Date: Wed, 13 Dec 2017 07:13:36 -0500 Subject: [PATCH 4/7] Updated README.md, to append suffix in template tag --- README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c5776419..dcd9d000 100644 --- a/README.md +++ b/README.md @@ -100,12 +100,7 @@ WEBPACK_LOADER = { 'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'), 'POLL_INTERVAL': 0.1, 'TIMEOUT': None, - 'IGNORE': ['.+\.hot-update.js', '.+\.map'], - 'APPEND_URL': { - 'ENABLE': not DEBUG, - 'CSS': '.gz', - 'JS': '.gz' - } + 'IGNORE': ['.+\.hot-update.js', '.+\.map'] } } ``` @@ -266,6 +261,7 @@ WEBPACK_LOADER = { {% render_bundle 'main' 'js' 'DEFAULT' attrs='async chatset="UTF-8"'%} + {% render_bundle 'main' 'js' 'DEFAULT' attrs='async chatset="UTF-8"' suffix=".gz" %} ``` From f9aba7e8797095de458d8db4ee4a31e61a562508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lins?= Date: Mon, 23 Aug 2021 19:06:39 -0300 Subject: [PATCH 5/7] Adds append extensions test --- tests/app/templates/append_extensions.html | 13 +++++++++++++ tests/app/tests/test_webpack.py | 8 ++++++++ tests/package.json | 3 ++- tests/webpack.config.gzipTest.js | 4 +++- 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/app/templates/append_extensions.html diff --git a/tests/app/templates/append_extensions.html b/tests/app/templates/append_extensions.html new file mode 100644 index 00000000..4c67f7b9 --- /dev/null +++ b/tests/app/templates/append_extensions.html @@ -0,0 +1,13 @@ +{% load render_bundle from webpack_loader %} + + + + + Example + {% render_bundle 'main' 'css' %} + + + + {% render_bundle 'main' 'js' suffix='.gz' %} + + diff --git a/tests/app/tests/test_webpack.py b/tests/app/tests/test_webpack.py index 303abc28..2f8f52a2 100644 --- a/tests/app/tests/test_webpack.py +++ b/tests/app/tests/test_webpack.py @@ -156,6 +156,14 @@ def test_preload(self): self.assertIn('', result.rendered_content) self.assertIn('', result.rendered_content) + def test_append_extensions(self): + self.compile_bundles('webpack.config.gzipTest.js') + view = TemplateView.as_view(template_name='append_extensions.html') + request = self.factory.get('/') + result = view(request) + + self.assertIn('', result.rendered_content) + def test_jinja2(self): self.compile_bundles('webpack.config.simple.js') self.compile_bundles('webpack.config.app2.js') diff --git a/tests/package.json b/tests/package.json index c45c2202..1ed0b7d9 100644 --- a/tests/package.json +++ b/tests/package.json @@ -14,7 +14,8 @@ "mini-css-extract-plugin": "^0.9.0", "react": "^16.0.0", "webpack": "^4.0.0", - "webpack-bundle-tracker": "1.1.0", + "compression-webpack-plugin": "^6.1.1", + "webpack-bundle-tracker": "1.2.0", "webpack-cli": "^3.3.10", "webpack-dev-server": "^3.0.0" } diff --git a/tests/webpack.config.gzipTest.js b/tests/webpack.config.gzipTest.js index 4e1d5b6c..08ac12cb 100644 --- a/tests/webpack.config.gzipTest.js +++ b/tests/webpack.config.gzipTest.js @@ -2,6 +2,7 @@ var path = require("path"); var webpack = require('webpack'); var BundleTracker = require('webpack-bundle-tracker'); var MiniCssExtractPlugin = require('mini-css-extract-plugin'); +var CompressionPlugin = require('compression-webpack-plugin'); module.exports = { @@ -9,11 +10,12 @@ module.exports = { entry: './assets/js/index', output: { path: path.resolve('./assets/django_webpack_loader_bundles/'), - filename: "[name].js.gz" + filename: "[name].js" }, plugins: [ new MiniCssExtractPlugin(), + new CompressionPlugin(), new BundleTracker({path: __dirname, filename: './webpack-stats.json'}), ], From ec1e944472308356d6f4105e268e9e056af15ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lins?= Date: Mon, 23 Aug 2021 19:08:20 -0300 Subject: [PATCH 6/7] Improves formatting --- webpack_loader/loader.py | 5 ++--- webpack_loader/templatetags/webpack_loader.py | 3 +-- webpack_loader/utils.py | 3 +-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/webpack_loader/loader.py b/webpack_loader/loader.py index 1da3454d..346905c9 100644 --- a/webpack_loader/loader.py +++ b/webpack_loader/loader.py @@ -61,7 +61,7 @@ def get_chunk_url(self, chunk_file): public_path = chunk_file.get('publicPath') if public_path: return public_path -# Use os.path.normpath for Windows paths + # Use os.path.normpath for Windows paths relpath = os.path.normpath( os.path.join(self.config['BUNDLE_DIR_NAME'], chunk_file['name']) ) @@ -91,8 +91,7 @@ def get_bundle(self, bundle_name): if assets.get('status') == 'done': chunks = assets['chunks'].get(bundle_name, None) if chunks is None: - raise WebpackBundleLookupError( - 'Cannot resolve bundle {0}.'.format(bundle_name)) + raise WebpackBundleLookupError('Cannot resolve bundle {0}.'.format(bundle_name)) filtered_chunks = self.filter_chunks(chunks) diff --git a/webpack_loader/templatetags/webpack_loader.py b/webpack_loader/templatetags/webpack_loader.py index 4bce4202..0514edff 100644 --- a/webpack_loader/templatetags/webpack_loader.py +++ b/webpack_loader/templatetags/webpack_loader.py @@ -8,8 +8,7 @@ @register.simple_tag -def render_bundle( - bundle_name, extension=None, config='DEFAULT', suffix='', attrs='', is_preload=False): +def render_bundle(bundle_name, extension=None, config='DEFAULT', suffix='', attrs='', is_preload=False): tags = utils.get_as_tags( bundle_name, extension=extension, config=config, suffix=suffix, attrs=attrs, is_preload=is_preload diff --git a/webpack_loader/utils.py b/webpack_loader/utils.py index d2105eb2..008fef44 100644 --- a/webpack_loader/utils.py +++ b/webpack_loader/utils.py @@ -47,8 +47,7 @@ def get_files(bundle_name, extension=None, config='DEFAULT'): return list(_get_bundle(bundle_name, extension, config)) -def get_as_tags( - bundle_name, extension=None, config='DEFAULT', suffix='', attrs='', is_preload=False): +def get_as_tags(bundle_name, extension=None, config='DEFAULT', suffix='', attrs='', is_preload=False): ''' Get a list of formatted