jekyll-js-converter 0.0.1 → 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: 3e277853609dc1c1eb7a4972df2edd963bb0d3ae0cc7d76ba72091a7d520cba8
4
- data.tar.gz: 29bd1ada5f27ac057e0e1abc9fd0fc831725b88e5843cb94649984c05490d051
3
+ metadata.gz: 8f975186d3b4d088fb3ee18595c6b6f43b1226defb8f44d06e2683663c60a611
4
+ data.tar.gz: fd7abf2439a6a69133cdd2c0e55ed3f30fa8d8e2dc95698d1b1314cb934e4a60
5
5
  SHA512:
6
- metadata.gz: 979134c9cbb28134ac6a4a7711856046223edcbde76d2ec0c35df449d655ec3a9a32349d28e548fd7836019db178bf5202c10afdc0e1795b3edc88539efc1041
7
- data.tar.gz: f31e3d05b155c931b342499705c3fe93f26e7bf9be074edf1140d48ec96f25cba2537b6f3cdfcab578b7888b003ab9d48fff36921411f399f2c7f9a4396091cf
6
+ metadata.gz: '086d6cef9f35f63debb0ff5a0bfead0c11cc69e45f1e543ba3191cd6a619a7d3f7a45b8ebe465348de2ee2bec04b3d2d1155f7f52645fbca394ce589f91f1941'
7
+ data.tar.gz: ee944a5a249415e7308a67488a6d2e0bc44a7f2a71153301444da9dd18265c03d0529be0b2d02125acc751dd06c08b3973c1aa39a2df0ea0b1888ccd51636a82
@@ -0,0 +1,26 @@
1
+ require 'jekyll-js-converter/version'
2
+ require 'jekyll/converters/js'
3
+
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
26
+ end
@@ -0,0 +1,3 @@
1
+ module JekyllJsConverter
2
+ VERSION = '0.0.6'
3
+ end
@@ -0,0 +1,159 @@
1
+ require 'uglifier'
2
+ require 'jekyll/js_source_map_page'
3
+
4
+ module Jekyll
5
+ module Converters
6
+ class Js < Converter
7
+ DIRECTIVES = %w(import import_directory import_tree)
8
+
9
+ safe true
10
+ priority :low
11
+
12
+ def associate_page(page)
13
+ @page = page
14
+ @site = @page.site
15
+ @source_map_page = JsSourceMapPage.new(@page)
16
+ end
17
+
18
+ def dissociate_page(page)
19
+ @page = nil
20
+ @site = nil
21
+ @source_map_page = nil
22
+ end
23
+
24
+ def javascript_config
25
+ @javascript_config ||= @config['javascript'] || {}
26
+ end
27
+
28
+ def javascript_dir
29
+ javascript_config['javascript_dir'].to_s.empty? ? '_javascript' : javascript_config['javascript_dir']
30
+ end
31
+
32
+ def load_paths
33
+ @load_paths ||= begin
34
+ paths = [Jekyll.sanitized_path(@site.source, javascript_dir)]
35
+ paths += javascript_config['load_paths'].map { |load_path| File.expand_path(load_path) } rescue []
36
+
37
+ if safe?
38
+ paths.map! { |path| Jekyll.sanitized_path(@site.source, path) }
39
+ end
40
+
41
+ Dir.chdir(@site.source) do
42
+ paths = paths.flat_map { |path| Dir.glob(path) }
43
+
44
+ paths.map! do |path|
45
+ if safe?
46
+ Jekyll.sanitized_path(site_source, path)
47
+ else
48
+ File.expand_path(path)
49
+ end
50
+ end
51
+ end
52
+
53
+ paths.uniq!
54
+ paths << @site.theme.javascript_path
55
+ paths.select { |path| File.directory?(path) }
56
+ end
57
+ end
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
+
71
+ def convert(content)
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
+ )
99
+ )
100
+
101
+ Uglifier.new(config[:uglifer]).compile(insert_imports(content))
102
+ end
103
+ end
104
+
105
+ private
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
+
117
+ def insert_imports(content)
118
+ content.enum_for(:scan, /^\/\/=\s*(\w+)\s+([\w\/\\\-\.:]+)\W*$/).map {
119
+ { directive: Regexp.last_match[1],
120
+ path: Regexp.last_match[2],
121
+ insert_at: Regexp.last_match.end(0) }
122
+ }.sort { |a, b|
123
+ # start inserting at the end of the file so the insert_at's remain accurate as the content's length changes
124
+ b[:insert_at] <=> a[:insert_at]
125
+ }.each { |match|
126
+ if DIRECTIVES.include?(match[:directive])
127
+ import_content = load_paths.reduce([]) { |files, load_path|
128
+ glob = case match[:directive]
129
+ when 'import'
130
+ if (match[:path] =~ URI::regexp).nil?
131
+ match[:path] += '.js' unless match[:path].end_with?('.js')
132
+ Dir.glob(File.join(load_path, '**', match[:path]))
133
+ else
134
+ [match[:path]]
135
+ end
136
+ when 'import_directory'
137
+ Dir.glob(File.join(load_path, '**', match[:path], '*.js'))
138
+ when 'import_tree'
139
+ Dir.glob(File.join(load_path, '**', match[:path], '**', '*.js'))
140
+ end
141
+
142
+ files + glob
143
+ }.uniq.reduce('') { |import_content, file|
144
+ import_content += (file =~ URI::regexp).nil? ? File.read(file) : Net::HTTP.get(URI(file))
145
+ }
146
+
147
+ content.insert(match[:insert_at], "\n#{insert_imports(import_content)}")
148
+ end
149
+ }
150
+
151
+ content
152
+ end
153
+
154
+ def source_map_option
155
+ javascript_config.fetch('source_map', :always).to_sym
156
+ end
157
+ end
158
+ end
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.1
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-02-23 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
@@ -99,7 +85,11 @@ email:
99
85
  executables: []
100
86
  extensions: []
101
87
  extra_rdoc_files: []
102
- files: []
88
+ files:
89
+ - lib/jekyll-js-converter.rb
90
+ - lib/jekyll-js-converter/version.rb
91
+ - lib/jekyll/converters/js.rb
92
+ - lib/jekyll/js_source_map_page.rb
103
93
  homepage: https://github.com/dsusco/jekyll-js-converter
104
94
  licenses:
105
95
  - MIT