Skip to content

Consolidate configuration into one file and setup caching for manifest #292

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

Closed
wants to merge 23 commits into from
Closed

Consolidate configuration into one file and setup caching for manifest #292

wants to merge 23 commits into from

Conversation

gauravtiwari
Copy link
Member

@gauravtiwari gauravtiwari commented Apr 26, 2017

Reload Webpacker manifest when cache_manifest is set

# config/webpack/configuration.yml
# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
  cache_manifest: false

def load(path = file_path)
self.instance = new(path)
end

def reloadable?
["development", "test"].include?(Rails.env)
Copy link
Member

Choose a reason for hiding this comment

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

I think is more common use !Rails.env.production? in Rails code base but we can check

Copy link
Member Author

Choose a reason for hiding this comment

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

Yepp thought about that too but I only wanted to limit this feature explicitly for - test and development. For ex - folks also might have staging and other environments where they don't want to reload. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I think environment is the wrong thing to check here exactly because of that. People may want to enable that in staging, or in an other environment. I'd introduce a new configuration like reload_webpacker with default to false and have this config set to true in the development and test environment files. Just like sprockets does with debug option for example.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense. Will fix 👍

@gauravtiwari
Copy link
Member Author

@guilleiguaran @rafaelfranca Please take a look 👍

@@ -12,6 +13,10 @@ class Webpacker::Engine < ::Rails::Engine
include Webpacker::Helper
end

ActiveSupport.on_load :before_eager_load do
app.config.x.webpacker[:reload] = false
Copy link
Member

Choose a reason for hiding this comment

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

instead of using the x namespace what about creating a webpacker namespace? Also you don't need to use ActiveSupport.on_load :before_eager_load do. The railtie can set the value in its body.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yepp lets do that, now is the good time apparently 😄 Gotcha 👍

def load(path = file_path)
self.instance = new(path)
end

def reloadable?
Rails.configuration.x.webpacker[:reload]
Copy link
Member

Choose a reason for hiding this comment

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

Instead of coupling webpacker with the Rails, we should be pushing the configuration to Webpacker::FileLoader from inside the Railtie.

initializer :webpacker do |app|
  Webpacker::FileLoade.reloadable = app.config.webpacker.reload

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense 👍 Will fix :)

@@ -7,9 +7,17 @@ class FileLoaderError < StandardError; end
attr_accessor :data

class << self
def exist?
instance.data.present? rescue false
Copy link
Member

Choose a reason for hiding this comment

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

The instance or the data not being there isn't really exceptional or unexpected, so let's use try or & to invocate rather than exception handling.

attr_accessor :data

class << self
reloadable = false
Copy link
Member

Choose a reason for hiding this comment

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

Should not this be self.reloadable = false? Otherwise it will only define a local variable.

lib/webpacker.rb Outdated
@@ -1,4 +1,7 @@
module Webpacker
mattr_accessor :reload_webpacker
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this one? I doesn't seems used.

Copy link
Member Author

Choose a reason for hiding this comment

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

Still work in progress, I guess pushed it by mistake 😄

@dhh
Copy link
Member

dhh commented May 1, 2017

I'd go with config.webpacker.caching to match config.action_controller.perform_caching. cache alone sounds like where the actual caching instance would live. But otherwise good.

@gauravtiwari
Copy link
Member Author

Yepp makes sense. Will fix 👍

@gauravtiwari
Copy link
Member Author

Should be good to go now. Updated the PR description to reflect the changes done here 😄

CHANGELOG.md Outdated
Webpacker::Configuration.exist? # => true
```

- Add `reloadable?` helper method to check if configuration or manifest is reloaded.
Copy link
Member

Choose a reason for hiding this comment

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

What is this used for?

Copy link
Member Author

@gauravtiwari gauravtiwari May 1, 2017

Choose a reason for hiding this comment

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

Nowhere at the moment, just added it as a reverse alias to Webpacker.caching in case someone wants to use this instead. Probably more readable?

@dhh
Copy link
Member

dhh commented May 1, 2017 via email

gauravtiwari referenced this pull request May 3, 2017
Remove redundant assign

Fix comment

Fix sentence
@gauravtiwari
Copy link
Member Author

@dhh This one is ready too 👍

@javan
Copy link
Contributor

javan commented May 10, 2017

I'd go with config.webpacker.caching to match config.action_controller.perform_caching. cache alone sounds like where the actual caching instance would live. But otherwise good.

This change is more similar to config.cache_classes than config.action_controller.perform_caching. Maybe config.webpacker.cache_configuration instead?

@gauravtiwari
Copy link
Member Author

gauravtiwari commented May 10, 2017

@javan We are caching both configuration and manifest here so, not sure if the name should include particular class (instead be more generic)

@javan
Copy link
Contributor

javan commented May 10, 2017

I would expect config.webpacker.caching to cache compiled assets or something. I wonder if we should just use config.cache_classes from Rails to determine this.

@dhh
Copy link
Member

dhh commented May 10, 2017 via email

@javan
Copy link
Contributor

javan commented May 10, 2017

I'd prefer something more descriptive then. Maybe config.webpacker.cache_file_loaders

@javan
Copy link
Contributor

javan commented May 10, 2017

And, generally, it's confusing that we'll have one environment-specific config option in config/environments/*.rb and the rest in config/webpacker/*.

@javan
Copy link
Contributor

javan commented May 10, 2017

WDYT about combining config/webpack/paths.yml + config/webpack/development.server.yml into a single file (maybe config/webpack/settings.yml) that also includes this new caching config?

@gauravtiwari
Copy link
Member Author

@javan I started doing that in #264. I guess having one setting file makes sense and gives a lot more flexibility towards what we want to configure without worrying about where it should go. We can namespace settings as needed.

@gauravtiwari
Copy link
Member Author

@javan @dhh Consolidated the configuration into one file ^_^ Please take a look when you get a chance 👍

@gauravtiwari gauravtiwari changed the title Reload manifest and configuration in both test and development Consolidate configuration into one file and setup caching for manifest May 16, 2017
your code is most likely to change on each run, manifest is reloaded
to ensure that latest change and packs has been picked up.
If you want to toggle this behavior in certain
environments you can do it in your `config/webpack/configuration.yml`
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think "cache" is the best word to describe this behavior. Perhaps invert it with some form or "reload"? Also, should we apply this behavior more broadly to all file loaders and name it as such? Seems like we'd want configuration.yml to reload in dev mode too.

@javan
Copy link
Contributor

javan commented May 18, 2017

Since we're down to a single .yml file now, what do you think about moving it to config/webpack.yml or config/webpacker.yml? That way config/webpack/ will just be .js files, which feels nice.

@dhh
Copy link
Member

dhh commented May 18, 2017 via email

@gauravtiwari gauravtiwari deleted the reload-manifest branch May 20, 2017 11:57
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