@divriots/jampack 0.12.1 → 0.13.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/dist/cache.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
- export type Category = 'img' | 'img-ext';
2
+ declare const listOfCategories: readonly ["img", "img-ext"];
3
+ export type Category = (typeof listOfCategories)[number];
3
4
  export type CacheData = {
4
5
  buffer: Buffer;
5
6
  meta: any;
package/dist/cache.js CHANGED
@@ -1,10 +1,13 @@
1
1
  import hasha from 'hasha';
2
2
  import path from 'path';
3
3
  import * as fs from 'fs/promises';
4
- import { VERSION } from './version.js';
5
4
  import $state from './state.js';
5
+ const listOfCategories = ['img', 'img-ext'];
6
6
  const CACHE_FOLDER = '.jampack/cache';
7
- const CACHE_FOLDER_VERSION = CACHE_FOLDER + '/' + VERSION;
7
+ const CACHE_VERSIONS = {
8
+ img: 'v1',
9
+ 'img-ext': 'v1',
10
+ };
8
11
  async function cleanCache(full) {
9
12
  if (full) {
10
13
  try {
@@ -15,19 +18,36 @@ async function cleanCache(full) {
15
18
  }
16
19
  return;
17
20
  }
18
- // List versions in cache
19
- let files;
21
+ // Delete old cache category
22
+ let catFolders = [];
20
23
  try {
21
- files = await fs.readdir(CACHE_FOLDER);
24
+ catFolders = await fs.readdir(CACHE_FOLDER);
22
25
  }
23
26
  catch (e) {
24
- // Nothing to do probably no cache available
25
- return;
27
+ // No problem
28
+ }
29
+ for (const f of catFolders) {
30
+ // @ts-ignore
31
+ if (!listOfCategories.includes(f))
32
+ fs.rm(path.join(CACHE_FOLDER, f), { recursive: true });
33
+ }
34
+ // Loop cache folders
35
+ for (const cat of listOfCategories) {
36
+ const location = path.join(CACHE_FOLDER, cat);
37
+ // List versions in cache
38
+ let folders;
39
+ try {
40
+ folders = await fs.readdir(location);
41
+ }
42
+ catch (e) {
43
+ continue;
44
+ }
45
+ // Delete old cache versions
46
+ for (const f of folders) {
47
+ if (f !== CACHE_VERSIONS[cat])
48
+ fs.rm(path.join(location, f), { recursive: true });
49
+ }
26
50
  }
27
- // Delete old cache versions
28
- await files
29
- .filter((f) => f !== VERSION)
30
- .forEach(async (f) => fs.rm(path.join(CACHE_FOLDER, f), { recursive: true }));
31
51
  }
32
52
  function computeCacheHash(buffer, options) {
33
53
  if ($state.args.nocache) {
@@ -39,8 +59,11 @@ function computeCacheHash(buffer, options) {
39
59
  }
40
60
  return hash;
41
61
  }
62
+ function getVersionOfCategory(category) {
63
+ return CACHE_VERSIONS[category];
64
+ }
42
65
  function getLocation(hash, category) {
43
- return path.join(CACHE_FOLDER_VERSION, category, hash);
66
+ return path.join(CACHE_FOLDER, category, getVersionOfCategory(category), hash);
44
67
  }
45
68
  async function getFromCache(category, hash) {
46
69
  if ($state.args.nocache) {
package/dist/index.js CHANGED
@@ -11,8 +11,6 @@ import { exit } from 'process';
11
11
  import kleur from 'kleur';
12
12
  import { cleanCache } from './cache.js';
13
13
  import { VERSION } from './version.js';
14
- import { mkdirSync } from 'fs';
15
- import { join } from 'path';
16
14
  const logo = ` __ __
17
15
  |__|____ _____ ___________ ____ | | __
18
16
  | \\__ \\ / \\\\____ \\__ \\ _/ ___\\| |/ /
@@ -55,15 +53,7 @@ program
55
53
  fast();
56
54
  }
57
55
  // Clean cache
58
- cleanCache(options.cleancache);
59
- // Create _jampack folder
60
- try {
61
- mkdirSync(join($state.dir, '_jampack'));
62
- }
63
- catch (e) {
64
- console.error(e.message);
65
- exit(1);
66
- }
56
+ await cleanCache(options.cleancache);
67
57
  if (!options.onlycomp) {
68
58
  printTitle('PASS 1 - Optimizing');
69
59
  console.time('Done');
package/dist/optimize.js CHANGED
@@ -194,7 +194,8 @@ async function processImage(htmlfile, $, imgElement, isAboveTheFold) {
194
194
  return;
195
195
  case 'download': // Download external image for local processing
196
196
  try {
197
- attrib_src = await downloadExternalImage(attrib_src);
197
+ const htmlFolder = path.dirname(path.resolve(path.join($state.dir, htmlfile)));
198
+ attrib_src = await downloadExternalImage(attrib_src, htmlFolder);
198
199
  img.attr('src', attrib_src);
199
200
  }
200
201
  catch (e) {
@@ -1 +1,2 @@
1
- export declare function downloadExternalImage(href: string): Promise<string>;
1
+ import '../utils/polyfill-fetch.js';
2
+ export declare function downloadExternalImage(href: string, dir: string): Promise<string>;
@@ -4,8 +4,8 @@ import hasha from 'hasha';
4
4
  import { fileTypeFromBuffer } from 'file-type';
5
5
  import { addToCache, getFromCache } from '../cache.js';
6
6
  import { parse } from '../utils/cache-control-parser.js';
7
- import $state from '../state.js';
8
- export async function downloadExternalImage(href) {
7
+ import '../utils/polyfill-fetch.js';
8
+ export async function downloadExternalImage(href, dir) {
9
9
  var _a, _b, _c, _d, _e;
10
10
  const hash = hasha(href, {
11
11
  algorithm: 'md5',
@@ -93,7 +93,13 @@ export async function downloadExternalImage(href) {
93
93
  // Construct local filename
94
94
  if (!ext)
95
95
  throw new Error('Unknown image format');
96
- const filename = `/_jampack/${hash}.${ext}`;
97
- fs.writeFile(path.join($state.dir, filename), buffer);
96
+ const filename = `./_jampack/${hash}.${ext}`;
97
+ try {
98
+ await fs.mkdir(path.join(dir, '_jampack'));
99
+ }
100
+ catch (e) {
101
+ // Nothing to do - folder is probably already present
102
+ }
103
+ fs.writeFile(path.join(dir, filename), buffer);
98
104
  return filename;
99
105
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import { fetch } from 'undici';
2
+ if (!Object.keys(global).includes('fetch')) {
3
+ Object.defineProperty(global, 'fetch', { value: fetch });
4
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@divriots/jampack",
3
- "version": "0.12.1",
3
+ "version": "0.13.0",
4
4
  "description": "Packer for static websites",
5
5
  "author": "Georges Gomes @georges-gomes",
6
6
  "bin": {
@@ -31,7 +31,8 @@
31
31
  "ora": "^6.3.0",
32
32
  "sharp": "^0.32.1",
33
33
  "svgo": "^3.0.2",
34
- "table": "^6.8.1"
34
+ "table": "^6.8.1",
35
+ "undici": "^5.22.1"
35
36
  },
36
37
  "devDependencies": {
37
38
  "@types/html-minifier-terser": "^7.0.0",