lib/webpack_rails/task.rb in webpack_rails-0.3.0 vs lib/webpack_rails/task.rb in webpack_rails-1.0.0

- old
+ new

@@ -26,14 +26,30 @@ def webpack_gem_dir @webpack_gem_dir ||= File.dirname(File.expand_path(__FILE__)) end - def webpack_task_script - @webpack_task_script ||= File.join(webpack_gem_dir, 'webpack-task.js') + def webpack_task_watch_script + @webpack_task_watch_script ||= File.join(webpack_gem_dir, 'webpack-task-watch.js') end + def webpack_task_dev_server_script + @webpack_task_dev_server_script ||= File.join(webpack_gem_dir, 'webpack-task-dev-server.js') + end + + def webpack_task_script(opts) + return webpack_task_watch_script if opts[:watch] + return webpack_task_dev_server_script if opts[:dev_server] + fail "can't determine which task to run" + end + + def webpack_task_opts(opts) + opts.merge( + webpack_config_file: opts[:webpack_config_file] ? opts[:webpack_config_file].to_s : nil, + ) + end + def app_node_path @app_node_path ||= File.join(root_dir, 'node_modules') end def with_app_node_path @@ -45,41 +61,58 @@ end ENV['NODE_PATH'] = prev_node_path return_value end - def build_result - begin - JSON.parse(File.open(File.join(working_dir, 'webpack-build-result.json')).read) - rescue Errno::ENOENT => e - {modules: []} + # TODO: move to NodeTask + def alive? + current_pid = nil + alive = false + if @controller + begin + current_pid = @controller.pid + rescue Errno::ENOENT + end end + if current_pid + begin + Process.getpgid(current_pid) + alive = true + rescue Errno::ESRCH + end + end + alive end - def run_webpack(opts = nil) - result = nil - if ENV['DISABLE_WEBPACK'] - return build_result + def build_once(webpack_task_config) + WebpackRails::Task.with_app_node_path do + webpack_cmd_script = `#{WebpackRails::Task.node_command} -e "process.stdout.write(require.resolve('webpack/bin/webpack.js'))"` + system "#{WebpackRails::Task.node_command} #{webpack_cmd_script} --config #{webpack_task_config[:webpack_config_file]}" end + end + def run_webpack(opts = {}) + return if ENV['DISABLE_WEBPACK'] + + result = nil task_duration = Benchmark.realtime do - result = with_app_node_path do + with_app_node_path do begin - task = self.new - task.run(opts) - build_result + task = self.new(webpack_task_script(opts)) + result = task.run(webpack_task_opts(opts)) rescue NodeTask::Error => e raise self::Error.new(e) end end end task_duration_ms = task_duration * 1000 - if defined?(Rails) && task_duration_ms > 10 - Rails.logger.info("Webpack: #{task_duration_ms.round(0)}ms") + if defined?(Rails) && result + if opts[:dev_server] && result[:started] + Rails.logger.info("Started webpack-dev-server in #{task_duration_ms.round(0)}ms") + end end - result end private @@ -87,12 +120,8 @@ # one node_task daemon will be created per unique working dir wd = File.join(root_dir, 'tmp', 'webpack', 'task') FileUtils.mkpath(wd) wd end - end - - def initialize - super(self.class.webpack_task_script) end end end