Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain how to import local modules recursively #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

malkoG
Copy link

@malkoG malkoG commented Jun 29, 2024

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

@pullapprove4 pullapprove4 bot requested a review from davegaeddert June 29, 2024 05:54
@davegaeddert
Copy link
Member

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants