jekyll-js-converter 0.0.1 → 0.0.2

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: 35ce19874bece0b15749342809e2be5c3ca1c61986c9e9fe50511e173d9a00a8
4
+ data.tar.gz: 6e8fa014a1e3a5c9d95bcfff49560e1462dd6a154003205e392852789fd14832
5
5
  SHA512:
6
- metadata.gz: 979134c9cbb28134ac6a4a7711856046223edcbde76d2ec0c35df449d655ec3a9a32349d28e548fd7836019db178bf5202c10afdc0e1795b3edc88539efc1041
7
- data.tar.gz: f31e3d05b155c931b342499705c3fe93f26e7bf9be074edf1140d48ec96f25cba2537b6f3cdfcab578b7888b003ab9d48fff36921411f399f2c7f9a4396091cf
6
+ metadata.gz: b348d0d6275cdfd5f8f9d380c88758fa88dcf623c9cdf715151de1a40d8493c0086e8d9b70c68abf1d1bb593c1c5bba9b0a045b6c7b8cd3badc1482b4e8c7246
7
+ data.tar.gz: 3e83022159c18365ca3d2141e6c0c7ad972edc76d850862b6b7dc8a6dd298a24c0077bbb9704885ed9f86273ff930e2d6839725ddfd16fc964097f694404a12b
@@ -0,0 +1,5 @@
1
+ require 'jekyll-js-converter/version'
2
+ require 'jekyll/converters/js'
3
+
4
+ module JekyllJsConverter
5
+ end
@@ -0,0 +1,3 @@
1
+ module JekyllJsConverter
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,110 @@
1
+ require 'uglifier'
2
+
3
+ module Jekyll
4
+ module Converters
5
+ class Js < Converter
6
+ DIRECTIVES = %w(import import_directory import_tree)
7
+
8
+ safe true
9
+ priority :low
10
+
11
+ def initialize(config = {})
12
+ @site = Jekyll.sites.last
13
+ super
14
+ end
15
+
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']
26
+ end
27
+
28
+ def javascript_config
29
+ @javascript_config ||= @config['javascript'] || {}
30
+ end
31
+
32
+ def javascript_dir
33
+ javascript_config['javascript_dir'].to_s.empty? ? '_javascript' : javascript_config['javascript_dir']
34
+ end
35
+
36
+ def load_paths
37
+ @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 []
40
+
41
+ if safe?
42
+ paths.map! { |path| Jekyll.sanitized_path(@site.source, path) }
43
+ end
44
+
45
+ Dir.chdir(@site.source) do
46
+ paths = paths.flat_map { |path| Dir.glob(path) }
47
+
48
+ paths.map! do |path|
49
+ if safe?
50
+ Jekyll.sanitized_path(site_source, path)
51
+ else
52
+ File.expand_path(path)
53
+ end
54
+ end
55
+ end
56
+
57
+ paths.uniq!
58
+ paths << @site.theme.javascript_path if @site.theme&.javascript_path
59
+ paths.select { |path| File.directory?(path) }
60
+ end
61
+ end
62
+
63
+ def convert(content)
64
+ config = Jekyll::Utils.symbolize_hash_keys(
65
+ Jekyll::Utils.deep_merge_hashes(
66
+ { :uglifer => {} },
67
+ javascript_config
68
+ )
69
+ )
70
+
71
+ Uglifier.new(config[:uglifer]).compile(insert_imports(content))
72
+ end
73
+
74
+ private
75
+
76
+ def insert_imports(content)
77
+ content.enum_for(:scan, /^\W*=\s*(\w+)\W+([\w\/\\\-\.]+)\W*$/).map {
78
+ { directive: Regexp.last_match[1],
79
+ path: Regexp.last_match[2],
80
+ insert_at: Regexp.last_match.end(0) }
81
+ }.sort { |a, b|
82
+ # start inserting at the end of the file so the insert_at's remain accurate as the content's length changes
83
+ b[:insert_at] <=> a[:insert_at]
84
+ }.each { |match|
85
+ if DIRECTIVES.include?(match[:directive])
86
+ import_content = load_paths.reduce([]) { |files, load_path|
87
+ glob = case match[:directive]
88
+ when 'import'
89
+ match[:path] += '.js' unless match[:path].end_with?('.js')
90
+ File.join(load_path, '**', match[:path])
91
+ when 'import_directory'
92
+ File.join(load_path, '**', match[:path], '*.js')
93
+ when 'import_tree'
94
+ File.join(load_path, '**', match[:path], '**', '*.js')
95
+ end
96
+
97
+ files + Dir.glob(glob)
98
+ }.uniq.reduce('') { |import_content, file|
99
+ import_content += File.read(file)
100
+ }
101
+
102
+ content.insert(match[:insert_at], "\n#{insert_imports(import_content)}")
103
+ end
104
+ }
105
+
106
+ content
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,7 @@
1
+ module Jekyll
2
+ class Theme
3
+ def javascript_path
4
+ @javascript_path ||= path_for '_javascript'
5
+ end
6
+ end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Susco
@@ -99,7 +99,11 @@ email:
99
99
  executables: []
100
100
  extensions: []
101
101
  extra_rdoc_files: []
102
- files: []
102
+ files:
103
+ - lib/jekyll-js-converter.rb
104
+ - lib/jekyll-js-converter/version.rb
105
+ - lib/jekyll/converters/js.rb
106
+ - lib/jekyll/theme.rb
103
107
  homepage: https://github.com/dsusco/jekyll-js-converter
104
108
  licenses:
105
109
  - MIT