Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ tests/venv/
tests/venv3/
tests/node_modules/
tests/assets/bundles/
tests/assets/django_webpack_loader_bundles/
tests/webpack-stats.json
tests/webpack-stats-app2.json

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ For more general information, view the [readme](README.md).
Releases are added to the
[github release page](https://github.com/ezhome/django-webpack-loader/releases).

## Unreleased
- Applies ignore rule before checking assets


## [1.1.0] -- 2021-06-18

- Added compatibility with `[email protected]`
Expand Down
9 changes: 6 additions & 3 deletions tests/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,17 @@
WEBPACK_LOADER = {
'DEFAULT': {
'CACHE': False,
'BUNDLE_DIR_NAME': 'bundles/',
'BUNDLE_DIR_NAME': 'django_webpack_loader_bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
},
'APP2': {
'CACHE': False,
'BUNDLE_DIR_NAME': 'bundles/',
'BUNDLE_DIR_NAME': 'django_webpack_loader_bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats-app2.json'),
}
},
'NO_IGNORE_APP': {
'IGNORE': [],
},
}

from django_jinja.builtins import DEFAULT_EXTENSIONS
Expand Down
4 changes: 2 additions & 2 deletions tests/app/tests/test_custom_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_bad_custom_loader(self):
with self.settings(WEBPACK_LOADER={
'DEFAULT': {
'CACHE': False,
'BUNDLE_DIR_NAME': 'bundles/',
'BUNDLE_DIR_NAME': 'django_webpack_loader_bundles/',
'LOADER_CLASS': loader_class
}
}):
Expand All @@ -54,7 +54,7 @@ def test_good_custom_loader(self):
with self.settings(WEBPACK_LOADER={
'DEFAULT': {
'CACHE': False,
'BUNDLE_DIR_NAME': 'bundles/',
'BUNDLE_DIR_NAME': 'django_webpack_loader_bundles/',
'LOADER_CLASS': loader_class,
}
}):
Expand Down
49 changes: 33 additions & 16 deletions tests/app/tests/test_webpack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import time
from shutil import rmtree
from subprocess import call
from threading import Thread

Expand All @@ -19,13 +20,17 @@
from webpack_loader.utils import get_loader


BUNDLE_PATH = os.path.join(settings.BASE_DIR, 'assets/bundles/')
BUNDLE_PATH = os.path.join(settings.BASE_DIR, 'assets/django_webpack_loader_bundles/')
DEFAULT_CONFIG = 'DEFAULT'


class LoaderTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.cleanup_bundles_folder()

def cleanup_bundles_folder(self):
rmtree('./assets/django_webpack_loader_bundles', ignore_errors=True)

def compile_bundles(self, config, wait=None):
if wait:
Expand All @@ -39,7 +44,7 @@ def test_config_check(self):
from webpack_loader.errors import BAD_CONFIG_ERROR

with self.settings(WEBPACK_LOADER={
'BUNDLE_DIR_NAME': 'bundles/',
'BUNDLE_DIR_NAME': 'django_webpack_loader_bundles/',
'STATS_FILE': 'webpack-stats.json',
}):
errors = webpack_cfg_check(None)
Expand All @@ -64,8 +69,20 @@ def test_simple_and_css_extract(self):
self.assertEqual(len(chunks), 1)

files = assets['assets']
self.assertEqual(files['main.css']['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.css'))
self.assertEqual(files['main.js']['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.js'))
self.assertEqual(files['main.css']['path'], os.path.join(settings.BASE_DIR, 'assets/django_webpack_loader_bundles/main.css'))
self.assertEqual(files['main.js']['path'], os.path.join(settings.BASE_DIR, 'assets/django_webpack_loader_bundles/main.js'))

def test_default_ignore_config_ignores_map_files(self):
self.compile_bundles('webpack.config.sourcemaps.js')
chunks = get_loader('NO_IGNORE_APP').get_bundle('main')
has_map_files_chunks = any([".map" in chunk["name"] for chunk in chunks])

self.assertTrue(has_map_files_chunks)

chunks = get_loader(DEFAULT_CONFIG).get_bundle('main')
has_map_files_chunks = any([".map" in chunk["name"] for chunk in chunks])

self.assertFalse(has_map_files_chunks)

def test_js_gzip_extract(self):
self.compile_bundles('webpack.config.gzipTest.js')
Expand All @@ -78,8 +95,8 @@ def test_js_gzip_extract(self):
self.assertEqual(len(chunks), 1)

files = assets['assets']
self.assertEqual(files['main.css']['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.css'))
self.assertEqual(files['main.js.gz']['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.js.gz'))
self.assertEqual(files['main.css']['path'], os.path.join(settings.BASE_DIR, 'assets/django_webpack_loader_bundles/main.css'))
self.assertEqual(files['main.js.gz']['path'], os.path.join(settings.BASE_DIR, 'assets/django_webpack_loader_bundles/main.js.gz'))

def test_static_url(self):
self.compile_bundles('webpack.config.publicPath.js')
Expand All @@ -98,26 +115,26 @@ def test_code_spliting(self):
self.assertEquals(len(chunks), 1)

files = assets['assets']
self.assertEqual(files['main.js']['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.js'))
self.assertEqual(files['vendors.js']['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/vendors.js'))
self.assertEqual(files['main.js']['path'], os.path.join(settings.BASE_DIR, 'assets/django_webpack_loader_bundles/main.js'))
self.assertEqual(files['vendors.js']['path'], os.path.join(settings.BASE_DIR, 'assets/django_webpack_loader_bundles/vendors.js'))

def test_templatetags(self):
self.compile_bundles('webpack.config.simple.js')
self.compile_bundles('webpack.config.app2.js')
view = TemplateView.as_view(template_name='home.html')
request = self.factory.get('/')
result = view(request)
self.assertIn('<link type="text/css" href="/static/bundles/main.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/main.js" async charset="UTF-8"></script>', result.rendered_content)
self.assertIn('<link type="text/css" href="/static/django_webpack_loader_bundles/main.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/django_webpack_loader_bundles/main.js" async charset="UTF-8"></script>', result.rendered_content)

self.assertIn('<link type="text/css" href="/static/bundles/app2.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/app2.js" ></script>', result.rendered_content)
self.assertIn('<link type="text/css" href="/static/django_webpack_loader_bundles/app2.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/django_webpack_loader_bundles/app2.js" ></script>', result.rendered_content)
self.assertIn('<img src="/static/my-image.png"/>', result.rendered_content)

view = TemplateView.as_view(template_name='only_files.html')
result = view(request)
self.assertIn("var contentCss = '/static/bundles/main.css'", result.rendered_content)
self.assertIn("var contentJS = '/static/bundles/main.js'", result.rendered_content)
self.assertIn("var contentCss = '/static/django_webpack_loader_bundles/main.css'", result.rendered_content)
self.assertIn("var contentJS = '/static/django_webpack_loader_bundles/main.js'", result.rendered_content)

self.compile_bundles('webpack.config.publicPath.js')
view = TemplateView.as_view(template_name='home.html')
Expand Down Expand Up @@ -156,8 +173,8 @@ def test_jinja2(self):
with self.settings(**settings):
request = self.factory.get('/')
result = view(request)
self.assertIn('<link type="text/css" href="/static/bundles/main.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/main.js" async charset="UTF-8"></script>', result.rendered_content)
self.assertIn('<link type="text/css" href="/static/django_webpack_loader_bundles/main.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/django_webpack_loader_bundles/main.js" async charset="UTF-8"></script>', result.rendered_content)

def test_reporting_errors(self):
self.compile_bundles('webpack.config.error.js')
Expand Down
2 changes: 1 addition & 1 deletion tests/webpack.config.error.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
context: __dirname,
entry: './assets/js/bad_index',
output: {
path: path.resolve('./assets/bundles/'),
path: path.resolve('./assets/django_webpack_loader_bundles/'),
filename: "[name].js",
},

Expand Down
2 changes: 1 addition & 1 deletion tests/webpack.config.gzipTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
context: __dirname,
entry: './assets/js/index',
output: {
path: path.resolve('./assets/bundles/'),
path: path.resolve('./assets/django_webpack_loader_bundles/'),
filename: "[name].js.gz"
},

Expand Down
2 changes: 1 addition & 1 deletion tests/webpack.config.simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
context: __dirname,
entry: './assets/js/index',
output: {
path: path.resolve('./assets/bundles/'),
path: path.resolve('./assets/django_webpack_loader_bundles/'),
filename: "[name].js"
},

Expand Down
42 changes: 42 additions & 0 deletions tests/webpack.config.sourcemaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var path = require("path");
var webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker');
var MiniCssExtractPlugin = require('mini-css-extract-plugin');


module.exports = {
context: __dirname,
entry: './assets/js/index',
devtool: 'source-map',
output: {
path: path.resolve('./assets/django_webpack_loader_bundles/'),
filename: "[name].js"
},

plugins: [
new MiniCssExtractPlugin(),
new BundleTracker({path: __dirname, filename: './webpack-stats.json'}),
],

module: {
rules: [
// we pass the output from babel loader to react-hot loader
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react']
}
}
},
{ test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'], }
],
},

resolve: {
modules: ['node_modules'],
extensions: ['.js', '.jsx']
},
}
2 changes: 1 addition & 1 deletion tests/webpack.config.split.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
context: __dirname,
entry: './assets/js/index',
output: {
path: path.resolve('./assets/bundles/'),
path: path.resolve('./assets/django_webpack_loader_bundles/'),
filename: "[name].js",
chunkFilename: "[name].js"
},
Expand Down
22 changes: 16 additions & 6 deletions webpack_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ def get_assets(self):
return self.load_assets()

def filter_chunks(self, chunks):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check all calls to filter_chunks?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, could you please track this change of behavior in a Unreleased section of the CHANGELOG?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, there is only one call at

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))
filtered_chunks = self.filter_chunks(chunks)
for chunk in filtered_chunks:
asset = assets['assets'][chunk]
if asset is None:
raise WebpackBundleLookupError('Cannot resolve asset {0}.'.format(chunk))

assets = self.get_assets()
filtered_chunks = []

for chunk in chunks:
ignore = any(regex.match(chunk)
for regex in self.config['ignores'])
if not ignore:
files = assets['assets']
url = self.get_chunk_url(files[chunk])
yield { 'name': chunk, 'url': url }
filtered_chunks.append(chunk)

return filtered_chunks

def map_chunk_files_to_url(self, chunks):
assets = self.get_assets()
files = assets['assets']

for chunk in chunks:
url = self.get_chunk_url(files[chunk])
yield { 'name': chunk, 'url': url }

def get_chunk_url(self, chunk_file):
public_path = chunk_file.get('publicPath')
Expand Down Expand Up @@ -86,12 +94,14 @@ def get_bundle(self, bundle_name):
if chunks is None:
raise WebpackBundleLookupError('Cannot resolve bundle {0}.'.format(bundle_name))

for chunk in chunks:
filtered_chunks = self.filter_chunks(chunks)

for chunk in filtered_chunks:
asset = assets['assets'][chunk]
if asset is None:
raise WebpackBundleLookupError('Cannot resolve asset {0}.'.format(chunk))

return self.filter_chunks(chunks)
return self.map_chunk_files_to_url(filtered_chunks)

elif assets.get('status') == 'error':
if 'file' not in assets:
Expand Down