Skip to content

Commit

Permalink
Run webpack within Rails
Browse files Browse the repository at this point in the history
* Configure webpack to save assets to tmp/webpack in dev mode
* Serve assets saved under tmp/webpack using ActionDispatch::Static
* Start webpack as child process
  • Loading branch information
guilleiguaran committed Nov 19, 2016
1 parent e78acc2 commit 62985cb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -34,3 +34,5 @@ end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'childprocess'
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -74,6 +74,8 @@ GEM
arel (7.1.4)
builder (3.2.2)
byebug (9.0.6)
childprocess (0.5.9)
ffi (~> 1.0, >= 1.0.11)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
erubis (2.7.0)
Expand Down Expand Up @@ -138,6 +140,7 @@ PLATFORMS

DEPENDENCIES
byebug
childprocess
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
pg
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/assets.rb
Expand Up @@ -26,3 +26,10 @@ def read_manifest
ActiveSupport.on_load(:action_view) do
include WebpackAssetUrlHelper
end

Rails.application.config.middleware.insert_after Rack::Sendfile, ActionDispatch::Static, File.join(Rails.root, "tmp", "webpack")

process = ChildProcess.build("webpack")

process.io.inherit!
process.start
6 changes: 5 additions & 1 deletion webpack.config.js
Expand Up @@ -21,6 +21,8 @@ const css_output_template = prod ? "[name]-[chunkhash].css" : "[name].css";
const js_output_template = prod ? "[name]-[chunkhash].js" : "[name].js";
const asset_output_template = prod ? "[name]-[hash].[ext]" : "[name].[ext]";

const outputFolder = prod ? "/public/assets" : "/tmp/webpack/assets";

const asset_folders = ["./app/assets", "./lib/assets", "./vendor/assets", "./node_modules"];

module.exports = {
Expand All @@ -30,11 +32,13 @@ module.exports = {
},

output: {
path: __dirname + "/public/assets",
path: __dirname + outputFolder,
publicPath: "/assets/",
filename: js_output_template
},

watch: true,

devServer: {
proxy: {
'/': {
Expand Down

0 comments on commit 62985cb

Please sign in to comment.