Module: Jekyll::Esm

Defined in:
lib/jekyll-esm.rb,
lib/jekyll-esm/version.rb,
lib/jekyll-esm/managers/npm.rb,
lib/jekyll-esm/managers/yarn.rb,
lib/jekyll-esm/managers/bower.rb

Defined Under Namespace

Modules: Managers Classes: Error

Constant Summary collapse

VERSION =
"0.3.1"
@@existing_esm_packages =
[]
@@new_esm_packages =
[]
@@esm_ids =
[]

Class Method Summary collapse

Class Method Details

.apply(site) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jekyll-esm.rb', line 73

def self.apply(site)
  manager = select_manager(site.config.dig('esm', 'manager'))
  location = manager.location(site.config)
  dist = manager.dist(site.config)

  if @@existing_esm_packages.any?
    for_removal = @@existing_esm_packages - @@new_esm_packages.uniq

    # Remove any packages that are no longer referenced in an esm declaration
    if for_removal.any?
      packages = for_removal.join(' ')
      manager.remove(packages: packages, site: site)
    end
  end

  return unless Dir.exists?(File.join(site.source, location))

  FileUtils.rm_rf(File.join(site.dest, dist))
  FileUtils.cp_r(File.join(site.source, location), File.join(site.dest, dist))
  @@existing_esm_packages = @@new_esm_packages
  @@new_esm_packages = []
  @@esm_ids = []
end

.process(page) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jekyll-esm.rb', line 21

def self.process(page)
  site = page.site
  manager = select_manager(site.config.dig('esm', 'manager'))
  location = manager.location(site.config)

  doc = Nokogiri::HTML(page.output)

  import_maps = doc.search('script[type=importmap]')

  import_maps.each do |value|
    esm_id = value.attributes["data-esm-id"]&.value
    # declare a data-esm-id so that jekyll will only process an esm declaration once
    next if @@esm_ids.include?(esm_id)
    @@esm_ids << esm_id if esm_id

    importmap = JSON.parse(value.children[0].content)
    imports = importmap["imports"]
    imports.keys.each do |import_key|
      # ignore urls
      next if import_key =~ /https?:\/\/[\S]+/
      next if imports[import_key] =~ /https?:\/\/[\S]+/
      # ignore relative paths
      next if import_key =~ /(^\.+\/)+/
      # ignore absolute paths
      next if import_key =~ /^\/[\S]+/

      # ignore namespaces but only if it is not scoped
      if import_key =~ /^@[\S]+/
        import = import_key.split('/')[0..1].join('/')
      else
        import = import_key.split('/').first
      end

      pkg_path = File.join(site.source, location, import)

      # don't repeatedly attempt to install a package
      next if Dir.exists?(pkg_path) && @@new_esm_packages.include?(import)

      @@new_esm_packages << import

      manager.add(package: import, site: site)

    end
  end
end

.select_manager(name) ⇒ Object



67
68
69
70
71
# File 'lib/jekyll-esm.rb', line 67

def self.select_manager(name)
  return Managers::Npm if name == 'npm'
  return Managers::Bower if name == 'bower'
  return Managers::Yarn
end