webpack-rails icon indicating copy to clipboard operation
webpack-rails copied to clipboard

Bring webpack tasks to npm scripts and wrap it with rakes

Open halan opened this issue 9 years ago • 9 comments

I usually do it in my projects. What you think about?

We can run npm tasks or rake tasks whenever you need:

npm start
npm run build

rake webpack:dev
rake webpack:compile

I often run rails s in one tab and npm start in another.

The main advantage is have all commands configured in just one place and can eventually be removed from rails (without ruby methods as dependency to run webpack server o build it).

halan avatar Sep 18 '16 21:09 halan

I need to think about this one a bit more. I was originally against this because if you changed away from the default paths in your config it'd break.. but there's lots of 👍 's and it's really annoying having to wait for rails to boot just to run a compile.

mipearson avatar Sep 21 '16 22:09 mipearson

Yeah, if you change the default path it will break. I usually put configs into config/js/ but I have only one and simple place to change, the package.json. Who use webpack hope it. Let npm do his job.

halan avatar Sep 21 '16 23:09 halan

Look. How can I improve more? @talyssonoc, @mipearson.

halan avatar Sep 21 '16 23:09 halan

I unfortunately can't merge this as it is as it will be a breaking change for those that have reconfigured paths in webpack-rails and expect the rake task to 'just work'.

That said, though, this is the better way.

I will be making a 1.x branch and we can merge it in to that.

mipearson avatar Sep 21 '16 23:09 mipearson

Nice. Anyway I will think about improve it in this way.

halan avatar Sep 22 '16 00:09 halan

What you think about this?:

namespace :webpack do
  desc "Compile webpack bundles"
  task compile: :environment do
    ENV["TARGET"] = 'production' # TODO: Deprecated, use NODE_ENV instead
    ENV["NODE_ENV"] = 'production'

    package_json = JSON.parse(File.read(Rails.root.join('package.json')))

    if( package_json['scripts']['compile'] )
      sh "npm run compile"
    else
      # WE can put a deprecation warning here

      webpack_bin = ::Rails.root.join(::Rails.configuration.webpack.binary)
      config_file = ::Rails.root.join(::Rails.configuration.webpack.config_file)

      unless File.exist?(webpack_bin)
        raise "Can't find our webpack executable at #{webpack_bin} - have you run `npm install`?"
      end

      unless File.exist?(config_file)
        raise "Can't find our webpack config file at #{config_file}"
      end

      sh "#{webpack_bin} --config #{config_file} --bail"
    end
  end
end

halan avatar Sep 22 '16 00:09 halan

This is really cool! Would love to see this as it doesn't require my rails environment (secrets.yml, redis.yml) to exist at the time of assets compilation.

bpinto avatar Oct 18 '16 16:10 bpinto

I'm going to be bringing this in to an API-breaking 1.0-pre branch shortly.

The delay on this (and other) pull requests is that I've been cautious to merge anything that could break functionality for existing users.

mipearson avatar Oct 26 '16 22:10 mipearson

Of course, I understand. Do you need some more adjustments?

halan avatar Oct 26 '16 22:10 halan