jekyll-terser 1.0.0

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/lib/jekyll-terser.rb +82 -0
  4. metadata +69 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a9c7a205f5dfd49092807ba450bd93968f6283755a422f78e5a4d3d8e7ad1c9d
4
+ data.tar.gz: c79031c8e5adf569821bf3ecdb17f1190c79e4d021cc2bc2119a84240b2a2864
5
+ SHA512:
6
+ metadata.gz: ca2830e836e66fb99922520c018db92bbd7f5ab228458ebb743ce15156fbddf298ffd0e445a0566668482270146c084b3e386a2fa2f63df50de7d5a179e43a2c
7
+ data.tar.gz: d6a21cf9fdb0f4924d41ca7035a465ca6f3c2e93c184f1339feed20a3226024568d0c3a36c40cf7e95fd3bbdd993d2dcf50735f9caf2837c438a878c0e717ceb
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Strappazzon
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,82 @@
1
+ require 'jekyll'
2
+ require 'terser'
3
+
4
+ module Jekyll
5
+ module Terser
6
+ class ModTime
7
+ @mtimes = {}
8
+
9
+ class << self
10
+ attr_reader :mtimes
11
+ end
12
+
13
+ class << self
14
+ attr_writer :mtimes
15
+ end
16
+ end
17
+
18
+ class JSFile < Jekyll::StaticFile
19
+ def initialize(site, base, dir, name, terser = nil)
20
+ super(site, base, dir, name)
21
+
22
+ @site = site
23
+ @base = base
24
+ @dir = dir
25
+ @name = name
26
+ options = site.config.dig('jekyll-terser', 'terser_opts') || {} if terser.nil?
27
+ @terser = terser || ::Terser.new(options.transform_keys(&:to_sym))
28
+ end
29
+
30
+ def destination(dest)
31
+ File.join(dest, @dir, @name)
32
+ end
33
+
34
+ def write(dest)
35
+ dest_path = destination(dest)
36
+
37
+ return false if File.exist?(dest_path) && !modified?
38
+
39
+ ModTime.mtimes[path] = mtime
40
+
41
+ FileUtils.mkdir_p(File.dirname(dest_path))
42
+
43
+ begin
44
+ content = File.read(path)
45
+ content = @terser.compile(content)
46
+
47
+ File.write(dest_path, content)
48
+ rescue StandardError => e
49
+ Jekyll.logger.warn 'Terser:', e.message.to_s
50
+ end
51
+
52
+ return true
53
+ end
54
+ end
55
+
56
+ class TerserGenerator < Jekyll::Generator
57
+ def initialize(config = {})
58
+ super
59
+
60
+ @options = config.dig('jekyll-terser', 'terser_opts') || {}
61
+ @terser = ::Terser.new(@options.transform_keys(&:to_sym))
62
+ end
63
+
64
+ def generate(site)
65
+ return unless !site.config.dig('jekyll-terser', 'only_production') || Jekyll.env == 'production'
66
+
67
+ site.static_files.clone.each do |f|
68
+ next unless f.is_a?(Jekyll::StaticFile) && f.path.match?(/\.js$/) && !f.path.end_with?('.min.js')
69
+
70
+ Jekyll.logger.info 'Terser:', "Minifying #{f.path}"
71
+ site.static_files.delete(f)
72
+
73
+ name = File.basename(f.path)
74
+ destination = File.dirname(f.path).sub(site.source, '')
75
+ js_file = JSFile.new(site, site.source, destination, name, @terser)
76
+
77
+ site.static_files << js_file
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-terser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alberto Strappazzon
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-05-17 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: jekyll
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '4.4'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '4.4'
26
+ - !ruby/object:Gem::Dependency
27
+ name: terser
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.2'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.2'
40
+ description: 'Jekyll support for Terser: compress and mangle JS files.'
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - LICENSE.txt
46
+ - lib/jekyll-terser.rb
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ source_code_uri: https://github.com/Strappazzon/jekyll-terser
51
+ changelog_uri: https://github.com/Strappazzon/jekyll-terser/blob/master/CHANGELOG.md
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 3.4.2
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.6.2
67
+ specification_version: 4
68
+ summary: Jekyll support for Terser.
69
+ test_files: []