jekyll-esm 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8be5ff2015145244e522bea4f4308490a483ab7fd42d53326ac7622ff17654dc
4
- data.tar.gz: e2c9e352a085a10545fc6f7307ea70eb63fd7be06b7e040b56bd55f4a264545b
3
+ metadata.gz: b01d8615804fbdbfefba76e803b0822a3362c8a326bc168e3a4524d92b9d29c8
4
+ data.tar.gz: 245540e681ec36358cc7aa2f4d5486309fc6cd75306d0115708503bdaad403e2
5
5
  SHA512:
6
- metadata.gz: a469a23ca7e7ba487fa8815cdadc11447efc763b574441c141bc33c695d1543a01d8c0da2088db493d8629b85855261e00b3ee0612ec7a1cddbbcea255be528c
7
- data.tar.gz: e6be99a03195e9ce1d882379285cfd4af39a7bf309d1b26a6e3beb4c3f2f49a383db8037f87c5760decd7f1472ac87fde76d737829a29f85deeb6cd0cbb429eb
6
+ metadata.gz: 002a1d5b99b0807c28ac704c2932f513461383464bfe13c602f2fcb0d8a920773fa48c22a000733c9df9a8ecb03c5af22bc15e131c010c518931801c5ea162aa
7
+ data.tar.gz: 718f012f937426ec3fa381577509df001bee47886570a1ff93601fe85bf9e6751ad2f69f113d2e52e4abc740ccada2676d5a1a26223970519a9ddeda4e7e4592
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jekyll-esm (0.2.4.1)
4
+ jekyll-esm (0.2.5)
5
5
  jekyll
6
6
  listen
7
7
  nokogiri
data/README.md CHANGED
@@ -4,7 +4,7 @@ This plugin will allow you to define esm module definitions directly in your mar
4
4
 
5
5
  ## Why?
6
6
 
7
- Looks like some of the major frameworks are moving away from asset bundlers as es6 is now able to do most of it internally, which is great. Now you can simply declare your esm definitions in the markup of your templates and with this plugin Jekyll will manage the installation behind the scenes with yarn (will make the package manager configurable in a later version).
7
+ Looks like some of the major frameworks are moving away from asset bundlers as es6 is now able to do most of it internally, which is great. Now you can simply declare your esm definitions in the markup of your templates and with this plugin Jekyll will manage the installation behind the scenes with yarn by default, but you can additionally specify npm or bower as the package manager.
8
8
 
9
9
 
10
10
  ## Installation
@@ -27,20 +27,34 @@ And add the following to your site's `_config.yml`
27
27
 
28
28
  ```yml
29
29
  plugins:
30
- - jekyll/esm
30
+ - jekyll-esm
31
31
  ```
32
32
 
33
- NOTE the `/` - the `-` variant is not available yet.
33
+ ## Select a package manager
34
+ jekyll-esm uses yarn by default but if you prefer npm or bower, its:
34
35
 
35
- ALSO NOTE - it's probably advisable to place this plugin as the very last one in the build pipe. EG:-
36
+ ``` yml
37
+ # _config.yml
36
38
 
37
- ```yml
38
- plugins:
39
- - jekyll-otherplug
40
- ...
41
- - jekyll/esm
39
+ esm:
40
+ manager: npm|bower
42
41
  ```
43
42
 
43
+ # Destination folder
44
+ Additionally you can rename the destination folder for production, so where by default it would output to _\_site/node_packages_ or _\_site/bower_components_, if you set the `dist`:
45
+
46
+ _A NOTE ON NPM_ if you don't have a _package.json_ file in your root, npm will actually search in the parent folder for a _package.json_.
47
+
48
+
49
+ ``` yml
50
+ # _config.yml
51
+
52
+ esm:
53
+ dist: dist
54
+ ```
55
+
56
+ Then all your managed packages will be available at `_site/dist`.
57
+
44
58
  # Ignore package.json!
45
59
  Currently You *MUST* exclude package.json in the `_config.yml` otherwise jekyll will go into a loop. Sucks a bit but will try and improve that.
46
60
 
data/jekyll-esm.gemspec CHANGED
@@ -1,4 +1,4 @@
1
- require_relative 'lib/jekyll/esm/version'
1
+ require_relative 'lib/jekyll-esm/version'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "jekyll-esm"
data/lib/jekyll-esm.rb ADDED
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll-esm/version"
4
+ require "jekyll-esm/managers/yarn"
5
+ require "jekyll-esm/managers/npm"
6
+ require "jekyll-esm/managers/bower"
7
+ require "jekyll"
8
+ require "open3"
9
+ require 'tmpdir'
10
+ require 'nokogiri'
11
+ require 'json'
12
+
13
+ module Jekyll
14
+ module Esm
15
+ @@existing_esm_packages = []
16
+ @@new_esm_packages = []
17
+ @@esm_ids = []
18
+
19
+ class Error < StandardError; end
20
+
21
+ def self.process(page)
22
+ site = page.site
23
+ manager = select_manager(site.config.dig('esm', 'manager'))
24
+ location = manager.location(site.config)
25
+
26
+ doc = Nokogiri::HTML(page.output)
27
+
28
+ import_maps = doc.search('script[type=importmap]')
29
+
30
+ import_maps.each do |value|
31
+ esm_id = value.attributes["data-esm-id"]&.value
32
+ # declare a data-esm-id so that jekyll will only process an esm declaration once
33
+ next if @@esm_ids.include?(esm_id)
34
+ @@esm_ids << esm_id if esm_id
35
+
36
+ importmap = JSON.parse(value.children[0].content)
37
+ imports = importmap["imports"]
38
+ imports.keys.each do |import_key|
39
+ # ignore urls
40
+ next if import_key =~ /https?:\/\/[\S]+/
41
+ next if imports[import_key] =~ /https?:\/\/[\S]+/
42
+ # ignore relative paths
43
+ next if import_key =~ /(^\.+\/)+/
44
+ # ignore absolute paths
45
+ next if import_key =~ /^\/[\S]+/
46
+
47
+ # ignore namespaces but only if it is not scoped
48
+ if import_key =~ /^@[\S]+/
49
+ import = import_key.split('/')[0..1].join('/')
50
+ else
51
+ import = import_key.split('/').first
52
+ end
53
+
54
+ pkg_path = File.join(site.source, location, import)
55
+
56
+ # don't repeatedly attempt to install a package
57
+ next if Dir.exists?(pkg_path) && @@new_esm_packages.include?(import)
58
+
59
+ @@new_esm_packages << import
60
+
61
+ manager.add(package: import, site: site)
62
+
63
+ end
64
+ end
65
+ end
66
+
67
+ def self.select_manager(name)
68
+ return Managers::Npm if name == 'npm'
69
+ return Managers::Bower if name == 'bower'
70
+ return Managers::Yarn
71
+ end
72
+
73
+ def self.apply(site)
74
+ manager = select_manager(site.config.dig('esm', 'manager'))
75
+ location = manager.location(site.config)
76
+ dist = manager.dist(site.config)
77
+
78
+ if @@existing_esm_packages.any?
79
+ for_removal = @@existing_esm_packages - @@new_esm_packages.uniq
80
+
81
+ # Remove any packages that are no longer referenced in an esm declaration
82
+ if for_removal.any?
83
+ packages = for_removal.join(' ')
84
+ manager.remove(packages: packages, site: site)
85
+ end
86
+ end
87
+
88
+ return unless Dir.exists?(File.join(site.source, location))
89
+
90
+ FileUtils.rm_rf(File.join(site.dest, dist))
91
+ FileUtils.cp_r(File.join(site.source, location), File.join(site.dest, dist))
92
+ @@existing_esm_packages = @@new_esm_packages
93
+ @@new_esm_packages = []
94
+ @@esm_ids = []
95
+ end
96
+ end
97
+ end
98
+
99
+
100
+ Jekyll::Hooks.register :pages, :post_render do |page|
101
+ Jekyll::Esm.process(page)
102
+ end
103
+
104
+ Jekyll::Hooks.register :documents, :post_render do |page|
105
+ Jekyll::Esm.process(page)
106
+ end
107
+
108
+ Jekyll::Hooks.register :posts, :post_render do |page|
109
+ Jekyll::Esm.process(page)
110
+ end
111
+
112
+ Jekyll::Hooks.register :site, :post_write do |site|
113
+ Jekyll::Esm.apply(site)
114
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ module Jekyll
5
+ module Esm
6
+ module Managers
7
+ module Bower
8
+ class Error < StandardError; end
9
+
10
+ def self.location(config)
11
+ config.dig('esm', 'bower', 'dir') || 'bower_components'
12
+ end
13
+
14
+ def self.dist(config)
15
+ config.dig('esm', 'dist') || location(config) || 'bower_components'
16
+ end
17
+
18
+ def self.add(package:, site:)
19
+ stdout, stderr, status = Open3.capture3(
20
+ "bower install #{package}",
21
+ chdir: File.expand_path(site.source)
22
+ )
23
+
24
+ if site.config.dig('esm', 'strict')
25
+ runtime_error = stdout =~ /ERROR in|SyntaxError/
26
+
27
+ raise Error, stderr if stderr.size > 0
28
+ raise Error, stdout if !runtime_error.nil?
29
+ end
30
+ end
31
+
32
+ def self.remove(packages:, site:)
33
+ stdout, stderr, status = Open3.capture3(
34
+ "bower uninstall #{packages}",
35
+ chdir: File.expand_path(site.source)
36
+ )
37
+
38
+ if site.config.dig('esm', 'strict')
39
+ runtime_error = stdout =~ /ERROR in|SyntaxError/
40
+
41
+ raise Error, stderr if stderr.size > 0
42
+ raise Error, stdout if !runtime_error.nil?
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ module Jekyll
5
+ module Esm
6
+ module Managers
7
+ module Npm
8
+ class Error < StandardError; end
9
+
10
+ def self.location(config)
11
+ config.dig('esm', 'npm', 'dir') || 'node_modules'
12
+ end
13
+
14
+ def self.dist(config)
15
+ config.dig('esm', 'dist') || location(config) || 'node_modules'
16
+ end
17
+
18
+ def self.add(package:, site:)
19
+ stdout, stderr, status = Open3.capture3(
20
+ "npm install #{package}",
21
+ chdir: File.expand_path(File.join(site.source, '/'))
22
+ )
23
+
24
+ if site.config.dig('esm', 'strict')
25
+ runtime_error = stdout =~ /ERROR in|SyntaxError/
26
+
27
+ raise Error, stderr if stderr.size > 0
28
+ raise Error, stdout if !runtime_error.nil?
29
+ end
30
+ end
31
+
32
+ def self.remove(packages:, site:)
33
+ stdout, stderr, status = Open3.capture3(
34
+ "npm uninstall #{packages}",
35
+ chdir: File.expand_path(File.join(site.source, '/'))
36
+ )
37
+
38
+ if site.config.dig('esm', 'strict')
39
+ runtime_error = stdout =~ /ERROR in|SyntaxError/
40
+
41
+ raise Error, stderr if stderr.size > 0
42
+ raise Error, stdout if !runtime_error.nil?
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ module Jekyll
5
+ module Esm
6
+ module Managers
7
+ module Yarn
8
+
9
+ def self.location(config)
10
+ config.dig('esm', 'yarn', 'dir') || 'node_modules'
11
+ end
12
+
13
+ def self.dist(config)
14
+ config.dig('esm', 'dist') || location(config) || 'node_modules'
15
+ end
16
+
17
+ def self.add(package:, site:)
18
+ stdout, stderr, status = Open3.capture3(
19
+ "yarn add #{package}",
20
+ chdir: File.expand_path(site.source)
21
+ )
22
+
23
+ if site.config.dig('esm', 'strict')
24
+ runtime_error = stdout =~ /ERROR in|SyntaxError/
25
+
26
+ raise Error, stderr if stderr.size > 0
27
+ raise Error, stdout if !runtime_error.nil?
28
+ end
29
+ end
30
+
31
+ def self.remove(packages:, site:)
32
+ stdout, stderr, status = Open3.capture3(
33
+ "yarn remove #{packages}",
34
+ chdir: File.expand_path(site.source)
35
+ )
36
+
37
+ if site.config.dig('esm', 'strict')
38
+ runtime_error = stdout =~ /ERROR in|SyntaxError/
39
+
40
+ raise Error, stderr if stderr.size > 0
41
+ raise Error, stdout if !runtime_error.nil?
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Esm
3
- VERSION = "0.2.5"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
data/lib/jekyll/esm.rb CHANGED
@@ -1,113 +1,2 @@
1
- # frozen_string_literal: true
2
-
3
- require "jekyll/esm/version"
4
- require "jekyll"
5
- require "open3"
6
- require 'tmpdir'
7
- require 'nokogiri'
8
- require 'json'
9
-
10
- module Jekyll
11
- module Esm
12
- @@existing_esm_packages = []
13
- @@new_esm_packages = []
14
- @@esm_ids = []
15
-
16
- class Error < StandardError; end
17
-
18
- def self.process(page)
19
- doc = Nokogiri::HTML(page.output)
20
-
21
- import_maps = doc.search('script[type=importmap]')
22
-
23
- import_maps.each do |value|
24
- esm_id = value.attributes["data-esm-id"]&.value
25
- # declare a data-esm-id so that jekyll will only process an esm declaration once
26
- next if @@esm_ids.include?(esm_id)
27
- @@esm_ids << esm_id if esm_id
28
-
29
- importmap = JSON.parse(value.children[0].content)
30
- imports = importmap["imports"]
31
- imports.keys.each do |import_key|
32
- # ignore urls
33
- next if import_key =~ /https?:\/\/[\S]+/
34
- next if imports[import_key] =~ /https?:\/\/[\S]+/
35
- # ignore relative paths
36
- next if import_key =~ /(^\.+\/)+/
37
- # ignore absolute paths
38
- next if import_key =~ /^\/[\S]+/
39
-
40
- # ignore namespaces but only if it is not scoped
41
- if import_key =~ /^@[\S]+/
42
- import = import_key.split('/')[0..1].join('/')
43
- else
44
- import = import_key.split('/').first
45
- end
46
-
47
- pkg_path = File.join(page.site.source, 'node_modules', import)
48
-
49
- # don't repeatedly attempt to install a package
50
- next if Dir.exists?(pkg_path) && @@new_esm_packages.include?(import)
51
-
52
- @@new_esm_packages << import
53
-
54
- stdout, stderr, status = Open3.capture3(
55
- "yarn add #{import}",
56
- chdir: File.expand_path(page.site.source)
57
- )
58
-
59
- if page.site.config.dig('esm', 'strict')
60
- runtime_error = stdout =~ /ERROR in|SyntaxError/
61
-
62
- raise Error, stderr if stderr.size > 0
63
- raise Error, stdout if !runtime_error.nil?
64
- end
65
- end
66
- end
67
- end
68
-
69
- def self.apply(site)
70
- if @@existing_esm_packages.any?
71
- for_removal = @@existing_esm_packages - @@new_esm_packages.uniq
72
-
73
- # Remove any packages that are no longer referenced in an esm declaration
74
- if for_removal.any?
75
- stdout, stderr, status = Open3.capture3(
76
- "yarn remove #{for_removal.join(' ')}",
77
- chdir: File.expand_path(site.source)
78
- )
79
-
80
- if site.config.dig('esm', 'strict')
81
- runtime_error = stdout =~ /ERROR in|SyntaxError/
82
-
83
- raise Error, stderr if stderr.size > 0
84
- raise Error, stdout if !runtime_error.nil?
85
- end
86
- end
87
- end
88
-
89
- FileUtils.rm_rf(File.join(site.dest, 'node_modules'))
90
- FileUtils.cp_r(File.join(site.source, 'node_modules'), File.join(site.dest, 'node_modules'))
91
- @@existing_esm_packages = @@new_esm_packages
92
- @@new_esm_packages = []
93
- @@esm_ids = []
94
- end
95
- end
96
- end
97
-
98
-
99
- Jekyll::Hooks.register :pages, :post_render do |page|
100
- Jekyll::Esm.process(page)
101
- end
102
-
103
- Jekyll::Hooks.register :documents, :post_render do |page|
104
- Jekyll::Esm.process(page)
105
- end
106
-
107
- Jekyll::Hooks.register :posts, :post_render do |page|
108
- Jekyll::Esm.process(page)
109
- end
110
-
111
- Jekyll::Hooks.register :site, :post_write do |site|
112
- Jekyll::Esm.apply(site)
113
- end
1
+ # keep support for _config plugin syntax '- jekyll/esm'
2
+ require_relative '../jekyll-esm.rb'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-esm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Martin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-19 00:00:00.000000000 Z
11
+ date: 2021-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -157,8 +157,12 @@ files:
157
157
  - bin/console
158
158
  - bin/setup
159
159
  - jekyll-esm.gemspec
160
+ - lib/jekyll-esm.rb
161
+ - lib/jekyll-esm/managers/bower.rb
162
+ - lib/jekyll-esm/managers/npm.rb
163
+ - lib/jekyll-esm/managers/yarn.rb
164
+ - lib/jekyll-esm/version.rb
160
165
  - lib/jekyll/esm.rb
161
- - lib/jekyll/esm/version.rb
162
166
  - package.json
163
167
  - yarn.lock
164
168
  homepage: https://github.com/tevio/jekyll-esm