image-guard 5.1.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.
- package/.editorconfig +12 -0
- package/.github/FUNDING.yml +3 -0
- package/.github/dependabot.yml +12 -0
- package/.github/workflows/npm.yml +21 -0
- package/.github/workflows/tests.yml +54 -0
- package/LICENSE +201 -0
- package/NOTICE +6 -0
- package/README.md +177 -0
- package/SECURITY.md +9 -0
- package/bin/imagemin-guard.js +10 -0
- package/bin/imagemin-guard.test.js +392 -0
- package/eslint.config.js +26 -0
- package/media/output.png +0 -0
- package/package.json +46 -0
- package/src/index.js +195 -0
- package/src/utils.js +200 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/dependabot-options-reference
|
|
2
|
+
|
|
3
|
+
version: 2
|
|
4
|
+
updates:
|
|
5
|
+
- package-ecosystem: "npm"
|
|
6
|
+
directory: "/"
|
|
7
|
+
schedule:
|
|
8
|
+
interval: "monthly"
|
|
9
|
+
- package-ecosystem: "github-actions"
|
|
10
|
+
directory: "/"
|
|
11
|
+
schedule:
|
|
12
|
+
interval: "monthly"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
name: "npm Publishing"
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
|
|
6
|
+
jobs:
|
|
7
|
+
publish:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
id-token: write
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v6
|
|
14
|
+
- uses: actions/setup-node@v6
|
|
15
|
+
with:
|
|
16
|
+
node-version: 'lts/*'
|
|
17
|
+
registry-url: 'https://registry.npmjs.org'
|
|
18
|
+
- run: npm ci
|
|
19
|
+
- run: npm publish --provenance
|
|
20
|
+
env:
|
|
21
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
name: "Tests"
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
concurrency:
|
|
12
|
+
group: ${{ github.workflow }}-${{ github.ref }}
|
|
13
|
+
cancel-in-progress: true
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
test:
|
|
17
|
+
strategy:
|
|
18
|
+
matrix:
|
|
19
|
+
platform: [ubuntu-latest]
|
|
20
|
+
node: ['lts/*', 'current']
|
|
21
|
+
name: "Node ${{ matrix.node }} (${{ matrix.platform }})"
|
|
22
|
+
runs-on: ${{ matrix.platform }}
|
|
23
|
+
steps:
|
|
24
|
+
- name: "Check out repository"
|
|
25
|
+
uses: actions/checkout@v6
|
|
26
|
+
- name: "Set up Node.js"
|
|
27
|
+
uses: actions/setup-node@v6
|
|
28
|
+
with:
|
|
29
|
+
node-version: ${{ matrix.node }}
|
|
30
|
+
cache: npm
|
|
31
|
+
- name: "Install dependencies"
|
|
32
|
+
run: npm ci
|
|
33
|
+
- name: "Run tests"
|
|
34
|
+
run: npm test
|
|
35
|
+
|
|
36
|
+
test-macos:
|
|
37
|
+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
38
|
+
strategy:
|
|
39
|
+
matrix:
|
|
40
|
+
node: ['lts/*', 'current']
|
|
41
|
+
name: "Node ${{ matrix.node }} (macOS)"
|
|
42
|
+
runs-on: macos-latest
|
|
43
|
+
steps:
|
|
44
|
+
- name: "Check out repository"
|
|
45
|
+
uses: actions/checkout@v6
|
|
46
|
+
- name: "Set up Node.js"
|
|
47
|
+
uses: actions/setup-node@v6
|
|
48
|
+
with:
|
|
49
|
+
node-version: ${{ matrix.node }}
|
|
50
|
+
cache: npm
|
|
51
|
+
- name: "Install dependencies"
|
|
52
|
+
run: npm ci
|
|
53
|
+
- name: "Run tests"
|
|
54
|
+
run: npm test
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
Image Guard
|
|
2
|
+
Copyright 2022 Jens Oliver Meiert (https://meiert.com/)
|
|
3
|
+
|
|
4
|
+
The initial developer of imagemin is the imagemin team (https://github.com/orgs/imagemin/people).
|
|
5
|
+
|
|
6
|
+
The initial developer of imagemin-merlin (aka Merlin) is sum.cumo (https://web.archive.org/web/20191208211414/https://www.sumcumo.com/). Copyright 2019 sum.cumo GmbH.
|
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# Image Guard
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/image-guard) [](https://github.com/j9t/image-guard/actions) [](https://socket.dev/npm/package/image-guard)
|
|
4
|
+
|
|
5
|
+
(This project was based on [sum.cumo’s imagemin-merlin](https://github.com/sumcumo/imagemin-merlin). [Changes are documented](https://github.com/sumcumo/imagemin-merlin/compare/master...j9t:master), and include this README. Image Guard supports two additional file formats—WebP and AVIF—, comes with improved code and documentation, and is being maintained. For this reason, it’s [not based on any Imagemin packages](https://meiert.com/blog/imagemin-guard-4/) anymore.)
|
|
6
|
+
|
|
7
|
+
Image Guard takes care of near-lossless compression of your images, to help you avoid bloat in your repositories. It makes it convenient and as safe as possible to automatically compress PNG, JPG, GIF, WebP, and AVIF images.
|
|
8
|
+
|
|
9
|
+
It’s convenient because setup is simple. Run it right away—done. Or install, run, add hook—done.
|
|
10
|
+
|
|
11
|
+
It’s as safe as possible because compression happens losslessly (near-lossless for JPG and GIF images). That allows you to stop worrying about forgetting to compress images, but also about sacrificing too much quality. (You can take care of additional optimizations manually or through other tooling.)
|
|
12
|
+
|
|
13
|
+
## Installation and Use
|
|
14
|
+
|
|
15
|
+
(Note available parameters below.)
|
|
16
|
+
|
|
17
|
+
### Ways to Use Image Guard
|
|
18
|
+
|
|
19
|
+
#### Option 1: Immediate Manual Use
|
|
20
|
+
|
|
21
|
+
You can use Image Guard right away, without installation, by running
|
|
22
|
+
|
|
23
|
+
```console
|
|
24
|
+
npx image-guard
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
#### Option 2: Project-Linked Manual Use
|
|
28
|
+
|
|
29
|
+
Install Image Guard in your project:
|
|
30
|
+
|
|
31
|
+
```console
|
|
32
|
+
npm i -D image-guard
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Run Image Guard by calling
|
|
36
|
+
|
|
37
|
+
```console
|
|
38
|
+
npx imagemin-guard
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
To make sure that _all_ images are being compressed, it’s recommended to run Image Guard like this at least once, after installation.
|
|
42
|
+
|
|
43
|
+
#### Option 3: Automated Use
|
|
44
|
+
|
|
45
|
+
Install Image Guard in your project:
|
|
46
|
+
|
|
47
|
+
```console
|
|
48
|
+
npm i -D image-guard
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
To compress images already in the code base, run Image Guard once by calling
|
|
52
|
+
|
|
53
|
+
```console
|
|
54
|
+
npx imagemin-guard
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
For automated use, Image Guard should be triggered through a [Git hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) on `pre-commit`. You can choose between native Git hooks (recommended for simple projects) or [Husky](https://github.com/typicode/husky).
|
|
58
|
+
|
|
59
|
+
##### Native Git Hooks
|
|
60
|
+
|
|
61
|
+
Native Git hooks are simpler to set up and don’t require additional dependencies. Run these commands from your project root:
|
|
62
|
+
|
|
63
|
+
```console
|
|
64
|
+
mkdir -p .githooks;\
|
|
65
|
+
cat > .githooks/pre-commit << 'EOF'
|
|
66
|
+
#!/bin/sh
|
|
67
|
+
npx imagemin-guard --staged
|
|
68
|
+
EOF
|
|
69
|
+
chmod +x .githooks/pre-commit;\
|
|
70
|
+
git config core.hooksPath .githooks;\
|
|
71
|
+
git add .githooks/pre-commit;\
|
|
72
|
+
git commit -m "feat: add Git pre-commit hook for Image Guard";\
|
|
73
|
+
npm pkg set scripts.postprepare="mkdir -p .githooks && cat > .githooks/pre-commit << 'EOF'
|
|
74
|
+
#\!/bin/sh
|
|
75
|
+
npx imagemin-guard --staged
|
|
76
|
+
EOF
|
|
77
|
+
chmod +x .githooks/pre-commit && git config core.hooksPath .githooks"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
##### Husky
|
|
81
|
+
|
|
82
|
+
If you already use [Husky](https://typicode.github.io/husky/), run the following commands in your project root (you can copy and execute them at once):
|
|
83
|
+
|
|
84
|
+
```console
|
|
85
|
+
grep -qxF "npx imagemin-guard --staged" .husky/pre-commit || echo "\nnpx imagemin-guard --staged" >> .husky/pre-commit;\
|
|
86
|
+
git add .husky/pre-commit;\
|
|
87
|
+
git commit -m "feat: add Husky pre-commit hook for Image Guard";\
|
|
88
|
+
npm pkg set scripts.postprepare="grep -qxF 'npx imagemin-guard --staged' .husky/pre-commit || echo '\nnpx imagemin-guard --staged' >> .husky/pre-commit"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
If you don’t use Husky yet, run the following commands from your project root:
|
|
92
|
+
|
|
93
|
+
```console
|
|
94
|
+
npm i -D husky;\
|
|
95
|
+
npx husky init;\
|
|
96
|
+
echo "npx imagemin-guard --staged" > .husky/pre-commit;\
|
|
97
|
+
git add .husky/pre-commit;\
|
|
98
|
+
git commit -m "feat: add Husky pre-commit hook for Image Guard";\
|
|
99
|
+
npm pkg set scripts.postprepare="grep -qxF 'npx imagemin-guard --staged' .husky/pre-commit || echo '\nnpx imagemin-guard --staged' >> .husky/pre-commit"
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
(The `postprepare` script ensures that the hook is added to the repository whenever someone installs the package.)
|
|
103
|
+
|
|
104
|
+
**Important:** When you commit images that have not yet been compressed, the automated compression process (triggered by the pre-commit hook) will modify those image files to reduce their size. As a result, after your initial commit attempt, you will see these images appear as changed files in Git. To include the optimized images in your repository, you need to stage and commit them again. In rare cases, if further compression is possible, you may need to repeat this process until no further changes are detected. This workflow is intentional and ensures that only optimally compressed images are committed. Many editors can display diffs for images, helping you review these changes.
|
|
105
|
+
|
|
106
|
+
### Parameters
|
|
107
|
+
|
|
108
|
+
* `--dry` allows you to run Image Guard in “dry mode.” All changes are shown in the terminal.
|
|
109
|
+
|
|
110
|
+
* `--ignore` allows you to specify paths to be ignored (as in `--ignore=example,test`). Multiple paths must be separated by commas. The option supports glob patterns (e.g., `assets/**`, `**/*.png`); matching is case‑insensitive and honors `.gitignore`.
|
|
111
|
+
|
|
112
|
+
* `--staged` (recommended with automated use) triggers a mode that watches PNG, JPG, GIF, WebP, and AVIF files in `git diff` and only compresses those files—that approach makes Image Guard more efficient in operation.
|
|
113
|
+
|
|
114
|
+
* `--quiet` suppresses per‑file logs and prints only the final summary (plus errors). This reduces console noise and speeds up runs in CI and Git hooks.
|
|
115
|
+
|
|
116
|
+
### Troubleshooting
|
|
117
|
+
|
|
118
|
+
#### “npx: command not found”
|
|
119
|
+
|
|
120
|
+
If Git hooks fail with “npx: command not found,” make sure to install (`npm i -D image-guard`) and to refer to the binary directly in the `pre-commit` hook (and, not detailed here, also in the `postprepare` script):
|
|
121
|
+
|
|
122
|
+
```console
|
|
123
|
+
#!/bin/sh
|
|
124
|
+
export PATH="$PWD/node_modules/.bin:$PATH"
|
|
125
|
+
./node_modules/.bin/imagemin-guard --staged
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
This issue can arise in GUI Git clients (VS Code, GitHub Desktop, etc.) or with Node version managers, as these environments may not inherit your shell's `PATH`/Node environment. This affects any tool using npx in hooks.
|
|
129
|
+
|
|
130
|
+
## What Does the Output Look Like?
|
|
131
|
+
|
|
132
|
+
Roughly like this:
|
|
133
|
+
|
|
134
|
+

|
|
135
|
+
|
|
136
|
+
* Green: The image file has been compressed.
|
|
137
|
+
* White (light gray): The image file has not been changed.
|
|
138
|
+
* Blue: The image file had already been compressed more aggressively than the new result, and was therefore skipped, too.
|
|
139
|
+
|
|
140
|
+
Tip: Use `--quiet` to suppress these per‑file lines and keep only the final summary.
|
|
141
|
+
|
|
142
|
+
## How Does Image Guard Work?
|
|
143
|
+
|
|
144
|
+
Image Guard is a Node script that uses [sharp](https://www.npmjs.com/package/sharp) under the hood.
|
|
145
|
+
|
|
146
|
+
Automated compression works by monitoring whether a given [change list](https://webglossary.info/terms/change-list/) includes any PNGs, JPGs, GIFs, WebPs, or AVIFs. It’s initiated by a Git hook. Only those images are compressed where there is an improvement. The compressed images can then be committed to the underlying repository.
|
|
147
|
+
|
|
148
|
+
Through this approach, though glossed over here, Image Guard makes up for what’s missing or complicated in other packages, namely easy, near-riskless, automatable, resource-friendly in-repo optimization.
|
|
149
|
+
|
|
150
|
+
## Why Use Image Guard?
|
|
151
|
+
|
|
152
|
+
You use Image Guard when you need a simple, automatable, robust solution to compress images in a way that limits unnecessary image payload right from the start, in your repositories, and that reduces the risk that entirely uncompressed images go into production.
|
|
153
|
+
|
|
154
|
+
As Image Guard compresses near-losslessly, there’s little risk of quality issues from compression. (Lossless compression is not possible for every image format, however, so there’s a risk when excessively iterating over the same images. Doing so may eventually degrade quality.)
|
|
155
|
+
|
|
156
|
+
## What Does Image Guard _Not_ Do?
|
|
157
|
+
|
|
158
|
+
Image Guard is no substitute for image fine-tuning and micro-optimization. That’s difficult to do in an automated fashion, because this type of compression requires [balancing quality and performance](https://meiert.com/blog/understanding-image-compression/) and is context-dependent. In its most extreme form, when maximum quality at maximum performance is required from each graphic, micro-optimization is even challenging to do manually.
|
|
159
|
+
|
|
160
|
+
That is, micro-optimization still needs to be taken care of through other means, whether manually or through tools. Image Guard just solves the problem that images are checked in or go live that are not compressed _at all_.
|
|
161
|
+
|
|
162
|
+
## What’s Next?
|
|
163
|
+
|
|
164
|
+
There are a few ideas, like adding light SVG support, or ensuring compatibility with projects in which the project’s .git folder is not at the same level as its package.json (currently, automatic mode doesn’t work in these cases).
|
|
165
|
+
|
|
166
|
+
Feedback is appreciated: Please [file an issue](https://github.com/j9t/image-guard/issues/new) or send a pull request. Thank you!
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
Copyright 2019 [sum.cumo GmbH](https://web.archive.org/web/20191208211414/https://www.sumcumo.com/)
|
|
171
|
+
Copyright 2022 [Jens Oliver Meiert](https://meiert.com/)
|
|
172
|
+
|
|
173
|
+
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
|
174
|
+
|
|
175
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
176
|
+
|
|
177
|
+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Supported Versions
|
|
4
|
+
|
|
5
|
+
Only the latest and therefore current version of Image Guard is supported. It’s advised to update older versions to the latest version.
|
|
6
|
+
|
|
7
|
+
## Reporting a Vulnerability
|
|
8
|
+
|
|
9
|
+
To report a vulnerability, please [file an issue](https://github.com/j9t/image-guard/issues/new) or email info@meiert.com.
|