lib/jekyll/esm.rb in jekyll-esm-0.1.1 vs lib/jekyll/esm.rb in jekyll-esm-0.2.0
- old
+ new
@@ -21,15 +21,18 @@
import_maps.each do |value|
importmap = JSON.parse(value.children[0].content)
imports = importmap["imports"]
imports.keys.each do |import_key|
+ next if import_key =~ /https?:\/\/[\S]+/
+ next if import_key =~ /(^\.+\/)+/
+
import = import_key.split('/').first
pkg_path = File.join(page.site.source, 'node_modules', import)
# don't repeatedly attempt to install a package
- return if Dir.exists?(pkg_path) && @@new_esm_packages.include?(import)
+ next if Dir.exists?(pkg_path) && @@new_esm_packages.include?(import)
@@new_esm_packages << import
stdout, stderr, status = Open3.capture3(
"yarn add #{import}",
@@ -45,28 +48,31 @@
end
end
end
def self.apply(site)
- existing_packages = Dir.children(File.join(site.source, 'node_modules')).reject { |dir| dir =~ /^\..*$/ }
- for_removal = existing_packages - @@new_esm_packages.uniq
+ 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?
- stdout, stderr, status = Open3.capture3(
- "yarn remove #{for_removal.join(' ')}",
- chdir: File.expand_path(site.source)
- )
+ # Remove any packages that are no longer referenced in an esm declaration
+ if for_removal.any?
+ stdout, stderr, status = Open3.capture3(
+ "yarn remove #{for_removal.join(' ')}",
+ chdir: File.expand_path(site.source)
+ )
- if site.config.dig('esm', 'strict')
- runtime_error = stdout =~ /ERROR in|SyntaxError/
+ if site.config.dig('esm', 'strict')
+ runtime_error = stdout =~ /ERROR in|SyntaxError/
- raise Error, stderr if stderr.size > 0
- raise Error, stdout if !runtime_error.nil?
+ raise Error, stderr if stderr.size > 0
+ raise Error, stdout if !runtime_error.nil?
+ end
end
end
+ FileUtils.rm_rf(File.join(site.dest, 'node_modules'))
FileUtils.cp_r(File.join(site.source, 'node_modules'), File.join(site.dest, 'node_modules'))
+ @@existing_esm_packages = @@new_esm_packages
@@new_esm_packages = []
end
end
end