Skip to content

Add Automated Files System Based Pack Generation#1455

Merged
justin808 merged 42 commits intomasterfrom
pulkitkkr/automate-bundle-creation
Aug 21, 2022
Merged

Add Automated Files System Based Pack Generation#1455
justin808 merged 42 commits intomasterfrom
pulkitkkr/automate-bundle-creation

Conversation

@pulkitkkr
Copy link
Copy Markdown
Contributor

@pulkitkkr pulkitkkr commented May 30, 2022

Summary

This PR adds

  • react_on_rails:generate_packs rake task
  • updates to View Helpers to generate component packs if not present.
  • add auto_load_bundle config option
  • add tests for packs generation

Detail

It has been a mundane task to create entry files, e.g.: client_entry.js inside the packs directory for a long time. The sole purpose of this file was to import a component and register it using the ReactOnRails.register API for usage in rails views.

The PR automates this process by introducing the concept of react on rail components. Any .jsx file components inside the ror_components directory will be detected and registered automatically for the usage on Rails View using the new helper react_component_with_bundle.

The name of the directory can be configured in src/config/initializers/react_on_rails.rb by setting

config.components_directory = "new_directory_name"


This change is Reviewable

@pulkitkkr pulkitkkr changed the title Add Automated Files System Based Pack Generation Feature WIP: Add Automated Files System Based Pack Generation Feature May 30, 2022
@justin808
Copy link
Copy Markdown
Member

@pulkitkkr what's remaining?

@pulkitkkr pulkitkkr force-pushed the pulkitkkr/automate-bundle-creation branch from 82ef861 to 1a72b37 Compare June 17, 2022 15:24
Comment thread lib/react_on_rails/packs_generator.rb Outdated
@justin808
Copy link
Copy Markdown
Member

Needs documentation.

Needs tests.

Can we have the spec/dummy app show this?

@pulkitkkr pulkitkkr force-pushed the pulkitkkr/automate-bundle-creation branch from 14dc032 to 7e01dd4 Compare July 5, 2022 22:55
@alexeyr-ci1 alexeyr-ci1 mentioned this pull request Jul 6, 2022
4 tasks
@justin808
Copy link
Copy Markdown
Member

@pulkitkkr what's remaining?

@pulkitkkr pulkitkkr force-pushed the pulkitkkr/automate-bundle-creation branch 3 times, most recently from d2cd1d4 to 31cca27 Compare July 7, 2022 23:56
Comment thread lib/react_on_rails/helper.rb Outdated
Comment thread lib/react_on_rails/configuration.rb Outdated
Comment thread lib/react_on_rails/packs_generator.rb Outdated
Comment thread lib/react_on_rails/packs_generator.rb Outdated
@alexeyr-ci1
Copy link
Copy Markdown
Contributor

The tests look fine. If the .jsx files are supposed to be empty, maybe make it clear by making them consist of # Empty test component comment?

@alexeyr-ci1
Copy link
Copy Markdown
Contributor

Also is load_bundle a good name for the added option? It doesn't look particularly related to components. But I am not certain what should replace it, if anything. load_component_bundle? load_component_pack?

@justin808
Copy link
Copy Markdown
Member

@alexeyr-ci1 how about

auto_load_bundle

which signifies that no additional work is required to ensure the right bundles load on a given Rails view.

This works by automatically calling append_javascript_pack_tag and append_stylesheet_pack_tag whenever react_component and react_component_hash are used.

# FILE SYSTEM BASED COMPONENT REGISTRY
################################################################################
# components_directory is the directory that is used to automatically detect and register components for the usage on
# the rails view. By Default any component inside `ror_components` directory is registered for the usage on rails view
Copy link
Copy Markdown
Member

@justin808 justin808 Jul 12, 2022

Choose a reason for hiding this comment

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

default is nil

if defined:

  1. assets precompile will automatically run the rake task
  2. for development: react_component view for an auto_load_bundle == true will confirm that the generated file inside the components_directory, generated/{component_name} exists in the packs (entries) directory exists. If not, run the rake task to build, and log a message to the console that the generated files were rebuilt. If directory of generated is empty, build all of the generated. Or else just build the single generated file.

For non-development, raise an error suggesting that the rake task needs to run. This will mainly apply to a test run.

Comment thread lib/tasks/generate_packs.rake Outdated

namespace :react_on_rails do
desc <<~DESC
If there is a jsx file inside config.components_directory, this command generates corresponding packs and#{' '}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

#{' '} is probably accidental.

Comment thread lib/tasks/generate_packs.rake Outdated
namespace :react_on_rails do
desc <<~DESC
If there is a jsx file inside config.components_directory, this command generates corresponding packs and#{' '}
registers the component for usage with react_component_with_bundle helper.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually, is this helper added? Because I can't see it anywhere in changes. If it isn't added because it was decided to take another approach, please change this and also update the PR description.

@alexeyr-ci1
Copy link
Copy Markdown
Contributor

alexeyr-ci1 commented Jul 14, 2022

which signifies that no additional work is required to ensure the right bundles load on a given Rails view.

But then do we ever want it to be false? Or at least do we want it to be false by default?

Unless it's just for backward compatibility, in which case we can document that the default will be true in the next major version.

@pulkitkkr pulkitkkr force-pushed the pulkitkkr/automate-bundle-creation branch 2 times, most recently from b6ebf37 to d7f4acf Compare July 18, 2022 21:47
@alexeyr-ci1 alexeyr-ci1 force-pushed the pulkitkkr/automate-bundle-creation branch from 1175779 to 752bafd Compare July 20, 2022 19:13
Comment thread lib/react_on_rails/packs_generator.rb
@alexeyr-ci1 alexeyr-ci1 force-pushed the pulkitkkr/automate-bundle-creation branch from 0287fa4 to 9feb90c Compare July 20, 2022 20:52
@pulkitkkr pulkitkkr force-pushed the pulkitkkr/automate-bundle-creation branch from 9272b19 to 59d5d8f Compare July 20, 2022 23:39
Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Comment thread docs/guides/file-system-based-automated-bundle-generation.md
@pulkitkkr
Copy link
Copy Markdown
Contributor Author

Address Docs related concerns @alexeyr

Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Comment thread docs/guides/file-system-based-automated-bundle-generation.md Outdated
Copy link
Copy Markdown
Member

@justin808 justin808 left a comment

Choose a reason for hiding this comment

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

LGTM

Reviewed 4 of 6 files at r27, 3 of 3 files at r28, all commit messages.
Reviewable status: all files reviewed, 36 unresolved discussions (waiting on @alexeyr and @alexeyr-ci1)

@justin808 justin808 merged commit 9f4802d into master Aug 21, 2022
@justin808 justin808 deleted the pulkitkkr/automate-bundle-creation branch August 21, 2022 09:49
@dnesteryuk
Copy link
Copy Markdown

Packs get generated after calling ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config) in rails_helper.rb even though config.auto_load_bundle = false

ReactOnRails::PacksGenerator.instance.generate_packs_if_stale

In our case it breaks feature tests (an issue with sass variables, but there is no such an issue while running bin/webpacker), we don't need a pack for every component. I fixed our tests by adding:

module ReactOnRails
  class PacksGenerator
    def generate_packs_if_stale
      # does nothing
    end
  end
end

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.

5 participants