Class: VueRails::WebpackerAssetFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/vue_rails/asset_finder.rb

Constant Summary collapse

CLIENT_REQUIRE =

This pattern matches the code that initializes the dev-server client.

%r{__webpack_require__\(.*webpack-dev-server\/client\/index\.js.*\n}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compatible?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/vue_rails/asset_finder.rb', line 14

def self.compatible?
  !!defined?(Webpacker)
end

Instance Method Details

#configObject



71
72
73
# File 'lib/vue_rails/asset_finder.rb', line 71

def config
  Webpacker::Configuration
end

#file_path(path) ⇒ Object

1.0 and 1.1 support



89
90
91
# File 'lib/vue_rails/asset_finder.rb', line 89

def file_path path
  manifest.lookup_path(path)
end

#find_asset(logical_path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vue_rails/asset_finder.rb', line 24

def find_asset(logical_path)
  # raises if not found
  asset_path = manifest.lookup(logical_path).to_s
  if asset_path.start_with?('http')
    # Get a file from the webpack-dev-server
    dev_server_asset = open(asset_path).read
    # Remove `webpack-dev-server/client/index.js` code which causes ExecJS to 💥
    dev_server_asset.sub!(CLIENT_REQUIRE, '//\0')
    dev_server_asset
  else
    # Read the already-compiled pack:
    full_path = file_path(logical_path).to_s
    File.read(full_path)
  end
end

#find_path(logical_path) ⇒ Object



19
20
21
22
# File 'lib/vue_rails/asset_finder.rb', line 19

def find_path(logical_path)
  asset_path = manifest.lookup(logical_path).to_s
  asset_path.start_with?('http') ? asset_path : file_path(logical_path).to_s
end

#manifestObject



61
62
63
# File 'lib/vue_rails/asset_finder.rb', line 61

def manifest
  Webpacker::Manifest
end

#output_pathObject



95
96
97
98
# File 'lib/vue_rails/asset_finder.rb', line 95

def output_path
  # Webpack1 /:output/:entry, Webpack3 /public/:output
  config.respond_to?(:output_path) ? config.output_path : 'public'
end