-
Notifications
You must be signed in to change notification settings - Fork 81
Bring webpack tasks to npm scripts and wrap it with rakes #57
base: master
Are you sure you want to change the base?
Conversation
lib/tasks/webpack.rake
Outdated
ENV["NODE_ENV"] = 'production' | ||
webpack_bin = ::Rails.root.join(::Rails.configuration.webpack.binary) | ||
config_file = ::Rails.root.join(::Rails.configuration.webpack.config_file) | ||
task(:compile) { sh "npm run build" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We gotta make sure NODE_ENV
will be 'production'
when this script is run.
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. |
example/package.json
Outdated
@@ -2,6 +2,10 @@ | |||
"name": "webpack-rails-example", | |||
"version": "0.0.1", | |||
"license": "MIT", | |||
"scripts": { | |||
"build": "webpack -d --config config/webpack.config.js", | |||
"start": "webpack-dev-server -p --config config/webpack.config.js" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to rename start
to serve
. People's apps might have other things they want to do with npm start
that aren't webpack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.
Yeah, if you change the default path it will break. I usually put configs into |
Look. How can I improve more? @talyssonoc, @mipearson. |
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. |
Nice. Anyway I will think about improve it in this way. |
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 |
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. |
@@ -2,6 +2,10 @@ | |||
"name": "webpack-rails-example", | |||
"version": "0.0.1", | |||
"license": "MIT", | |||
"scripts": { | |||
"compile": "webpack -d --config config/webpack.config.js", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you have used d
here but you wanted p
and the same on the line below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'r right. I'll fix it. Thanks.
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. |
Of course, I understand. Do you need some more adjustments? |
@@ -2,6 +2,10 @@ | |||
"name": "webpack-rails-example", | |||
"version": "0.0.1", | |||
"license": "MIT", | |||
"scripts": { | |||
"compile": "webpack -d --config config/webpack.config.js", | |||
"serve": "webpack-dev-server -p --config config/webpack.config.js" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using -p
option, I had the following issue: webpack/webpack#1385 which was fixed by removing the -p
option.
when using new UglifyJsPlugin and --opimize-minimize (or -p) you are adding the UglifyJsPlugin twice. Omit the CLI option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. I will change.
I usually do it in my projects. What you think about?
We can run
npm
tasks orrake
tasks whenever you need:I often run
rails s
in one tab andnpm 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).