django-importmap
django-importmap copied to clipboard
Explain how to import local modules recursively
First, I want to say thank you very much for writing this package. I am using this package so well. This PR supplements how to import local modules recursively without modifying codebases.
Background
In importmap-rails, it support functionality to import all files in a specific folder by using pin_all_from.
For example, If you want to import all javascript files under app/javascript/src you can write as below:
# config/importmap.rb
pin_all_from 'app/javascript/src', under: 'src', to: 'src'
But, this package's README does not explains how to do it. And it seems lack this feature.
What does this PR do?
I added additional explanation of how to import local modules recursively like pin_all_from, as I explained above.
I have wondered which project I contribute between bolt-import and this package. But i have no experience with contributing django-plugin and I don't know how to contribute to bolt, too. So, I made decision to explaining trick without modifying codebase.
class ImportArguments(TypedDict):
under: str
to: str
def import_recursive(**kwargs: Unpack[ImportArguments]):
root_dir = settings.BASE_DIR
source_dir: str = str(root_dir / kwargs['under'])
source_files = glob(f"{source_dir}/**/*.js", recursive=True)
importmap_dict = {}
for source_file in source_files:
source_module = source_file.split('static/')[-1]
target_module = source_module.replace(source_dir, kwargs['to']).replace(".js", "")
importmap_dict[target_module] = static(source_module)
return importmap_dict
Implementing import_recursive function is not much difficult and useful trick.
If there are people who want to use SPA framework without building backend/frontend separately, this trick would be very helpful.
Links
- You can take a look how I use: https://github.com/kodingwarrior/memedex-backend/blob/main/jinja.py#L53-L61
- Also, you can setup auto completion by
jsconfig.json: https://github.com/kodingwarrior/memedex-backend/blob/main/jsconfig.json
Hey @malkoG — thanks for your patience! Just wanted to let you know that I've seen this. I'm most likely going to "archive" this Django package and focus on the Bolt iteration, and there's a couple big changes I'm envisioning in getting that to a 1.0. I will definitely keep this in mind for when I get to that part. (Regardless of what happens, I'll keep this package and code up and available so people can continue to use it or fork it if they want.)