astro-accelerator 0.0.72 → 0.0.74

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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.72",
2
+ "version": "0.0.74",
3
3
  "author": "Steve Fenton",
4
4
  "name": "astro-accelerator",
5
5
  "description": "A super-lightweight, accessible, SEO-friendly starter project for Astro",
@@ -24,15 +24,15 @@
24
24
  "dts": "tsc ./tests/locate-content.js ./tests/locate-navigation.js ./tests/locate-search.js --allowJs --declaration --emitDeclarationOnly"
25
25
  },
26
26
  "dependencies": {
27
- "@astrojs/mdx": "^0.17.0",
28
- "@squoosh/lib": "^0.4.0",
29
- "astro": "^2.0.13",
27
+ "@astrojs/mdx": "^0.18.2",
28
+ "astro": "^2.1.7",
30
29
  "astro-accelerator-utils": "^0.2.23",
31
30
  "hast-util-from-selector": "^2.0.1",
32
- "remark-directive": "^2.0.1"
31
+ "remark-directive": "^2.0.1",
32
+ "sharp": "^0.31.3"
33
33
  },
34
34
  "devDependencies": {
35
- "@playwright/test": "^1.30.0"
35
+ "@playwright/test": "^1.31.2"
36
36
  },
37
37
  "engines": {
38
38
  "node": "*"
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
- import { ImagePool } from '@squoosh/lib';
3
+ import sharp from 'sharp';
4
4
 
5
5
  const workingDirectory = process.cwd();
6
6
 
@@ -79,13 +79,6 @@ await recurseFiles('');
79
79
 
80
80
  console.log(`Found ${filesToProcess.length} files to process`);
81
81
 
82
- async function processImage(imagePool, src, options) {
83
- const file = await fs.promises.readFile(src);
84
- const image = imagePool.ingestImage(file);
85
- await image.encode(options);
86
- return image;
87
- }
88
-
89
82
  for (const file of filesToProcess) {
90
83
  console.log(file.path);
91
84
  const source = path.join(imageDirectory, file.path);
@@ -94,71 +87,55 @@ for (const file of filesToProcess) {
94
87
 
95
88
  const ext = path.parse(source).ext;
96
89
 
97
- let image = null;
98
- let rawEncodedImage;
99
-
100
- // Create optimised fallback image
101
- const imagePool = new ImagePool(1);
102
90
  switch (ext) {
103
91
  case '.png':
104
- image = await processImage(imagePool, source, { oxipng: {} });
105
- rawEncodedImage = (await image.encodedWith.oxipng).binary;
106
- await fs.promises.writeFile(destination + '.png', rawEncodedImage);
92
+ sharp(source)
93
+ .png()
94
+ .toFile(destination + '.png');
107
95
  break;
108
96
  case '.jpg':
109
97
  case '.jpeg':
110
- image = await processImage(imagePool, source, { mozjpeg: {} });
111
- rawEncodedImage = (await image.encodedWith.mozjpeg).binary;
112
- await fs.promises.writeFile(destination + '.jpg', rawEncodedImage);
98
+ sharp(source)
99
+ .jpeg({ mozjpeg: true })
100
+ .toFile(destination + '.jpg');
113
101
  break;
114
102
  case '.webp':
115
- image = await processImage(imagePool, source, { webp: { quality: 85 } });
116
- rawEncodedImage = (await image.encodedWith.webp).binary;
117
- await fs.promises.writeFile(destination + '.webp', rawEncodedImage);
103
+ sharp(source)
104
+ .webp({ quality: 80 })
105
+ .toFile(destination + '.webp');
118
106
  break;
119
107
  }
120
108
 
121
- if (image) {
122
- const info = await image.decoded;
123
- const metadata = {
124
- width: info.bitmap.width,
125
- height: info.bitmap.height,
126
- sizeInBytes: info.size
127
- };
109
+ const info = await sharp(source).metadata();
128
110
 
129
- const metaFile = source + '.json';
130
- await fs.promises.writeFile(metaFile, JSON.stringify(metadata));
131
- }
111
+ const metadata = {
112
+ width: info.width,
113
+ height: info.height,
114
+ sizeInBytes: info.size
115
+ };
132
116
 
133
- await imagePool.close();
117
+ const metaFile = source + '.json';
118
+ await fs.promises.writeFile(metaFile, JSON.stringify(metadata));
134
119
 
135
120
  // Create resized images
136
121
  for (const key in size) {
137
- const imagePool = new ImagePool(1);
138
122
  const resizeDestination = getDestinationFilePathless(file.path, size[key]);
139
123
  await createDestinationFolder(resizeDestination);
140
124
 
141
- const imgFile = await fs.promises.readFile(source);
142
- const image = imagePool.ingestImage(imgFile);
125
+ const metadata = await sharp(source).metadata();
143
126
 
144
- const info = await image.decoded;
145
- if (info.bitmap.width > size[key]) {
127
+ if (metadata.width > size[key]) {
146
128
  // Only resize if the image is larger than the target size
147
- const preprocessOptions = {
148
- resize: {
149
- width: size[key]
150
- }
151
- };
152
-
153
- await image.preprocess(preprocessOptions);
129
+ sharp(source)
130
+ .resize(size[key], null)
131
+ .webp({ quality: 80 })
132
+ .toFile(resizeDestination + '.webp');
133
+ } else {
134
+ // Don't resize as it's smaller than target size
135
+ sharp(source)
136
+ .webp({ quality: 80 })
137
+ .toFile(resizeDestination + '.webp');
154
138
  }
155
-
156
- await image.encode({ webp: { quality: 90 } });
157
-
158
- rawEncodedImage = (await image.encodedWith.webp).binary;
159
- await fs.promises.writeFile(resizeDestination + '.webp', rawEncodedImage);
160
-
161
- await imagePool.close();
162
139
  }
163
140
  }
164
141