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 +4 -4
- data/README.md +3 -4
- data/lib/color.rb +5 -0
- data/lib/jekyll-minify-js.rb +70 -101
- data/lib/version.rb +1 -1
- metadata +5 -6
- data/assets/index.js +0 -50
- data/assets/index.js.map +0 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b44a42e35257584ee45662ee7739a91e85cde0769071ed2373091278955dfa18
|
|
4
|
+
data.tar.gz: f84f80cc36b6eba9160a5424aef9f1bb77cd74b463d4ec702f83c0575d4f10e8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dceea9e11499497a69af5bc29031006c651a4428b83de86634822d2f4e91836d1738595c5cc9ad14881e33d15b77b8b9b50088226c44f12b99c7ea59f875e3d0
|
|
7
|
+
data.tar.gz: 303a138bf705689bbde1b7da5c875a2c802be8a52dd32c230b23866cf63eba6eb7fc726811cba23fafbef92d3edcd98838e0e778822468cd702f9bc38fa9dc60
|
data/README.md
CHANGED
data/lib/color.rb
ADDED
data/lib/jekyll-minify-js.rb
CHANGED
|
@@ -1,131 +1,100 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "logger"
|
|
4
|
+
|
|
3
5
|
require "jekyll"
|
|
4
|
-
require "
|
|
5
|
-
require "
|
|
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
|
-
#
|
|
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
|
-
#
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
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
|
-
|
|
85
|
-
|
|
39
|
+
entry_dir = config["entry_dir"] || "js"
|
|
40
|
+
out_dir = config["output_dir"] || "js"
|
|
86
41
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
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(
|
|
68
|
+
out = File.join(out_dir_full, rel)
|
|
117
69
|
map_name = "#{File.basename(out)}.map"
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
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.
|
|
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:
|
|
27
|
+
name: terser
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
29
29
|
requirements:
|
|
30
30
|
- - ">="
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version:
|
|
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:
|
|
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
|
-
-
|
|
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
|