Skip to content

Instantly share code, notes, and snippets.

@jarednorman
Last active February 19, 2017 20:48

Revisions

  1. jarednorman revised this gist Feb 22, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion webpack-rails-2.markdown
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    __Note:__ _This article is more generally about how to add dependencies to a
    Rake task, and why you might want to do that._

    If you followed [my previous article](/2015/webpack-with-rails/) on webpack and
    If you followed [my previous article](https://gist.github.com/jarednorman/11a1cbb11b389fb0501e) on webpack and
    Rails, you might have built yourself a trendy little
    [React](https://facebook.github.io/react/) app. Then you tried to deploy it, and
    that didn't work, did it?
  2. jarednorman revised this gist Feb 22, 2016. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions webpack-rails-2.markdown
    Original file line number Diff line number Diff line change
    @@ -40,6 +40,4 @@ task :webpack do
    end
    ```

    Ship it.

    Comments or questions? [Tweet your feelings at me.](https://twitter.com/_jaredn)
    Ship it.
  3. jarednorman created this gist Feb 22, 2016.
    45 changes: 45 additions & 0 deletions webpack-rails-2.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # Webpack with Rails Part 2: Precompile Harder

    __Note:__ _This article is more generally about how to add dependencies to a
    Rake task, and why you might want to do that._

    If you followed [my previous article](/2015/webpack-with-rails/) on webpack and
    Rails, you might have built yourself a trendy little
    [React](https://facebook.github.io/react/) app. Then you tried to deploy it, and
    that didn't work, did it?

    ## Wait, what are you on about?

    Whether you're doing artifact-based deploys, git deploys, or something else
    altogether, at some point something is going to run `rake assets:precompile`.
    This will work but miss your webpack generated assets, because unless you
    checked your compiled entries in (please, tell me you didn't do that) they
    won't exist when the rake task is run because webpack never did its thing.

    The best solution, rather than to modify your deploy system, is to add a
    dependency to the `assets:precompile` rake task that runs webpack. This is the
    correct thing to do (though it might look like a hack) because the
    `assets:precompile` doesn't actually do it's job properly if you haven't run
    webpack first. It really does depend on webpack.

    ## Adding a Dependency to Rake Task

    You can add a dependency to a rake task just by redefining it without a block,
    and passing dependencies just like you normally would.

    ```ruby
    # lib/tasks/assets.rake

    namespace :assets do
    task :precompile => :webpack
    end

    task :webpack do
    sh "npm install"
    sh "./node_modules/.bin/webpack"
    end
    ```

    Ship it.

    Comments or questions? [Tweet your feelings at me.](https://twitter.com/_jaredn)