@divriots/jampack 0.9.3 → 0.10.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/README.md CHANGED
@@ -68,6 +68,7 @@ For more options see [CLI options](https://jampack.divriots.com/cli-options).
68
68
  - [bayjs.org](https://bayjs.org/)
69
69
  - [qwind.pages.dev](https://qwind.pages.dev/)
70
70
  - [Bloycey's Blog](https://bloycey.blog/)
71
+ - [codenanshu.in](https://codenanshu.in/)
71
72
 
72
73
  > Add yours here
73
74
 
@@ -3,7 +3,7 @@ import sharp from 'sharp';
3
3
  import { MimeType } from 'file-type';
4
4
  export type ImageMimeType = MimeType | 'image/svg+xml';
5
5
  export declare const AllImageFormat: string[];
6
- export type ImageFormat = typeof AllImageFormat[number] | undefined;
6
+ export type ImageFormat = (typeof AllImageFormat)[number] | undefined;
7
7
  export type Image = {
8
8
  format: ImageFormat;
9
9
  data: Buffer;
@@ -30,24 +30,26 @@ export async function compressImage(data, options) {
30
30
  let outputFormat;
31
31
  // Special case for svg
32
32
  if (meta.format === 'svg') {
33
- const newData = svgo(data, {
34
- multipass: true,
35
- plugins: [
36
- {
37
- name: 'preset-default',
38
- params: {
39
- overrides: {
40
- removeViewBox: false,
33
+ try {
34
+ const output = svgo(data.toString(), {
35
+ multipass: true,
36
+ plugins: [
37
+ {
38
+ name: 'preset-default',
39
+ params: {
40
+ overrides: {
41
+ removeViewBox: false,
42
+ },
41
43
  },
42
44
  },
43
- },
44
- ],
45
- });
46
- if (newData.error || newData.modernError) {
47
- console.log(`Error processing svg ${data}`);
45
+ ],
46
+ });
47
+ return { format: 'svg', data: Buffer.from(output.data, 'utf8') };
48
+ }
49
+ catch (e) {
50
+ // In case of any issue with svg compression:
48
51
  return undefined;
49
52
  }
50
- return { format: 'svg', data: Buffer.from(newData.data, 'utf8') };
51
53
  }
52
54
  // TODO
53
55
  // Use information of input image to the destination format
@@ -0,0 +1,3 @@
1
+ import { Options } from './config-types.js';
2
+ declare const default_options: Options;
3
+ export default default_options;
@@ -0,0 +1,31 @@
1
+ const default_options = {
2
+ image: {
3
+ embed_size: 1500,
4
+ srcset_min_width: 640,
5
+ compress: true,
6
+ jpeg: {
7
+ options: {
8
+ quality: 75,
9
+ mozjpeg: true,
10
+ },
11
+ },
12
+ png: {
13
+ options: {
14
+ compressionLevel: 9,
15
+ },
16
+ },
17
+ webp: {
18
+ options_lossless: {
19
+ effort: 4,
20
+ quality: 77,
21
+ mode: 'lossless',
22
+ },
23
+ options_lossly: {
24
+ effort: 4,
25
+ quality: 77,
26
+ mode: 'lossly',
27
+ },
28
+ },
29
+ },
30
+ };
31
+ export default default_options;
@@ -0,0 +1,2 @@
1
+ declare const fast_options_override: any;
2
+ export default fast_options_override;
@@ -0,0 +1,26 @@
1
+ const fast_options_override = {
2
+ image: {
3
+ embed_size: 0,
4
+ srcset_min_width: 16000,
5
+ compress: false,
6
+ jpeg: {
7
+ options: {
8
+ mozjpeg: false,
9
+ },
10
+ },
11
+ png: {
12
+ options: {
13
+ compressionLevel: 0,
14
+ },
15
+ },
16
+ webp: {
17
+ options_lossless: {
18
+ effort: 0,
19
+ },
20
+ options_lossly: {
21
+ effort: 0,
22
+ },
23
+ },
24
+ },
25
+ };
26
+ export default fast_options_override;
@@ -0,0 +1,27 @@
1
+ export type WebpOptions = {
2
+ effort: number;
3
+ mode: 'lossless' | 'lossly';
4
+ quality: number;
5
+ };
6
+ export type Options = {
7
+ image: {
8
+ embed_size: number;
9
+ srcset_min_width: number;
10
+ compress: boolean;
11
+ jpeg: {
12
+ options: {
13
+ quality: number;
14
+ mozjpeg: boolean;
15
+ };
16
+ };
17
+ png: {
18
+ options: {
19
+ compressionLevel: number;
20
+ };
21
+ };
22
+ webp: {
23
+ options_lossless: WebpOptions;
24
+ options_lossly: WebpOptions;
25
+ };
26
+ };
27
+ };
@@ -0,0 +1 @@
1
+ export {};
package/dist/config.d.ts CHANGED
@@ -1,30 +1,4 @@
1
- export type WebpOptions = {
2
- effort: number;
3
- mode: 'lossless' | 'lossly';
4
- quality: number;
5
- };
6
- export type Options = {
7
- image: {
8
- embed_size: number;
9
- srcset_min_width: number;
10
- compress: boolean;
11
- jpeg: {
12
- options: {
13
- quality: number;
14
- mozjpeg: boolean;
15
- };
16
- };
17
- png: {
18
- options: {
19
- compressionLevel: number;
20
- };
21
- };
22
- webp: {
23
- options_lossless: WebpOptions;
24
- options_lossly: WebpOptions;
25
- };
26
- };
27
- };
28
- declare const default_options: Options;
1
+ declare const options: import("./config-types.js").Options;
29
2
  export declare function fast(): void;
30
- export default default_options;
3
+ export declare function loadConfig(): Promise<void>;
4
+ export default options;
package/dist/config.js CHANGED
@@ -1,59 +1,18 @@
1
- const default_options = {
2
- image: {
3
- embed_size: 1500,
4
- srcset_min_width: 640,
5
- compress: true,
6
- jpeg: {
7
- options: {
8
- quality: 75,
9
- mozjpeg: true,
10
- },
11
- },
12
- png: {
13
- options: {
14
- compressionLevel: 9,
15
- },
16
- },
17
- webp: {
18
- options_lossless: {
19
- effort: 4,
20
- quality: 77,
21
- mode: 'lossless',
22
- },
23
- options_lossly: {
24
- effort: 4,
25
- quality: 77,
26
- mode: 'lossly',
27
- },
28
- },
29
- },
30
- };
31
- const fast_options_override = {
32
- image: {
33
- embed_size: 0,
34
- srcset_min_width: 16000,
35
- compress: false,
36
- jpeg: {
37
- options: {
38
- mozjpeg: false,
39
- },
40
- },
41
- png: {
42
- options: {
43
- compressionLevel: 0,
44
- },
45
- },
46
- webp: {
47
- options_lossless: {
48
- effort: 0,
49
- },
50
- options_lossly: {
51
- effort: 0,
52
- },
53
- },
54
- },
55
- };
1
+ // @ts-ignore
2
+ import load from '@proload/core';
3
+ import deepmerge from 'deepmerge';
4
+ import default_options from './config-default.js';
5
+ import fast_options_override from './config-fast.js';
6
+ const options = default_options;
56
7
  export function fast() {
57
- Object.assign(default_options, fast_options_override);
8
+ deepmerge(options, fast_options_override);
58
9
  }
59
- export default default_options;
10
+ export async function loadConfig() {
11
+ const proload = await load('jampack', { mustExist: false });
12
+ if (proload) {
13
+ console.log('Overriding default config with:');
14
+ console.log(JSON.stringify(proload.value, null, 2));
15
+ deepmerge(options, proload.value);
16
+ }
17
+ }
18
+ export default options;
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ import { optimize } from './optimize.js';
5
5
  import $state from './state.js';
6
6
  import { table } from 'table';
7
7
  import { formatBytes } from './utils.js';
8
- import { fast } from './config.js';
8
+ import { fast, loadConfig } from './config.js';
9
9
  import { printTitle } from './logger.js';
10
10
  import { exit } from 'process';
11
11
  import kleur from 'kleur';
@@ -41,7 +41,9 @@ program
41
41
  .action(async (dir, options) => {
42
42
  $state.dir = dir;
43
43
  $state.args = options;
44
- // Override config with fast options
44
+ // Override default config with config file
45
+ await loadConfig();
46
+ // Override config with fast options if set
45
47
  if (options.fast) {
46
48
  fast();
47
49
  }
package/dist/optimize.js CHANGED
@@ -227,47 +227,39 @@ async function processImage(htmlfile, $, imgElement, isAboveTheFold) {
227
227
  ? 'unchanged'
228
228
  : 'webp';
229
229
  }
230
- if (!$state.optimizedFiles.has(originalImage.filePathAbsolute)) {
231
- // Let's avoid to optimize same images twice
232
- $state.optimizedFiles.add(originalImage.filePathAbsolute);
233
- newImage = await compressImage(await originalImage.getData(), {
234
- toFormat: srcToFormat,
235
- });
236
- if ((newImage === null || newImage === void 0 ? void 0 : newImage.data) &&
237
- (newImage.data.length < (await originalImage.getLen()) ||
238
- srcToFormat === 'jpeg+progressive') // Progressive override all even if worse
239
- ) {
240
- // Do we need to add an new extension?
241
- const newExtension = `.${newImage.format}`;
242
- const additionalExtension = path.extname(originalImage.filePathAbsolute) === newExtension
243
- ? ''
244
- : newExtension;
245
- const newFilename = originalImage.filePathAbsolute + additionalExtension;
246
- if (!$state.args.nowrite) {
247
- fs.writeFile(newFilename, newImage.data);
248
- }
249
- // Mark new file as optimized
250
- $state.compressedFiles.add(newFilename);
251
- // Report compression result
252
- $state.reportSummary({
253
- action: newFilename !== originalImage.filePathAbsolute
254
- ? `${await originalImage.getExt()}->${newImage.format}`
255
- : path.extname(originalImage.filePathAbsolute),
256
- originalSize: await originalImage.getLen(),
257
- compressedSize: newImage.data.length,
258
- });
259
- img.attr('src', attrib_src + additionalExtension);
260
- }
261
- else {
262
- // Drop new Image
263
- newImage = undefined;
264
- // Report non-compression
265
- $state.reportSummary({
266
- action: path.extname(originalImage.filePathAbsolute),
267
- originalSize: await originalImage.getLen(),
268
- compressedSize: await originalImage.getLen(),
269
- });
230
+ newImage = await compressImage(await originalImage.getData(), {
231
+ toFormat: srcToFormat,
232
+ });
233
+ if ((newImage === null || newImage === void 0 ? void 0 : newImage.data) &&
234
+ (newImage.data.length < (await originalImage.getLen()) ||
235
+ srcToFormat === 'jpeg+progressive') // Progressive override all even if worse
236
+ ) {
237
+ const additionalExtension = `.${newImage.format}`;
238
+ const newFilename = originalImage.filePathAbsolute + additionalExtension;
239
+ if (!$state.compressedFiles.has(newFilename) && !$state.args.nowrite) {
240
+ fs.writeFile(newFilename, newImage.data);
270
241
  }
242
+ // Mark new file as compressed
243
+ $state.compressedFiles.add(newFilename);
244
+ // Report compression result
245
+ $state.reportSummary({
246
+ action: newFilename !== originalImage.filePathAbsolute
247
+ ? `${await originalImage.getExt()}->${newImage.format}`
248
+ : path.extname(originalImage.filePathAbsolute),
249
+ originalSize: await originalImage.getLen(),
250
+ compressedSize: newImage.data.length,
251
+ });
252
+ img.attr('src', attrib_src + additionalExtension);
253
+ }
254
+ else {
255
+ // Drop new Image
256
+ newImage = undefined;
257
+ // Report non-compression
258
+ $state.reportSummary({
259
+ action: path.extname(originalImage.filePathAbsolute),
260
+ originalSize: await originalImage.getLen(),
261
+ compressedSize: await originalImage.getLen(),
262
+ });
271
263
  }
272
264
  /*
273
265
  * Embed small images
package/dist/state.d.ts CHANGED
@@ -21,7 +21,6 @@ export declare class GlobalState {
21
21
  dir: string;
22
22
  args: Args;
23
23
  compressedFiles: Set<string>;
24
- optimizedFiles: Set<string>;
25
24
  issues: Map<string, Issue[]>;
26
25
  summary: Summary;
27
26
  summaryByExtension: Record<string, Summary>;
package/dist/state.js CHANGED
@@ -3,7 +3,6 @@ export class GlobalState {
3
3
  this.dir = 'dist';
4
4
  this.args = {};
5
5
  this.compressedFiles = new Set();
6
- this.optimizedFiles = new Set();
7
6
  this.issues = new Map();
8
7
  this.summary = {
9
8
  nbFiles: 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@divriots/jampack",
3
- "version": "0.9.3",
3
+ "version": "0.10.0",
4
4
  "description": "Packer for static websites",
5
5
  "author": "Georges Gomes @georges-gomes",
6
6
  "bin": {
@@ -16,40 +16,39 @@
16
16
  "/dist"
17
17
  ],
18
18
  "dependencies": {
19
- "@swc/core": "^1.3.30",
19
+ "@proload/core": "^0.3.3",
20
+ "@swc/core": "^1.3.57",
20
21
  "@types/cheerio": "^0.22.31",
21
- "@types/commander": "^2.12.2",
22
22
  "@types/csso": "^5.0.0",
23
23
  "@types/html-minifier-terser": "^7.0.0",
24
- "@types/ora": "^3.2.0",
25
- "@types/sharp": "^0.31.1",
26
- "@types/svgo": "^2.6.4",
27
- "@types/table": "^6.3.2",
24
+ "@types/sharp": "^0.32.0",
25
+ "@types/svgo": "^3.0.0",
28
26
  "cheerio": "0.22.0",
29
- "commander": "^9.4.1",
27
+ "commander": "^10.0.1",
30
28
  "csso": "^5.0.5",
31
- "file-type": "^18.0.0",
32
- "globby": "^13.1.3",
29
+ "deepmerge": "^4.3.1",
30
+ "file-type": "^18.4.0",
31
+ "globby": "^13.1.4",
33
32
  "hasha": "^5.2.2",
34
- "html-minifier-terser": "^7.1.0",
33
+ "html-minifier-terser": "^7.2.0",
35
34
  "kleur": "^4.1.5",
36
- "lightningcss": "^1.18.0",
35
+ "lightningcss": "^1.20.0",
37
36
  "mini-svg-data-uri": "^1.4.4",
38
- "ora": "^6.1.2",
39
- "sharp": "^0.31.3",
40
- "svgo": "^2.8.0",
37
+ "ora": "^6.3.0",
38
+ "sharp": "^0.32.1",
39
+ "svgo": "^3.0.2",
41
40
  "table": "^6.8.1"
42
41
  },
43
42
  "devDependencies": {
44
- "@types/node": "^16.11.59",
43
+ "@types/node": "^16.18.28",
45
44
  "shx": "^0.3.3",
46
45
  "ts-node": "^10.2.1",
47
- "tslib": "^2.4.1",
48
- "typescript": "^4.9.4"
46
+ "tslib": "^2.5.0",
47
+ "typescript": "^5.0.4"
49
48
  },
50
49
  "scripts": {
51
50
  "watch": "pnpm build --watch",
52
- "try": "shx rm -fr demo && cp -R demo_wcd demo && node ./dist/index.js ./demo --nocache --onlyoptim",
51
+ "try": "shx rm -fr demo && cp -R demo_rf demo && node ./dist/index.js ./demo --nocache --onlyoptim",
53
52
  "build": "shx rm -rf dist && tsc -b",
54
53
  "lint": "eslint . --ext .ts --config .eslintrc"
55
54
  },