Skip to content

Commit 4af61d1

Browse files
authored
fix: fix the issue where the application's assets_path_ignore is being overridden (#8)
* fix: fix the issue where the application's assets_path_ignore is being overridden * test: add more tests * style: fix format
1 parent 65e19d9 commit 4af61d1

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed

dash_vite_plugin/plugin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,11 @@ def _set_assets_path_ignore(self, app: Dash) -> None:
181181
# Remove prefix2 prefix
182182
assets_to_ignore.append(path[len(prefix2) :])
183183

184-
# Set as assets_path_ignore if any paths were found
184+
# Set assets_path_ignore if any paths were found
185185
if assets_to_ignore:
186-
app.config.assets_path_ignore = assets_to_ignore
186+
if not app.config.assets_path_ignore:
187+
app.config.assets_path_ignore = []
188+
app.config.assets_path_ignore.extend(assets_to_ignore)
187189

188190
def _should_skip_build(self) -> bool:
189191
"""

tests/test_plugin.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,114 @@ def __init__(self):
730730
expected_ignore = ['css', 'js']
731731
assert mock_app.config.assets_path_ignore == expected_ignore
732732

733+
def test_set_assets_path_ignore_extends_existing(self):
734+
"""Test _set_assets_path_ignore method extends existing assets_path_ignore."""
735+
build_assets_paths = ['./assets/css', 'assets/js']
736+
entry_js_paths = ['assets/js/main.js']
737+
npm_packages = [NpmPackage('react')]
738+
739+
plugin = VitePlugin(
740+
build_assets_paths=build_assets_paths, entry_js_paths=entry_js_paths, npm_packages=npm_packages
741+
)
742+
743+
# Create a mock Dash app with existing assets_path_ignore
744+
class MockConfig:
745+
def __init__(self):
746+
self.assets_folder = 'assets'
747+
self.assets_path_ignore = ['existing/ignore']
748+
749+
mock_app = MagicMock()
750+
mock_app.config = MockConfig()
751+
752+
# Call _set_assets_path_ignore
753+
plugin._set_assets_path_ignore(mock_app)
754+
755+
# Check that assets_path_ignore was extended correctly
756+
expected_ignore = ['existing/ignore', 'css', 'js']
757+
assert mock_app.config.assets_path_ignore == expected_ignore
758+
759+
def test_set_assets_path_ignore_no_matching_paths(self):
760+
"""Test _set_assets_path_ignore method when no paths match assets_dir_name."""
761+
build_assets_paths = ['other/css', './public/js']
762+
entry_js_paths = ['assets/js/main.js']
763+
npm_packages = [NpmPackage('react')]
764+
765+
plugin = VitePlugin(
766+
build_assets_paths=build_assets_paths, entry_js_paths=entry_js_paths, npm_packages=npm_packages
767+
)
768+
769+
# Create a mock Dash app with real config object
770+
class MockConfig:
771+
def __init__(self):
772+
self.assets_folder = 'assets'
773+
self.assets_path_ignore = []
774+
775+
mock_app = MagicMock()
776+
mock_app.config = MockConfig()
777+
778+
# Call _set_assets_path_ignore
779+
plugin._set_assets_path_ignore(mock_app)
780+
781+
# Check that assets_path_ignore remains empty
782+
assert mock_app.config.assets_path_ignore == []
783+
784+
def test_set_assets_path_ignore_empty_build_assets_paths(self):
785+
"""Test _set_assets_path_ignore method when build_assets_paths is empty."""
786+
build_assets_paths = []
787+
entry_js_paths = ['assets/js/main.js']
788+
npm_packages = [NpmPackage('react')]
789+
790+
plugin = VitePlugin(
791+
build_assets_paths=build_assets_paths, entry_js_paths=entry_js_paths, npm_packages=npm_packages
792+
)
793+
794+
# Create a mock Dash app with real config object
795+
class MockConfig:
796+
def __init__(self):
797+
self.assets_folder = 'assets'
798+
self.assets_path_ignore = []
799+
800+
mock_app = MagicMock()
801+
mock_app.config = MockConfig()
802+
803+
# Call _set_assets_path_ignore
804+
plugin._set_assets_path_ignore(mock_app)
805+
806+
# Check that assets_path_ignore remains empty
807+
assert mock_app.config.assets_path_ignore == []
808+
809+
def test_set_assets_path_ignore_complex_paths(self):
810+
"""Test _set_assets_path_ignore method with complex paths."""
811+
build_assets_paths = [
812+
'assets/js',
813+
'./assets/css/styles',
814+
'assets/images/icons',
815+
'public/assets/js', # Should not match
816+
'./public/css', # Should not match
817+
]
818+
entry_js_paths = ['assets/js/main.js']
819+
npm_packages = [NpmPackage('react')]
820+
821+
plugin = VitePlugin(
822+
build_assets_paths=build_assets_paths, entry_js_paths=entry_js_paths, npm_packages=npm_packages
823+
)
824+
825+
# Create a mock Dash app with real config object
826+
class MockConfig:
827+
def __init__(self):
828+
self.assets_folder = 'assets'
829+
self.assets_path_ignore = []
830+
831+
mock_app = MagicMock()
832+
mock_app.config = MockConfig()
833+
834+
# Call _set_assets_path_ignore
835+
plugin._set_assets_path_ignore(mock_app)
836+
837+
# Check that assets_path_ignore contains the correct paths
838+
expected_ignore = ['js', 'css/styles', 'images/icons']
839+
assert mock_app.config.assets_path_ignore == expected_ignore
840+
733841
def test_build_assets_with_vite(self):
734842
"""Test _build_assets_with_vite method."""
735843
build_assets_paths = ['assets/js']

0 commit comments

Comments
 (0)