You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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_installyarn_installer=(Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 0) ? 'webpacker:yarn_install' : 'yarn:install'# install yarn and compile packs BEFORE assets:precompileRake::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?
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:
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.
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 trueprecompile: manual # don't hook into assets:precompile, alias is falseprecompile: before # hook into assets:precompile BEFORE assets:precompile
if the WEBPACKER_PRECOMPILE env variable is set it takes precedence over the yaml file.
Activity
jakeNiemiec commentedon Aug 19, 2019
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.
catmando commentedon Aug 20, 2019
@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:
This works fine, but its a bit cludgy.
The question is would a PR be accepted that reverses the order?
jakeNiemiec commentedon Aug 20, 2019
I intended to show you that we have a flag to disable the precompiling behavior:
You can find this reflected in the code:
webpacker/lib/tasks/webpacker/compile.rake
Lines 42 to 51 in c506521
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.
catmando commentedon Aug 21, 2019
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:
if the WEBPACKER_PRECOMPILE env variable is set it takes precedence over the yaml file.
catmando commentedon Apr 5, 2021
any progress on this?
justin808 commentedon Dec 23, 2021
@catmando you can modify the
assets:precompilerake 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.