Module: Sprockets::Npm

Included in:
Base
Defined in:
lib/sprockets/npm.rb

Instance Method Summary collapse

Instance Method Details

#read_package_directives(dirname, filename) {|File.expand_path(package['style'], dirname)| ... } ⇒ Object

Internal: Read package.json’s main and style directives.

dirname - String path to component directory. filename - String path to package.json.

Returns nothing.

Yields:

  • (File.expand_path(package['style'], dirname))


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sprockets/npm.rb', line 39

def read_package_directives(dirname, filename)
  package = JSON.parse(File.read(filename), create_additions: false)

  case package['main']
  when String
    yield File.expand_path(package['main'], dirname)
  when nil
    yield File.expand_path('index.js', dirname)
  end

  yield File.expand_path(package['style'], dirname) if package['style']
end

#resolve_alternates(load_path, logical_path) ⇒ Object

Internal: Override resolve_alternates to install package.json behavior.

load_path - String environment path logical_path - String path relative to base

Returns candidate filenames.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sprockets/npm.rb', line 12

def resolve_alternates(load_path, logical_path)
  candidates, deps = super

  dirname = File.join(load_path, logical_path)

  if directory?(dirname)
    filename = File.join(dirname, 'package.json')

    if self.file?(filename)
      deps << build_file_digest_uri(filename)
      read_package_directives(dirname, filename) do |path|
        if file?(path)
          candidates << path
        end
      end
    end
  end

  return candidates, deps
end