jekyll-importmap 0.0.7 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 12cc91588d8814896a8d0795e8652d0cb53d5d65c5da68061eb8c0dfef970cca
4
- data.tar.gz: 11d6dd93f09f7524b2068431ea99304c960998c6eaa86abd76778e972237bbc8
3
+ metadata.gz: fa320c90e7d34c61fe7157d25b4464ce980a873076985d57fd382ddba33be652
4
+ data.tar.gz: 3bcff1f36209602db237935d31515213c53f5441aec8e3d95d346a1aeea3bab1
5
5
  SHA512:
6
- metadata.gz: ae3d1dc1f3f04a9a8834f6b27cbecc84d05d85414af4f864c1352a5500feb342736c1cf60992031c2076901e2dc9fb67f2d8ea272f0b428fdb4431bbf13e5c99
7
- data.tar.gz: d95160c39d963e14eaa30e43440f1cfc8afe3b1f4ce60226b4a50237a9b6a2f060c54bd6a9c5557ce76d2b2ceffad1a3d92709e7062f615ab650bb5724e6d24e
6
+ metadata.gz: c1e6329ab9473f26e0b7ef4d870837ef5d82e66a612c000a60db7980de1a2a173dd7efaa96e95dc089c42ccd2233cae137ee6eec13722ff98a471575463e3027
7
+ data.tar.gz: 18d080b8a381adfc3709a72066f68db45ed64ad3d086cf43e97e23276cffe5e46e5efa07286fb1945530e28595473ad9b56496c11062b9837fe871d279aa69b5
@@ -0,0 +1,71 @@
1
+ require 'jekyll'
2
+ require 'jekyll-importmap'
3
+
4
+ module Jekyll::Importmap
5
+ class Map
6
+ attr_reader :packages, :directories
7
+
8
+ class InvalidFile < StandardError; end
9
+
10
+ def initialize
11
+ @packages, @directories = {}, {}
12
+ @resolver = Jekyll::Importmap::Resolver
13
+ end
14
+
15
+ def draw(path = nil, &block)
16
+ if path && File.exist?(path)
17
+ begin
18
+ instance_eval(File.read(path), path.to_s)
19
+ rescue StandardError => e
20
+ raise InvalidFile, "Unable to parse importmap file from #{path}: #{e.message}"
21
+ end
22
+ elsif block_given?
23
+ instance_eval(&block)
24
+ else
25
+ raise InvalidFile, "You must provide a file path or a block to draw the importmap, given: #{path}"
26
+ end
27
+
28
+ self
29
+ end
30
+
31
+ def pin(name, to: nil, preload: true)
32
+ @packages[name] = Jekyll::Importmap::MappedFile.new(name: name, path: to || "#{name}.js", preload: preload)
33
+ end
34
+ def pin_all_from(dir, under: nil, to: nil, preload: true)
35
+ @directories[dir] = Jekyll::Importmap::MappedDir.new(dir: dir, under: under, path: to, preload: preload)
36
+ end
37
+
38
+ def preloaded_module_paths
39
+ resolve_asset_paths(
40
+ expanded_preloading_packages_and_directories,
41
+ resolver: @resolver
42
+ ).values
43
+ end
44
+
45
+ def to_json
46
+ JSON.pretty_generate({
47
+ "imports" => resolved_asset_paths
48
+ })
49
+ end
50
+
51
+ private
52
+ def resolved_asset_paths
53
+ expanded_packages_and_directories.transform_values do |mapped_file|
54
+ begin
55
+ mapped_file.resolved_path
56
+ rescue => e
57
+ raise e
58
+ end
59
+ end.compact
60
+ end
61
+ def expanded_packages_and_directories
62
+ @packages.dup.tap do |packages|
63
+ @directories.values.each do |mapped_directory|
64
+ mapped_directory.expanded_paths.each do |mapped_file|
65
+ packages[mapped_file.name] = mapped_file
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,33 @@
1
+ require 'jekyll'
2
+ require 'jekyll-importmap'
3
+
4
+ module Jekyll::Importmap
5
+ class Mappable
6
+ attr_reader :path, :preload, :keyword_init
7
+
8
+ def initialize(hash, resolver=Jekyll::Importmap::Resolver)
9
+ @path = hash[:path]
10
+ @preload = hash[:preload]
11
+ @keyword_init = hash[:keyword_init] ||= true
12
+ @resolver = resolver
13
+ end
14
+
15
+ def resolved_path
16
+ @resolver.path_to_asset(@path)
17
+ end
18
+ def absolute_root_path
19
+ @pathname ||= calculate_absolute_root_path
20
+ end
21
+ def calculate_absolute_root_path
22
+ pathname = Pathname.new(@dir || @path)
23
+ if pathname.absolute?
24
+ pathname
25
+ else
26
+ # this is where you can adjust whether we allow adding js
27
+ # files anywhere or just in the /assets/js directory
28
+
29
+ Pathname.new(Dir.pwd).join(pathname)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,51 @@
1
+ require 'jekyll'
2
+ require 'jekyll-importmap'
3
+ require 'jekyll-importmap/mappable'
4
+
5
+ module Jekyll::Importmap
6
+
7
+ # MappedFile = Struct.new(:name, :path, :preload, keyword_init: true)
8
+ # MappedDir = Struct.new(:dir, :under, :path, :preload, keyword_init: true)
9
+
10
+ class MappedDir < Jekyll::Importmap::Mappable
11
+
12
+ attr_reader :dir, :under
13
+
14
+ def initialize(hash, resolver=Jekyll::Importmap::Resolver)
15
+ super
16
+ @dir = hash[:dir]
17
+ @under = hash[:under]
18
+ end
19
+
20
+ def javascript_files_in_tree
21
+ selector = absolute_root_path.join("**/*.js{,m}")
22
+ files = Dir[selector].sort
23
+ files.collect do |file|
24
+ Pathname.new(file)
25
+ end.select(&:file?)
26
+ end
27
+
28
+ def expanded_paths
29
+ javascript_files_in_tree.collect do |pathname|
30
+ module_filename = pathname.relative_path_from(absolute_root_path)
31
+ module_path = module_path_from(module_filename)
32
+ module_name = module_name_from(module_filename)
33
+
34
+ Jekyll::Importmap::MappedFile.new(
35
+ name: module_name,
36
+ path: module_path,
37
+ preload: @preload
38
+ )
39
+ end
40
+ end
41
+
42
+ def module_name_from(filename)
43
+ filename = filename.to_s.gsub(filename.extname, '').gsub(/\/?index$/, '')
44
+ filename = nil if filename.length < 1
45
+ [@under, filename].compact.join('/')
46
+ end
47
+ def module_path_from(filename)
48
+ [@path || @under, filename.to_s].compact.reject(&:empty?).join('/')
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,18 @@
1
+ require 'jekyll'
2
+ require 'jekyll-importmap'
3
+ require 'jekyll-importmap/mappable'
4
+
5
+ module Jekyll::Importmap
6
+
7
+ # MappedFile = Struct.new(:name, :path, :preload, keyword_init: true)
8
+ # MappedDir = Struct.new(:dir, :under, :path, :preload, keyword_init: true)
9
+
10
+ class MappedFile < Jekyll::Importmap::Mappable
11
+ attr_reader :name
12
+
13
+ def initialize(hash)
14
+ super
15
+ @name = hash[:name]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module Jekyll::Importmap
2
+ JS_PATH = ''
3
+
4
+ class Resolver
5
+ def self.path_to_asset(path)
6
+ 'https://' + self.url + JS_PATH + '/' + path
7
+ end
8
+
9
+ def self.url
10
+ if Jekyll.configuration['port'] && Jekyll.configuration['port'].length > 0
11
+ Jekyll.configuration['host'] + ':' + Jekyll.configuration['port']
12
+ else
13
+ Jekyll.configuration['host']
14
+ end
15
+ end
16
+ end
17
+ end
@@ -5,6 +5,6 @@ module Liquid; class Tag; end; end
5
5
 
6
6
  module Jekyll
7
7
  class ImportmapTag < Liquid::Tag
8
- VERSION = "0.0.7"
8
+ VERSION = "0.1.0"
9
9
  end
10
10
  end
@@ -1,16 +1,57 @@
1
1
  require "jekyll"
2
2
  require "jekyll-importmap/version"
3
+ require "jekyll-importmap/map"
4
+ require "jekyll-importmap/mapped_dir"
5
+ require "jekyll-importmap/mapped_file"
6
+ require "jekyll-importmap/mappable"
7
+ require "jekyll-importmap/resolver"
3
8
  require "importmap-rails"
4
9
 
5
10
  module Jekyll
11
+ module Importmap; end
12
+
13
+ ES_MODULE_SHIM = "https://ga.jspm.io/npm:es-module-shims@1.8.3/dist/es-module-shims.js"
14
+ IMPORTMAP_PATH = "/importmap.rb"
15
+
6
16
  class ImportmapTag < Liquid::Tag
7
17
  def initialize(tag_name, text, tokens)
8
18
  super
19
+ root_path = Pathname.new(Jekyll.configuration['source'])
20
+ @importmap = Jekyll::Importmap::Map.new.draw(root_path.join(IMPORTMAP_PATH))
21
+ @entry_point = "application"
9
22
  end
10
23
 
11
24
  def render(context)
12
- return "Proof of concept"
25
+ import_map_tags(@entry_point, @importmap)
13
26
  end
27
+
28
+ private
29
+
30
+ def import_map_tags(entry_point, importmap)
31
+ [
32
+ inline_importmap_tags,
33
+ importmap_module_preload_tags,
34
+ import_module_tag,
35
+ es_module_shim_tag
36
+ ].join('')
37
+ end
38
+
39
+ def inline_importmap_tags
40
+ "<script type='importmap'>#{@importmap.to_json}</script>"
41
+ end
42
+ def importmap_module_preload_tags
43
+ #module_preload_tag(*importmap.preloaded_module_paths)
44
+ end
45
+ def module_preload_tag(*paths)
46
+ Array(paths).collect {|p| %(<link rel="modulepreload" href="#{p}">)}.join('\n')
47
+ end
48
+ def import_module_tag
49
+ imports = Array(@entry_points).collect {|e| %(import '#{e}')}.join('\n')
50
+ "<script type='module'>#{imports}</script>"
51
+ end
52
+ def es_module_shim_tag
53
+ "<script src='#{ES_MODULE_SHIM}' async='async'></script>"
54
+ end
14
55
  end
15
56
  end
16
57
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-importmap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Kidd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-16 00:00:00.000000000 Z
11
+ date: 2024-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -80,6 +80,11 @@ extensions: []
80
80
  extra_rdoc_files: []
81
81
  files:
82
82
  - lib/jekyll-importmap.rb
83
+ - lib/jekyll-importmap/map.rb
84
+ - lib/jekyll-importmap/mappable.rb
85
+ - lib/jekyll-importmap/mapped_dir.rb
86
+ - lib/jekyll-importmap/mapped_file.rb
87
+ - lib/jekyll-importmap/resolver.rb
83
88
  - lib/jekyll-importmap/version.rb
84
89
  homepage: https://github.com/n-at-han-k/jekyll-importmap
85
90
  licenses: