jekyll-minify-js 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e1f72d786bd2ce0c1dbd137ff1ba6cebd86e821ddaf316ab29d7c069bbed93d7
4
- data.tar.gz: d64d7e54e46539d68d9de1bc960fa37b6addf655f861354291e622006996af14
3
+ metadata.gz: c041f8521ebee7c8566cf323651ccc353420efeef273f8fc3bdfd02663e2b113
4
+ data.tar.gz: '09726cc6fa6a01c886401c88598e31d51ba8f484a769d90e2123bb54e14cb4a5'
5
5
  SHA512:
6
- metadata.gz: ebf5214539d6cf1b969ee420d27c247f2cae2d3931ef8cf57ded6fa7673e322eee8f62bf49934515ccc8e72b0546b5c31b587816eac166b3545b1594245b36e6
7
- data.tar.gz: 615d71275d2ce2084feed54aa665b82faedc94e0e14d9853bcc4d6cef4e3f27e9466194dfcde019ebd1a00f1e745c40d797bfa5eb2b5e1047e01142f823b9d07
6
+ metadata.gz: 23b0a1eba4256d7ebeda0a84438c5726ad7bd3c116458215835e2b3e2f96e480ba9b269f345ddf45eb88713620379d9e899341db7c90224a9686e72090824372
7
+ data.tar.gz: 4951af9f563429564fd604263d3c183d9185c61d21df851080519ab84425d4387f6c33821d22768dd75a1ff6193aee0351ad6781ed8274148a71387108496866
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 phothinmg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/lib/color.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ def set_color(index, content, weight = 0)
4
+ "\e[#{weight};#{index}m#{content}\e[0m "
5
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll'
4
+ require 'fileutils'
5
+ require 'terser'
6
+
7
+ require_relative 'version'
8
+ require_relative 'color'
9
+
10
+ # module Jekyll
11
+ module Jekyll
12
+ # module Jekyll::MinifyJs
13
+ module MinifyJs
14
+ # class Jekyll::MinifyJs:TerserGenerator
15
+ class TerserGenerator < Jekyll::Generator
16
+ safe true
17
+ priority :low
18
+
19
+ def generate(_site)
20
+ # Work is done in the :post_write hook to avoid Jekyll overwriting outputs.
21
+ end
22
+
23
+ def self.run(site) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
24
+ config = site.config['minify_js'] || {}
25
+ # disable to skip minification
26
+ return if config['enable'] && config['enable'] == false
27
+
28
+ started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
29
+ # Build options from config
30
+ compress_opt = config['compress'] || true
31
+ mangle_opt = config['mangle'] || true
32
+ source_map_enabled = config['source_map'] || true
33
+
34
+ Jekyll.logger.info ''
35
+ Jekyll.logger.info set_color(96, "Running Jekyll MinifyJs v#{Jekyll::MinifyJs::VERSION}", 1)
36
+
37
+ entry_dir = config['entry_dir'] || 'js'
38
+ out_dir = config['output_dir'] || 'js'
39
+
40
+ if config['entry_dir']
41
+ Jekyll.logger.info "Entry : #{File.join(site.source, config['entry_dir'])}"
42
+ else
43
+ Jekyll.logger.info "Entry : No entry_dir found, look in #{File.join(site.source, 'js')}"
44
+ end
45
+
46
+ if config['output_dir']
47
+ Jekyll.logger.info "Output : #{File.join(site.dest, config['output_dir'])}"
48
+ else
49
+ Jekyll.logger.info "Output : No output_dir found, write to #{File.join(site.dest, 'js')}"
50
+ end
51
+
52
+ entry_dir_full = File.join(site.source, entry_dir)
53
+ out_dir_full = File.join(site.dest, out_dir)
54
+ js_files = Dir.glob(File.join(entry_dir_full, '**', '*.js'))
55
+
56
+ Jekyll.logger.warn 'MinifyJs:', "Entry directory not found: #{entry_dir_full}" unless Dir.exist?(entry_dir_full)
57
+ if Dir.exist?(entry_dir_full) && js_files.empty?
58
+ Jekyll.logger.info 'MinifyJs:',
59
+ "No JavaScript files found in #{entry_dir_full}"
60
+ end
61
+
62
+ FileUtils.mkdir_p(out_dir_full)
63
+
64
+ js_files.each do |src|
65
+ rel = src.sub(/\A#{Regexp.escape(entry_dir_full + File::SEPARATOR)}/, '')
66
+ out = File.join(out_dir_full, rel)
67
+ map_name = "#{File.basename(out)}.map"
68
+
69
+ begin
70
+ src_content = File.open(src, 'r:BOM|UTF-8', &:read)
71
+ options = {}
72
+ options[:compress] = compress_opt
73
+ options[:mangle] = mangle_opt
74
+ if source_map_enabled
75
+ options[:source_map] =
76
+ { filename: File.basename(src), output_filename: File.basename(out), sources_content: true,
77
+ url: map_name }
78
+ compiled, map = Terser.compile_with_map(src_content, options)
79
+ else
80
+ compiled = Terser.compile(src_content, options)
81
+ map = nil
82
+ end
83
+ compiled += "\n//# sourceMappingURL=#{map_name}\n" unless compiled.include?('sourceMappingURL')
84
+ File.write(out, compiled)
85
+ File.write("#{out}.map", map) if map
86
+ rescue StandardError => e
87
+ Jekyll.logger.warn 'Terser:', "ruby terser failed for #{rel}: #{e.message}; copying original"
88
+ FileUtils.cp(src, out)
89
+ end
90
+ end
91
+
92
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at
93
+ Jekyll.logger.info set_color(92, format('Done in %.2fs', elapsed), 1)
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ Jekyll::Hooks.register :site, :post_write do |site|
100
+ Jekyll::MinifyJs::TerserGenerator.run(site)
101
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module MinifyJs
5
+ VERSION = '0.2.1'
6
+ end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-minify-js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pho Thin Maung
@@ -37,34 +37,6 @@ dependencies:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '4.4'
40
- - !ruby/object:Gem::Dependency
41
- name: pathname
42
- requirement: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - "~>"
45
- - !ruby/object:Gem::Version
46
- version: 0.4.0
47
- type: :runtime
48
- prerelease: false
49
- version_requirements: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: 0.4.0
54
- - !ruby/object:Gem::Dependency
55
- name: shellwords
56
- requirement: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: 0.2.2
61
- type: :runtime
62
- prerelease: false
63
- version_requirements: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: 0.2.2
68
40
  - !ruby/object:Gem::Dependency
69
41
  name: terser
70
42
  requirement: !ruby/object:Gem::Requirement
@@ -84,11 +56,13 @@ executables: []
84
56
  extensions: []
85
57
  extra_rdoc_files: []
86
58
  files:
87
- - LICENSE
88
- - lib/jekyll_minify_js.rb
89
- homepage: https://rubygems.org/gems/hola
59
+ - LICENSE.txt
60
+ - lib/color.rb
61
+ - lib/jekyll-minify-js.rb
62
+ - lib/version.rb
63
+ homepage: https://github.com/phothinmg/jekyll-minify-js
90
64
  licenses:
91
- - ISC
65
+ - MIT
92
66
  metadata:
93
67
  rubygems_mfa_required: 'true'
94
68
  rdoc_options: []
@@ -98,14 +72,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
72
  requirements:
99
73
  - - ">="
100
74
  - !ruby/object:Gem::Version
101
- version: 3.4.8
75
+ version: '3.1'
102
76
  required_rubygems_version: !ruby/object:Gem::Requirement
103
77
  requirements:
104
78
  - - ">="
105
79
  - !ruby/object:Gem::Version
106
80
  version: '0'
107
81
  requirements: []
108
- rubygems_version: 3.6.9
82
+ rubygems_version: 4.0.11
109
83
  specification_version: 4
110
84
  summary: Jekyll plugin for Minify Js.
111
85
  test_files: []
data/LICENSE DELETED
@@ -1,19 +0,0 @@
1
- ISC License
2
-
3
- Copyright 2025 Pho Thin Maung<phothinmg@disroot.org>
4
-
5
- Permission to use, copy, modify, and/or distribute
6
- this software for any purpose with or without fee is
7
- hereby granted, provided that the above copyright
8
- notice and this permission notice appear in all copies.
9
-
10
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR
11
- DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
12
- INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
14
- FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15
- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
16
- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
17
- CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
18
- OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
19
- THIS SOFTWARE.
@@ -1,109 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'jekyll'
4
- require 'fileutils'
5
- require 'shellwords'
6
- begin
7
- require 'terser'
8
- rescue LoadError
9
- # terser gem not available; plugin will fallback to external CLI or copy
10
- end
11
- require 'pathname'
12
-
13
- # module Jekyll
14
- module Jekyll
15
- # MinifyJs is a Jekyll `Generator` that minifies JavaScript files found under
16
- # common asset directories. It prefers the `terser` Ruby gem when available
17
- # and falls back to an external `terser` CLI when necessary.
18
- class MinifyJs < Generator
19
- safe true
20
- priority :low
21
-
22
- def generate(_site)
23
- # Work is done in the :post_write hook to avoid Jekyll overwriting outputs.
24
- end
25
-
26
- def self.run(site) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
27
- # Support both repository-root layout and app/ layout.
28
- candidates = [
29
- File.join(site.source, 'assets', 'js'),
30
- File.join(site.source, 'app', 'assets', 'js')
31
- ]
32
- src_dir = candidates.find { |p| Dir.exist?(p) }
33
- return unless src_dir
34
-
35
- # Read configuration from _config.yml under `minify_js:` key
36
- cfg = site.config.fetch('minify_js', {})
37
- return if cfg.key?('enabled') && cfg['enabled'] == false
38
-
39
- dest_dir = File.join(site.dest, cfg.fetch('output_dir', 'assets/js'))
40
- FileUtils.mkdir_p(dest_dir)
41
-
42
- terser_available = defined?(Terser)
43
-
44
- # Build options from config
45
- compress_opt = cfg.fetch('compress', true)
46
- mangle_opt = cfg.fetch('mangle', true)
47
- source_map_enabled = cfg.fetch('source_map', true)
48
- exclude_patterns = cfg.fetch('exclude', ['**/*.min.js'])
49
-
50
- Dir.glob(File.join(src_dir, '**', '*.js')).each do |src| # rubocop:disable Metrics/BlockLength
51
- rel = Pathname.new(src).relative_path_from(Pathname.new(src_dir)).to_s
52
- out = File.join(dest_dir, rel)
53
- out_dir = File.dirname(out)
54
- FileUtils.mkdir_p(out_dir)
55
- map_name = "#{File.basename(out)}.map"
56
-
57
- # Skip excluded files
58
- next if exclude_patterns.any? { |pat| File.fnmatch(pat, rel) }
59
-
60
- if terser_available
61
- Jekyll.logger.info 'Terser:', "minifying #{rel} via terser Ruby gem"
62
- begin
63
- src_content = File.open(src, 'r:BOM|UTF-8', &:read)
64
- options = {}
65
- options[:compress] = compress_opt
66
- options[:mangle] = mangle_opt
67
- if source_map_enabled
68
- options[:source_map] =
69
- { filename: File.basename(src), output_filename: File.basename(out), sources_content: true,
70
- url: map_name }
71
- compiled, map = Terser.compile_with_map(src_content, options)
72
- else
73
- compiled = Terser.compile(src_content, options)
74
- map = nil
75
- end
76
- compiled += "\n//# sourceMappingURL=#{map_name}\n" unless compiled.include?('sourceMappingURL')
77
- File.write(out, compiled)
78
- File.write("#{out}.map", map) if map
79
- rescue StandardError => e
80
- Jekyll.logger.warn 'Terser:', "ruby terser failed for #{rel}: #{e.message}; copying original"
81
- FileUtils.cp(src, out)
82
- end
83
- else
84
- terser_cmd = new.detect_terser_cmd
85
- if terser_cmd
86
- cmd = %(#{terser_cmd} #{Shellwords.escape(src)} --compress --mangle -o #{Shellwords.escape(out)} --source-map "url='#{map_name}',includeSources") # rubocop:disable Layout/LineLength
87
- Jekyll.logger.info 'Terser:', "minifying #{rel} via external terser"
88
- success = system(cmd)
89
- unless success && File.exist?(out)
90
- Jekyll.logger.warn 'Terser:', "minify failed for #{rel}; copying original"
91
- FileUtils.cp(src, out)
92
- end
93
- else
94
- Jekyll.logger.warn 'Terser:', 'no terser available — copying original JS'
95
- FileUtils.cp(src, out)
96
- end
97
- end
98
- end
99
- end
100
-
101
- def detect_terser_cmd
102
- # Prefer the bundler-installed terser (from the `terser` gem) via `bundle exec terser`.
103
- return 'bundle exec terser' if system('bundle exec terser --version > /dev/null 2>&1')
104
- return 'terser' if system('terser --version > /dev/null 2>&1')
105
-
106
- nil
107
- end
108
- end
109
- end