jekyll-minify-js 0.2.13 → 0.2.14

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: acd026f57e7f1f4e1b695587f25a83a067ecbae57514787f6234155f85940def
4
- data.tar.gz: 6de165cf4f0d48fb066417a54d7d844bcfe28185966edd1b17c7ef1846a4edef
3
+ metadata.gz: b44a42e35257584ee45662ee7739a91e85cde0769071ed2373091278955dfa18
4
+ data.tar.gz: f84f80cc36b6eba9160a5424aef9f1bb77cd74b463d4ec702f83c0575d4f10e8
5
5
  SHA512:
6
- metadata.gz: b072b14a5eda7cf28a971c585caf4c682b795671fff04d6c1c40c5372d6abe5e50c2b7a141d05b4ce1a4f8a714f7fc5fc53c1115554c9a763c873e42a26d11ad
7
- data.tar.gz: 7f916fa240b4a3d415ab0d7f25b9722882ce44801e16410b975a995298aa31751a74c9fecb6b81646a89005bcfadde3ec100d1b0f206895a1a5b447eb18cc3f6
6
+ metadata.gz: dceea9e11499497a69af5bc29031006c651a4428b83de86634822d2f4e91836d1738595c5cc9ad14881e33d15b77b8b9b50088226c44f12b99c7ea59f875e3d0
7
+ data.tar.gz: 303a138bf705689bbde1b7da5c875a2c802be8a52dd32c230b23866cf63eba6eb7fc726811cba23fafbef92d3edcd98838e0e778822468cd702f9bc38fa9dc60
data/README.md CHANGED
@@ -42,10 +42,9 @@ minify_js:
42
42
  enable: true
43
43
  entry_dir: js
44
44
  output_dir: js
45
- terser_opts:
46
- compress: true
47
- mangle: true
48
- source_map: true
45
+ compress: true
46
+ mangle: true
47
+ source_map: true
49
48
  ```
50
49
 
51
50
  ### Options
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
@@ -1,131 +1,100 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "logger"
4
+
3
5
  require "jekyll"
4
- require "json"
5
- require "open3"
6
- require "pathname"
6
+ require "fileutils"
7
+ require "terser"
7
8
 
8
9
  require_relative "version"
10
+ require_relative "color"
9
11
 
12
+ # module Jekyll
10
13
  module Jekyll
11
- # Minifies JavaScript assets after Jekyll finishes writing the site.
12
- #
13
- # The plugin reads files from a configured entry directory, invokes the
14
- # bundled Node-based terser wrapper, and writes the compiled assets into the
15
- # destination site output.
14
+ # module Jekyll::MinifyJs
16
15
  module MinifyJs
17
- # Runs JavaScript minification from Jekyll's post-write lifecycle.
18
- #
19
- # The generator itself does not perform work during the normal generation
20
- # phase. Instead, {run} is invoked from a `:post_write` hook so Jekyll does
21
- # not overwrite the generated minified files.
16
+ # class Jekyll::MinifyJs:TerserGenerator
22
17
  class TerserGenerator < Jekyll::Generator
23
18
  safe true
24
19
  priority :low
25
20
 
26
- # Declares the generator without doing work during the normal build phase.
27
- #
28
- # @param _site [Jekyll::Site] the site being generated
29
- # @return [void]
30
21
  def generate(_site)
31
22
  # Work is done in the :post_write hook to avoid Jekyll overwriting outputs.
32
23
  end
33
24
 
34
- # Merges the plugin configuration with defaults.
35
- #
36
- # @param site [Jekyll::Site] the current site instance
37
- # @return [Hash] the merged `minify_js` configuration
38
- def self.minify_js_config(site)
39
- default_config = {
40
- "enabled" => true,
41
- "entry_dir" => "js",
42
- "output_dir" => "js",
43
- "terser_opts" => {
44
- "source_map" => true,
45
- "compress" => true
46
- }
47
- }
48
- site.config["minify_js"] = default_config.merge(site.config["minify_js"] || {})
49
- end
25
+ def self.run(site) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
26
+ config = site.config["minify_js"] || {}
27
+ # disable to skip minification
28
+ return if config["enable"] && config["enable"] == false
50
29
 
51
- # Runs the Node-based terser wrapper for one JavaScript source string.
52
- #
53
- # @param code [String] the JavaScript source to minify
54
- # @param ts_opts [Hash, nil] terser options passed to the wrapper
55
- # @return [Hash] the parsed JSON response from the wrapper
56
- # @raise [RuntimeError] if the wrapper process exits unsuccessfully
57
- def self.run_terser(code, ts_opts)
58
- script_path = File.expand_path("../assets/index.js", __dir__)
59
- input = JSON.generate({
60
- code: code,
61
- opts: ts_opts
62
- }.compact)
63
- stdout, stderr, status = Open3.capture3("node", script_path, stdin_data: input)
64
- raise "Minify JS failed: #{stderr}" unless status.success?
65
-
66
- JSON.parse(stdout)
67
- end
30
+ started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
31
+ # Build options from config
32
+ compress_opt = config.fetch("compress", true)
33
+ mangle_opt = config.fetch("mangle", true)
34
+ source_map_enabled = config.fetch("source_map", true)
68
35
 
69
- # Finds JavaScript files in the configured entry directory.
70
- #
71
- # @param site [Jekyll::Site] the current site instance
72
- # @param entry [String, nil] the source directory relative to `site.source`
73
- # @return [Array<String>, nil] matching absolute file paths, or `nil` when
74
- # no entry directory is configured or no files are found
75
- def self.entry_js_files(site, entry)
76
- entry_full = File.join(site.source, entry)
77
- js_files = Dir.glob(File.join(entry_full, "**", "*.js"))
78
- if Dir.exist?(entry_full) && js_files.empty?
79
- Jekyll.logger.warn "MinifyJs:",
80
- "No JavaScript files found in #{entry_full}."
81
- end
82
- return if entry.nil? || js_files.empty?
36
+ Jekyll.logger.info ""
37
+ Jekyll.logger.info set_color(96, "Running Jekyll MinifyJs v#{Jekyll::MinifyJs::VERSION}", 1)
83
38
 
84
- js_files
85
- end
39
+ entry_dir = config["entry_dir"] || "js"
40
+ out_dir = config["output_dir"] || "js"
86
41
 
87
- # Resolves the output directory for minified assets.
88
- #
89
- # @param site [Jekyll::Site] the current site instance
90
- # @param out [String, nil] the configured output directory
91
- # @param entry [String] the configured entry directory
92
- # @return [String] the absolute destination directory inside `site.dest`
93
- def self.output_dir(site, out, entry)
94
- out ? File.join(site.dest, out) : File.join(site.dest, entry)
95
- end
42
+ if config["entry_dir"]
43
+ Jekyll.logger.info "Entry : #{File.join(site.source, config["entry_dir"])}"
44
+ else
45
+ Jekyll.logger.info "Entry : No entry_dir found, look in #{File.join(site.source, "js")}"
46
+ end
47
+
48
+ if config["output_dir"]
49
+ Jekyll.logger.info "Output : #{File.join(site.dest, config["output_dir"])}"
50
+ else
51
+ Jekyll.logger.info "Output : No output_dir found, write to #{File.join(site.dest, "js")}"
52
+ end
96
53
 
97
- # Minifies every JavaScript file configured for the site.
98
- #
99
- # Files are written into the resolved output directory. If minification of
100
- # an individual file fails, the original file is copied instead.
101
- #
102
- # @param site [Jekyll::Site] the current site instance
103
- # @return [void]
104
- def self.run(site) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
105
- Jekyll.logger.info "Running jekyll-minify-js v#{Jekyll::MinifyJs::VERSION}"
106
- mini_js_config = minify_js_config(site)
107
- entry_dir = mini_js_config["entry_dir"]
108
- nil if (mini_js_config["enable"] && mini_js_config["enable"] == false) || entry_dir.nil?
109
- out_dir = output_dir(site, mini_js_config["output_dir"], entry_dir)
110
- ts_opts = mini_js_config["terser_opts"]
111
54
  entry_dir_full = File.join(site.source, entry_dir)
112
- js_files = entry_js_files(site, entry_dir)
55
+ out_dir_full = File.join(site.dest, out_dir)
56
+ js_files = Dir.glob(File.join(entry_dir_full, "**", "*.js"))
57
+
58
+ Jekyll.logger.warn "MinifyJs:", "Entry directory not found: #{entry_dir_full}" unless Dir.exist?(entry_dir_full)
59
+ if Dir.exist?(entry_dir_full) && js_files.empty?
60
+ Jekyll.logger.info "MinifyJs:",
61
+ "No JavaScript files found in #{entry_dir_full}"
62
+ end
63
+
64
+ FileUtils.mkdir_p(out_dir_full)
113
65
 
114
66
  js_files.each do |src|
115
67
  rel = src.sub(/\A#{Regexp.escape(entry_dir_full + File::SEPARATOR)}/, "")
116
- out = File.join(out_dir, rel)
68
+ out = File.join(out_dir_full, rel)
117
69
  map_name = "#{File.basename(out)}.map"
118
- src_content = File.read(src)
119
- result = run_terser(src_content, ts_opts)
120
- compiled = result["compiled"]
121
- source_map = result["source_map"]
122
- compiled += "\n//# sourceMappingURL=#{map_name}\n" if source_map && !compiled.include?("sourceMappingURL")
123
- File.write(out, compiled)
124
- File.write("#{out}.map", source_map) if source_map
125
- rescue StandardError => e
126
- Jekyll.logger.warn "jekyll-minify-js:", "failed to minify for #{rel}: #{e.message}; copying original"
127
- FileUtils.cp(src, out)
70
+
71
+ begin
72
+ src_content = File.open(src, "r:BOM|UTF-8", &:read)
73
+ options = {}
74
+ options[:compress] = compress_opt
75
+ options[:mangle] = mangle_opt
76
+ if source_map_enabled
77
+ options[:source_map] =
78
+ { filename: File.basename(src), output_filename: File.basename(out), sources_content: true,
79
+ url: map_name }
80
+ compiled, map = Terser.compile_with_map(src_content, options)
81
+ else
82
+ compiled = Terser.compile(src_content, options)
83
+ map = nil
84
+ end
85
+ if source_map_enabled && !compiled.include?("sourceMappingURL")
86
+ compiled += "\n//# sourceMappingURL=#{map_name}\n"
87
+ end
88
+ File.write(out, compiled)
89
+ File.write("#{out}.map", map) if map
90
+ rescue StandardError => e
91
+ Jekyll.logger.warn "Terser:", "ruby terser failed for #{rel}: #{e.message}; copying original"
92
+ FileUtils.cp(src, out)
93
+ end
128
94
  end
95
+
96
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at
97
+ Jekyll.logger.info set_color(92, format("Done in %.2fs", elapsed), 1)
129
98
  end
130
99
  end
131
100
  end
data/lib/version.rb CHANGED
@@ -6,6 +6,6 @@ module Jekyll
6
6
  # Current gem version.
7
7
  #
8
8
  # @return [String]
9
- VERSION = "0.2.13"
9
+ VERSION = "0.2.14"
10
10
  end
11
11
  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.2.13
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - phothinmg
@@ -24,19 +24,19 @@ dependencies:
24
24
  - !ruby/object:Gem::Version
25
25
  version: '4.4'
26
26
  - !ruby/object:Gem::Dependency
27
- name: open3
27
+ name: terser
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 0.2.1
32
+ version: 1.2.7
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: 0.2.1
39
+ version: 1.2.7
40
40
  email:
41
41
  - phothinmg@disroot.org
42
42
  executables: []
@@ -45,8 +45,7 @@ extra_rdoc_files: []
45
45
  files:
46
46
  - LICENSE.txt
47
47
  - README.md
48
- - assets/index.js
49
- - assets/index.js.map
48
+ - lib/color.rb
50
49
  - lib/jekyll-minify-js.rb
51
50
  - lib/version.rb
52
51
  homepage: https://github.com/phothinmg/jekyll-minify-js