module Jekyll::Esm
Constants
- VERSION
Public Class Methods
apply(site)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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