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
- .app_node_path ⇒ Object
- .build_result ⇒ Object
- .root_dir ⇒ Object
- .run_webpack(opts = nil) ⇒ Object
- .webpack_gem_dir ⇒ Object
- .webpack_task_script ⇒ Object
- .with_app_node_path ⇒ Object
- .working_dir ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ Task
constructor
A new instance of Task.
Constructor Details
#initialize ⇒ Task
Returns a new instance of Task.
94 95 96 |
# File 'lib/webpack_rails/task.rb', line 94 def initialize super(self.class.webpack_task_script) end |
Class Method Details
.app_node_path ⇒ Object
35 36 37 |
# File 'lib/webpack_rails/task.rb', line 35 def app_node_path @app_node_path ||= File.join(root_dir, 'node_modules') end |
.build_result ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/webpack_rails/task.rb', line 50 def build_result begin JSON.parse(File.open(File.join(working_dir, 'webpack-build-result.json')).read) rescue Errno::ENOENT => e {modules: []} 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 = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/webpack_rails/task.rb', line 58 def run_webpack(opts = nil) result = nil if ENV['DISABLE_WEBPACK'] return build_result end task_duration = Benchmark.realtime do result = with_app_node_path do begin task = self.new task.run(opts) build_result 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") 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_script ⇒ Object
31 32 33 |
# File 'lib/webpack_rails/task.rb', line 31 def webpack_task_script @webpack_task_script ||= File.join(webpack_gem_dir, 'webpack-task.js') end |
.with_app_node_path ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/webpack_rails/task.rb', line 39 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 |