Skip to content

run webpack BEFORE precompilation? #2237

Closed

Description

@catmando

Is there any reason to run webpack after precompilation?

Its obscure but the reason why we want to run webpack first is as follows:

In order to integrate server side prerendering with react you need to supply different versions of some of the react libraries, but then you also want to precompile the assets but with one of the two different pack files included.

But this does not work unless webpack compile is run first.

I have monkey patched this, and it works (at least for some limited cases)

Was wondering if I did a PR to swap them around, would it be accepted or is there some technical reason why the order is this way?

Activity

  1. jakeNiemiec commented on Aug 19, 2019

    @jakeNiemiec
    Member

    See following docs, this can be configured:

    I saw you need multiple versions of react. The only way I know of to do this is with engines.

  2. catmando commented on Aug 20, 2019

    @catmando
    Author

    @jakeNiemiec thanks for the response.

    Unless I am missing something, "custom rails environment" etc, just lets you have different builds depending on whether you are on staging/production/test etc.

    The problem here is a bit different. React prerendering requires two DIFFERENT sets of assets depending on whether the code will run on the server (prerendering) or on the client.

    The problem is easily solved by having two different packs: One for the client (i.e. client_only.js) and one for the common elements that run on client and server (client_and_server.js)

    This is all fine but the problem is that when the assets are precompiled, these packs must already exist.

    However webpacker first runs precompile, THEN runs webpack. The order simply needs to be swapped.

    For now the work around is to add this to the application Rake file:

    puts "Warning: Hyperstack is running yarn install and webpacker:compile BEFORE assets:precompile"
    # determine if we should use built in yarn:install or use webpacker:yarn_install
    yarn_installer = (Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 0) ? 'webpacker:yarn_install' : 'yarn:install'
    # install yarn and compile packs BEFORE assets:precompile
    Rake::Task["assets:precompile"].enhance([yarn_installer, 'webpacker:compile'])

    This works fine, but its a bit cludgy.

    The question is would a PR be accepted that reverses the order?

  3. jakeNiemiec commented on Aug 20, 2019

    @jakeNiemiec
    Member

    Unless I am missing something, "custom rails environment" etc, just lets you have different builds depending on whether you are on staging/production/test etc.... However webpacker first runs precompile, THEN runs webpack.

    I intended to show you that we have a flag to disable the precompiling behavior:

    image

    You can find this reflected in the code:

    # Compile packs after we've compiled all other assets during precompilation
    skip_webpacker_precompile = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
    unless skip_webpacker_precompile
    if Rake::Task.task_defined?("assets:precompile")
    enhance_assets_precompile
    else
    Rake::Task.define_task("assets:precompile" => ["webpacker:yarn_install", "webpacker:compile"])
    end
    end

    Any PRs that enhance or better define this functionality are very welcome. You would just need to make sure not to break any functionality for the different libraries that webpacker supports and to update any documentation that references the old flow.

  4. catmando commented on Aug 21, 2019

    @catmando
    Author

    okay so I propose to do it like this:

    change the meaning of WEBPACKER_PRECOMPILE so that "no/false/n/f" works the same as today, but WEBPACKER_PRECOMPILE=after means run precompilation AFTER running webpacker. Otherwise behavior is like it is today.

    I also wonder if while I am at it, if it would be appropriate to add an option to the .yml file, that would work like the environment variable. That way you can keep all the config in one place:

    ...
      
      # control when pre-compilation of packs occurs:
      precompile: after # after is default (i.e. current behavior), alias is true
      precompile: manual # don't hook into assets:precompile, alias is false
      precompile: before # hook into assets:precompile BEFORE assets:precompile

    if the WEBPACKER_PRECOMPILE env variable is set it takes precedence over the yaml file.

  5. catmando commented on Apr 5, 2021

    @catmando
    Author

    any progress on this?

  6. justin808 commented on Dec 23, 2021

    @justin808
    Contributor

    @catmando you can modify the assets:precompile rake task on your own, so this might be more of a doc issue.

    If you want to add a PR for your proposed enhancement, I'll take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions