jekyll-importmap 0.0.7 → 0.1.1

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: 1f846c42361c7da6b6bac17de40f7d49d371d492edf7cedaee988aaa8acf8736
4
+ data.tar.gz: 4533b6d72f8cd5d654ae602f527eb9c5e6e583f10c4d76895f47f32b486ea96c
5
5
  SHA512:
6
- metadata.gz: ae3d1dc1f3f04a9a8834f6b27cbecc84d05d85414af4f864c1352a5500feb342736c1cf60992031c2076901e2dc9fb67f2d8ea272f0b428fdb4431bbf13e5c99
7
- data.tar.gz: d95160c39d963e14eaa30e43440f1cfc8afe3b1f4ce60226b4a50237a9b6a2f060c54bd6a9c5557ce76d2b2ceffad1a3d92709e7062f615ab650bb5724e6d24e
6
+ metadata.gz: 56dac94721fadb3a3be47564011703297c7abb6dedd81e263031d589d9c9bcd83400c58f37e3f13dd00f0e3444b14d0d97f9855aebe502e8af424359d7c81db0
7
+ data.tar.gz: ea08f250e99326219589c8f94344e713d557b765b6efe00845b146ff91daadeab77e356d166cf4c2085dfd01e33e55f1b51077d6814fc2f03a7cbfe98057430d
@@ -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.1"
9
9
  end
10
10
  end
@@ -1,16 +1,58 @@
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
+ root_path = Dir.pwd + '/'
21
+ @importmap = Jekyll::Importmap::Map.new.draw(root_path.join(IMPORTMAP_PATH))
22
+ @entry_point = "application"
9
23
  end
10
24
 
11
25
  def render(context)
12
- return "Proof of concept"
26
+ import_map_tags(@entry_point, @importmap)
13
27
  end
28
+
29
+ private
30
+
31
+ def import_map_tags(entry_point, importmap)
32
+ [
33
+ inline_importmap_tags,
34
+ importmap_module_preload_tags,
35
+ import_module_tag,
36
+ es_module_shim_tag
37
+ ].join('')
38
+ end
39
+
40
+ def inline_importmap_tags
41
+ "<script type='importmap'>#{@importmap.to_json}</script>"
42
+ end
43
+ def importmap_module_preload_tags
44
+ #module_preload_tag(*importmap.preloaded_module_paths)
45
+ end
46
+ def module_preload_tag(*paths)
47
+ Array(paths).collect {|p| %(<link rel="modulepreload" href="#{p}">)}.join('\n')
48
+ end
49
+ def import_module_tag
50
+ imports = Array(@entry_points).collect {|e| %(import '#{e}')}.join('\n')
51
+ "<script type='module'>#{imports}</script>"
52
+ end
53
+ def es_module_shim_tag
54
+ "<script src='#{ES_MODULE_SHIM}' async='async'></script>"
55
+ end
14
56
  end
15
57
  end
16
58
 
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.1
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
@@ -34,16 +34,16 @@ dependencies:
34
34
  name: importmap-rails
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
37
+ - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0'
39
+ version: 2.0.1
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
- - - ">="
44
+ - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '0'
46
+ version: 2.0.1
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -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: