jekyll-js-converter 0.0.5 → 0.0.6

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: 1dbb1d9c51749616f197d23c0210a5f9cca4e46235a9ef8f0d64528903090d08
4
- data.tar.gz: 9d79b25157ea3579e0338fe6d9f74149ae5be2769b3d0a15f4c2cfc1e379205e
3
+ metadata.gz: 8f975186d3b4d088fb3ee18595c6b6f43b1226defb8f44d06e2683663c60a611
4
+ data.tar.gz: fd7abf2439a6a69133cdd2c0e55ed3f30fa8d8e2dc95698d1b1314cb934e4a60
5
5
  SHA512:
6
- metadata.gz: f4d31d5caff7f8eb48e6b1653ee6ffbcabaf853f6202355fd51370cdf53e0c634b5cc75972782b3348f10986c57483555ef662da6e0317491419d2febd171389
7
- data.tar.gz: 38a3249861d2afad8e7e0284812c647fc4b67f59e79c6d37d2c7b8b89ef6e9d2d4c8b2615d4a48f01bf6cb57960d27a7190578e7c57b952c88a917452c70ccd7
6
+ metadata.gz: '086d6cef9f35f63debb0ff5a0bfead0c11cc69e45f1e543ba3191cd6a619a7d3f7a45b8ebe465348de2ee2bec04b3d2d1155f7f52645fbca394ce589f91f1941'
7
+ data.tar.gz: ee944a5a249415e7308a67488a6d2e0bc44a7f2a71153301444da9dd18265c03d0529be0b2d02125acc751dd06c08b3973c1aa39a2df0ea0b1888ccd51636a82
@@ -7,4 +7,20 @@ module JekyllJsConverter
7
7
  @javascript_path ||= path_for '_javascript'
8
8
  end
9
9
  end
10
+
11
+ Jekyll::Hooks.register :pages, :pre_render do |page|
12
+ if page.is_a?(Jekyll::Page)
13
+ page.converters.each do |converter|
14
+ converter.associate_page(page) if converter.is_a?(Jekyll::Converters::Js)
15
+ end
16
+ end
17
+ end
18
+
19
+ Jekyll::Hooks.register :pages, :post_render do |page|
20
+ if page.is_a?(Jekyll::Page)
21
+ page.converters.each do |converter|
22
+ converter.dissociate_page(page) if converter.is_a?(Jekyll::Converters::Js)
23
+ end
24
+ end
25
+ end
10
26
  end
@@ -1,3 +1,3 @@
1
1
  module JekyllJsConverter
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'uglifier'
2
+ require 'jekyll/js_source_map_page'
2
3
 
3
4
  module Jekyll
4
5
  module Converters
@@ -8,21 +9,16 @@ module Jekyll
8
9
  safe true
9
10
  priority :low
10
11
 
11
- def initialize(config = {})
12
- @site = Jekyll.sites.last
13
- super
12
+ def associate_page(page)
13
+ @page = page
14
+ @site = @page.site
15
+ @source_map_page = JsSourceMapPage.new(@page)
14
16
  end
15
17
 
16
- def matches(ext)
17
- ext =~ /^\.js$/i
18
- end
19
-
20
- def output_ext(ext)
21
- '.js'
22
- end
23
-
24
- def safe?
25
- !!@config['safe']
18
+ def dissociate_page(page)
19
+ @page = nil
20
+ @site = nil
21
+ @source_map_page = nil
26
22
  end
27
23
 
28
24
  def javascript_config
@@ -60,21 +56,66 @@ module Jekyll
60
56
  end
61
57
  end
62
58
 
59
+ def matches(ext)
60
+ ext =~ /^\.js$/i
61
+ end
62
+
63
+ def output_ext(ext)
64
+ '.js'
65
+ end
66
+
67
+ def safe?
68
+ !!@config['safe']
69
+ end
70
+
63
71
  def convert(content)
64
- config = Jekyll::Utils.symbolize_hash_keys(
65
- Jekyll::Utils.deep_merge_hashes(
66
- { :uglifer => {} },
67
- javascript_config
72
+ if generate_source_map?
73
+ config = Jekyll::Utils.symbolize_hash_keys(
74
+ Jekyll::Utils.deep_merge_hashes(
75
+ { :uglifer => {
76
+ :source_map => {
77
+ :map_url => @source_map_page.name,
78
+ :sources_content => true,
79
+ :filename => @page.name
80
+ }
81
+ }
82
+ },
83
+ javascript_config
84
+ )
85
+ )
86
+
87
+ uglified, source_map = Uglifier.new(config[:uglifer]).compile_with_map(insert_imports(content))
88
+
89
+ @source_map_page.source_map(source_map)
90
+ @site.pages << @source_map_page
91
+
92
+ uglified
93
+ else
94
+ config = Jekyll::Utils.symbolize_hash_keys(
95
+ Jekyll::Utils.deep_merge_hashes(
96
+ { :uglifer => {} },
97
+ javascript_config
98
+ )
68
99
  )
69
- )
70
100
 
71
- Uglifier.new(config[:uglifer]).compile(insert_imports(content))
101
+ Uglifier.new(config[:uglifer]).compile(insert_imports(content))
102
+ end
72
103
  end
73
104
 
74
105
  private
75
106
 
107
+ def generate_source_map?
108
+ if @page.nil? || source_map_option.eql?(:never)
109
+ false
110
+ elsif source_map_option.eql?(:always)
111
+ true
112
+ else
113
+ :development == source_map_option == Jekyll.env.to_sym
114
+ end
115
+ end
116
+
76
117
  def insert_imports(content)
77
- content.enum_for(:scan, /^\W*=\s*(\w+)\W+([\w\/\\\-\.:]+)\W*$/).map {
118
+ content.enum_for(:scan, /^\/\/=\s*(\w+)\s+([\w\/\\\-\.:]+)\W*$/).map {
78
119
  { directive: Regexp.last_match[1],
79
120
  path: Regexp.last_match[2],
80
121
  insert_at: Regexp.last_match.end(0) }
@@ -109,6 +150,10 @@ module Jekyll
109
150
 
110
151
  content
111
152
  end
153
+
154
+ def source_map_option
155
+ javascript_config.fetch('source_map', :always).to_sym
156
+ end
112
157
  end
113
158
  end
114
159
  end
@@ -0,0 +1,29 @@
1
+ module Jekyll
2
+ class JsSourceMapPage < Page
3
+ def initialize(js_page)
4
+ @site = js_page.site
5
+ @dir = js_page.dir
6
+ @data = js_page.data
7
+ @name = js_page.basename + '.js.map'
8
+
9
+ process(@name)
10
+ Jekyll::Hooks.trigger :pages, :post_init, self
11
+ end
12
+
13
+ def source_map(map)
14
+ self.content = map
15
+ end
16
+
17
+ def ext
18
+ '.map'
19
+ end
20
+
21
+ def asset_file?
22
+ true
23
+ end
24
+
25
+ def inspect
26
+ "#<#{self.class} @name=#{name.inspect}>"
27
+ end
28
+ end
29
+ end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-js-converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Susco
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-01 00:00:00.000000000 Z
11
+ date: 2021-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: jekyll
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 4.2.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 4.2.0
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: uglifier
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -103,6 +89,7 @@ files:
103
89
  - lib/jekyll-js-converter.rb
104
90
  - lib/jekyll-js-converter/version.rb
105
91
  - lib/jekyll/converters/js.rb
92
+ - lib/jekyll/js_source_map_page.rb
106
93
  homepage: https://github.com/dsusco/jekyll-js-converter
107
94
  licenses:
108
95
  - MIT
@@ -122,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
109
  - !ruby/object:Gem::Version
123
110
  version: '0'
124
111
  requirements: []
125
- rubygems_version: 3.0.8
112
+ rubygems_version: 3.1.4
126
113
  signing_key:
127
114
  specification_version: 4
128
115
  summary: A JavaScript converter for Jekyll.