Class: WebpackRails::Task
- Inherits:
-
NodeTask
- Object
- NodeTask
- WebpackRails::Task
- Defined in:
- lib/webpack_rails/task.rb
Defined Under Namespace
Classes: Error
Class Method Summary collapse
-
.alive? ⇒ Boolean
TODO: move to NodeTask.
- .app_node_path ⇒ Object
- .build_once(webpack_task_config) ⇒ Object
- .root_dir ⇒ Object
- .run_webpack(opts = {}) ⇒ Object
- .webpack_gem_dir ⇒ Object
- .webpack_task_dev_server_script ⇒ Object
- .webpack_task_opts(opts) ⇒ Object
- .webpack_task_script(opts) ⇒ Object
- .webpack_task_watch_script ⇒ Object
- .with_app_node_path ⇒ Object
- .working_dir ⇒ Object
Class Method Details
.alive? ⇒ Boolean
TODO: move to NodeTask
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/webpack_rails/task.rb', line 67 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 |
.app_node_path ⇒ Object
51 52 53 |
# File 'lib/webpack_rails/task.rb', line 51 def app_node_path @app_node_path ||= File.join(root_dir, 'node_modules') end |
.build_once(webpack_task_config) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/webpack_rails/task.rb', line 86 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 |
.root_dir ⇒ Object
19 20 21 |
# File 'lib/webpack_rails/task.rb', line 19 def root_dir @root_dir ||= defined?(Rails) ? Rails.root.to_s : Dir.pwd end |
.run_webpack(opts = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/webpack_rails/task.rb', line 93 def run_webpack(opts = {}) return if ENV['DISABLE_WEBPACK'] result = nil task_duration = Benchmark.realtime do with_app_node_path do begin 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) && 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 |
.webpack_gem_dir ⇒ Object
27 28 29 |
# File 'lib/webpack_rails/task.rb', line 27 def webpack_gem_dir @webpack_gem_dir ||= File.dirname(File.(__FILE__)) end |
.webpack_task_dev_server_script ⇒ Object
35 36 37 |
# File 'lib/webpack_rails/task.rb', line 35 def webpack_task_dev_server_script @webpack_task_dev_server_script ||= File.join(webpack_gem_dir, 'webpack-task-dev-server.js') end |
.webpack_task_opts(opts) ⇒ Object
45 46 47 48 49 |
# File 'lib/webpack_rails/task.rb', line 45 def webpack_task_opts(opts) opts.merge( webpack_config_file: opts[:webpack_config_file] ? opts[:webpack_config_file].to_s : nil, ) end |
.webpack_task_script(opts) ⇒ Object
39 40 41 42 43 |
# File 'lib/webpack_rails/task.rb', line 39 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 |
.webpack_task_watch_script ⇒ Object
31 32 33 |
# File 'lib/webpack_rails/task.rb', line 31 def webpack_task_watch_script @webpack_task_watch_script ||= File.join(webpack_gem_dir, 'webpack-task-watch.js') end |
.with_app_node_path ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/webpack_rails/task.rb', line 55 def with_app_node_path prev_node_path = ENV['NODE_PATH'] ENV['NODE_PATH'] = app_node_path return_value = nil Dir.chdir(root_dir) do return_value = yield end ENV['NODE_PATH'] = prev_node_path return_value end |
.working_dir ⇒ Object
23 24 25 |
# File 'lib/webpack_rails/task.rb', line 23 def working_dir @working_dir ||= _make_working_dir end |