jekyll-js-converter 0.0.2 → 1.0.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: 35ce19874bece0b15749342809e2be5c3ca1c61986c9e9fe50511e173d9a00a8
4
- data.tar.gz: 6e8fa014a1e3a5c9d95bcfff49560e1462dd6a154003205e392852789fd14832
3
+ metadata.gz: 01a1eeed7b7f346b0419859c5c4f59cd735c311f4f80ab1571147abf0d82cf66
4
+ data.tar.gz: 004070e0d99a326d423b20bf8bb3691c98e05d7e1089f7262304c3740317e297
5
5
  SHA512:
6
- metadata.gz: b348d0d6275cdfd5f8f9d380c88758fa88dcf623c9cdf715151de1a40d8493c0086e8d9b70c68abf1d1bb593c1c5bba9b0a045b6c7b8cd3badc1482b4e8c7246
7
- data.tar.gz: 3e83022159c18365ca3d2141e6c0c7ad972edc76d850862b6b7dc8a6dd298a24c0077bbb9704885ed9f86273ff930e2d6839725ddfd16fc964097f694404a12b
6
+ metadata.gz: 27142c7333b1ba84a3dea919ff2f3614e23b8d4f783f8c7280b97e55e6d9266924e0af3ddc1be79ba0ee6f9368013a2e70918ce18c03a9204d4dc25ca6cd86cb
7
+ data.tar.gz: a91ec043fbcc3c279616f47e462f8574dbbcc355962c4534605749653e9f77983bdcd79455424c9a6d8c77a602afa3527eb3b50413510cbf6958b42c620e779a
@@ -2,4 +2,25 @@ require 'jekyll-js-converter/version'
2
2
  require 'jekyll/converters/js'
3
3
 
4
4
  module JekyllJsConverter
5
+ class Jekyll::Theme
6
+ def javascript_path
7
+ @javascript_path ||= path_for '_javascript'
8
+ end
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
5
26
  end
@@ -1,3 +1,3 @@
1
1
  module JekyllJsConverter
2
- VERSION = '0.0.2'
2
+ VERSION = '1.0.0'
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,15 @@ 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
+ @source_map_page = JsSourceMapPage.new(@page)
14
15
  end
15
16
 
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']
17
+ def dissociate_page(page)
18
+ @page = nil
19
+ @site = nil
20
+ @source_map_page = nil
26
21
  end
27
22
 
28
23
  def javascript_config
@@ -35,19 +30,19 @@ module Jekyll
35
30
 
36
31
  def load_paths
37
32
  @load_paths ||= begin
38
- paths = [Jekyll.sanitized_path(@site.source, javascript_dir)]
39
- paths += javascript_config['load_paths'].map { |load_path| File.expand_path(load_path) } rescue []
33
+ paths = [Jekyll.sanitized_path(site.source, javascript_dir)]
34
+ paths += javascript_config['load_paths'].map { |load_path| File.expand_path(load_path, site.source) } rescue []
40
35
 
41
36
  if safe?
42
- paths.map! { |path| Jekyll.sanitized_path(@site.source, path) }
37
+ paths.map! { |path| Jekyll.sanitized_path(site.source, path) }
43
38
  end
44
39
 
45
- Dir.chdir(@site.source) do
40
+ Dir.chdir(site.source) do
46
41
  paths = paths.flat_map { |path| Dir.glob(path) }
47
42
 
48
43
  paths.map! do |path|
49
44
  if safe?
50
- Jekyll.sanitized_path(site_source, path)
45
+ Jekyll.sanitized_path(site.source, path)
51
46
  else
52
47
  File.expand_path(path)
53
48
  end
@@ -55,26 +50,71 @@ module Jekyll
55
50
  end
56
51
 
57
52
  paths.uniq!
58
- paths << @site.theme.javascript_path if @site.theme&.javascript_path
53
+ paths << site.theme.javascript_path if site.theme&.javascript_path
59
54
  paths.select { |path| File.directory?(path) }
60
55
  end
61
56
  end
62
57
 
58
+ def matches(ext)
59
+ ext =~ /^\.js$/i
60
+ end
61
+
62
+ def output_ext(ext = '')
63
+ '.js'
64
+ end
65
+
66
+ def safe?
67
+ !!@config['safe']
68
+ end
69
+
63
70
  def convert(content)
64
- config = Jekyll::Utils.symbolize_hash_keys(
65
- Jekyll::Utils.deep_merge_hashes(
66
- { :uglifer => {} },
67
- javascript_config
71
+ if generate_source_map?
72
+ config = Jekyll::Utils.symbolize_hash_keys(
73
+ Jekyll::Utils.deep_merge_hashes(
74
+ { :uglifer => {
75
+ :source_map => {
76
+ :map_url => @source_map_page.name,
77
+ :sources_content => true,
78
+ :filename => @page.name
79
+ }
80
+ }
81
+ },
82
+ javascript_config
83
+ )
84
+ )
85
+
86
+ uglified, source_map = Uglifier.new(config[:uglifer]).compile_with_map(insert_imports(content))
87
+
88
+ @source_map_page.source_map(source_map)
89
+ site.pages << @source_map_page
90
+
91
+ uglified
92
+ else
93
+ config = Jekyll::Utils.symbolize_hash_keys(
94
+ Jekyll::Utils.deep_merge_hashes(
95
+ { :uglifer => {} },
96
+ javascript_config
97
+ )
68
98
  )
69
- )
70
99
 
71
- Uglifier.new(config[:uglifer]).compile(insert_imports(content))
100
+ Uglifier.new(config[:uglifer]).compile(insert_imports(content))
101
+ end
72
102
  end
73
103
 
74
104
  private
75
105
 
106
+ def generate_source_map?
107
+ if @page.nil? || source_map_option.eql?(:never)
108
+ false
109
+ elsif source_map_option.eql?(:always)
110
+ true
111
+ else
112
+ :development == source_map_option && source_map_option == Jekyll.env.to_sym
113
+ end
114
+ end
115
+
76
116
  def insert_imports(content)
77
- content.enum_for(:scan, /^\W*=\s*(\w+)\W+([\w\/\\\-\.]+)\W*$/).map {
117
+ content.enum_for(:scan, /^\/\/=\s*(\w+)\s+([\w\/\\\-\.:]+)\W*$/).map {
78
118
  { directive: Regexp.last_match[1],
79
119
  path: Regexp.last_match[2],
80
120
  insert_at: Regexp.last_match.end(0) }
@@ -86,17 +126,21 @@ module Jekyll
86
126
  import_content = load_paths.reduce([]) { |files, load_path|
87
127
  glob = case match[:directive]
88
128
  when 'import'
89
- match[:path] += '.js' unless match[:path].end_with?('.js')
90
- File.join(load_path, '**', match[:path])
129
+ if (match[:path] =~ URI::regexp).nil?
130
+ match[:path] += '.js' unless match[:path].end_with?('.js')
131
+ Dir.glob(File.join(load_path, '**', match[:path]))
132
+ else
133
+ [match[:path]]
134
+ end
91
135
  when 'import_directory'
92
- File.join(load_path, '**', match[:path], '*.js')
136
+ Dir.glob(File.join(load_path, '**', match[:path], '*.js'))
93
137
  when 'import_tree'
94
- File.join(load_path, '**', match[:path], '**', '*.js')
138
+ Dir.glob(File.join(load_path, '**', match[:path], '**', '*.js'))
95
139
  end
96
140
 
97
- files + Dir.glob(glob)
141
+ files + glob
98
142
  }.uniq.reduce('') { |import_content, file|
99
- import_content += File.read(file)
143
+ import_content += (file =~ URI::regexp).nil? ? File.read(file) : Net::HTTP.get(URI(file))
100
144
  }
101
145
 
102
146
  content.insert(match[:insert_at], "\n#{insert_imports(import_content)}")
@@ -105,6 +149,14 @@ module Jekyll
105
149
 
106
150
  content
107
151
  end
152
+
153
+ def site
154
+ @site ||= @page.nil? ? Jekyll.sites.last : @page.site
155
+ end
156
+
157
+ def source_map_option
158
+ javascript_config.fetch('source_map', :always).to_sym
159
+ end
108
160
  end
109
161
  end
110
162
  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.2
4
+ version: 1.0.0
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-02-23 00:00:00.000000000 Z
11
+ date: 2021-03-30 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,7 +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
106
- - lib/jekyll/theme.rb
92
+ - lib/jekyll/js_source_map_page.rb
107
93
  homepage: https://github.com/dsusco/jekyll-js-converter
108
94
  licenses:
109
95
  - MIT
data/lib/jekyll/theme.rb DELETED
@@ -1,7 +0,0 @@
1
- module Jekyll
2
- class Theme
3
- def javascript_path
4
- @javascript_path ||= path_for '_javascript'
5
- end
6
- end
7
- end