@ecopages/image-processor 0.2.0-beta.8 → 0.2.0-rc.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 +11 -3
- package/package.json +19 -5
- package/src/bun-plugins.js +48 -29
- package/src/component/html.js +12 -6
- package/src/component/jsx.d.ts +12 -0
- package/src/component/jsx.js +18 -0
- package/src/component/react.js +17 -10
- package/src/constants.js +5 -4
- package/src/image-plugins.js +48 -29
- package/src/image-processor.js +147 -135
- package/src/image-renderer.js +222 -220
- package/src/image-runtime-state.d.ts +20 -0
- package/src/image-runtime-state.js +59 -0
- package/src/image-utils.js +109 -98
- package/src/index.js +3 -3
- package/src/plugin.d.ts +17 -11
- package/src/plugin.js +292 -274
- package/src/types.js +1 -0
- package/src/utils.js +24 -25
package/src/plugin.js
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
const logger = new Logger("[@ecopages/image-processor]", {
|
|
18
|
-
debug: process.env.ECOPAGES_LOGGER_DEBUG === "true"
|
|
1
|
+
/**
|
|
2
|
+
* ImageProcessorPlugin
|
|
3
|
+
* @module @ecopages/image-processor
|
|
4
|
+
*/
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { fileSystem } from '@ecopages/file-system';
|
|
7
|
+
import { mergeProcessorOptions, Processor, resolveGeneratedPath, } from '@ecopages/core/plugins/processor';
|
|
8
|
+
import { isLitStaticRenderWorkerThread } from '@ecopages/core/build/lit-static-render-worker-context';
|
|
9
|
+
import { Logger } from '@ecopages/logger';
|
|
10
|
+
import { createImagePlugin, createImagePluginBundler } from './image-plugins.js';
|
|
11
|
+
import { ImageProcessor } from './image-processor.js';
|
|
12
|
+
import { getSourceImagePaths, loadProcessedImagesFromDisk } from './image-runtime-state.js';
|
|
13
|
+
import { anyCaseToCamelCase } from './utils.js';
|
|
14
|
+
const logger = new Logger('[@ecopages/image-processor]', {
|
|
15
|
+
debug: process.env.ECOPAGES_LOGGER_DEBUG === 'true',
|
|
19
16
|
});
|
|
20
17
|
const IMAGE_VIRTUAL_MODULE_TYPES = `/**
|
|
21
18
|
* ImageAttributes
|
|
@@ -62,230 +59,250 @@ type ImageSize = {
|
|
|
62
59
|
width: number;
|
|
63
60
|
label: string;
|
|
64
61
|
};`;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
get plugins() {
|
|
97
|
-
return [createImagePlugin(this.processedImages)];
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Replaces image-map contents without swapping the backing object.
|
|
101
|
-
*
|
|
102
|
-
* @remarks
|
|
103
|
-
* The build/runtime virtual-module plugins close over `processedImages`, so
|
|
104
|
-
* mutating the existing object keeps those plugins live after preparation.
|
|
105
|
-
*/
|
|
106
|
-
replaceProcessedImages(images) {
|
|
107
|
-
for (const key of Object.keys(this.processedImages)) {
|
|
108
|
-
delete this.processedImages[key];
|
|
109
|
-
}
|
|
110
|
-
Object.assign(this.processedImages, images);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Prepares the image virtual-module state before config build seals the app
|
|
114
|
-
* manifest.
|
|
115
|
-
*/
|
|
116
|
-
async prepareBuildContributions() {
|
|
117
|
-
if (this.buildContributionsPrepared) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
if (process.env.ECOPAGES_LIT_STATIC_RENDER_WORKER === "true") {
|
|
121
|
-
this.buildContributionsPrepared = true;
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
if (!this.context) {
|
|
125
|
-
throw new Error("ImageProcessor requires context to be set");
|
|
126
|
-
}
|
|
127
|
-
logger.debug("Setting up image processor", {
|
|
128
|
-
srcDir: this.context.srcDir,
|
|
129
|
-
distDir: this.context.distDir
|
|
130
|
-
});
|
|
131
|
-
const defaultConfig = {
|
|
132
|
-
sourceDir: `${this.context.srcDir}/public/assets/images`,
|
|
133
|
-
outputDir: `${this.context.distDir}/assets/optimized`,
|
|
134
|
-
publicPath: "/assets/optimized",
|
|
135
|
-
sizes: [],
|
|
136
|
-
quality: 80,
|
|
137
|
-
format: "webp"
|
|
138
|
-
};
|
|
139
|
-
const config = this.options ? mergeProcessorOptions(defaultConfig, this.options) : defaultConfig;
|
|
140
|
-
this.resolvedConfig = config;
|
|
141
|
-
this.processor = new ImageProcessor(config, {
|
|
142
|
-
readCache: (key) => this.readCache(key),
|
|
143
|
-
writeCache: (key, data) => this.writeCache(key, data)
|
|
144
|
-
});
|
|
145
|
-
this.replaceProcessedImages(await this.processor.processDirectory());
|
|
146
|
-
if (this.watchConfig) {
|
|
147
|
-
this.watchConfig.paths = [config.sourceDir];
|
|
148
|
-
}
|
|
149
|
-
this.dependencies = this.generateDependencies();
|
|
150
|
-
this.generateTypes();
|
|
151
|
-
this.buildContributionsPrepared = true;
|
|
152
|
-
}
|
|
153
|
-
getRuntimeVirtualModulePath() {
|
|
154
|
-
if (!this.context) {
|
|
155
|
-
throw new Error("ImageProcessor requires context to be set");
|
|
62
|
+
/**
|
|
63
|
+
* ImageProcessorPlugin
|
|
64
|
+
* A Processor for optimizing images.
|
|
65
|
+
*/
|
|
66
|
+
export class ImageProcessorPlugin extends Processor {
|
|
67
|
+
buildContributionsPrepared = false;
|
|
68
|
+
resolvedConfig;
|
|
69
|
+
processedImages = {};
|
|
70
|
+
constructor(config) {
|
|
71
|
+
const acceptedFormats = config.options?.acceptedFormats ?? ['jpg', 'jpeg', 'png', 'webp'];
|
|
72
|
+
const extensionPatterns = acceptedFormats.map((format) => `*.${format.replace(/^\./, '')}`);
|
|
73
|
+
const defaultWatchConfig = {
|
|
74
|
+
paths: [],
|
|
75
|
+
extensions: acceptedFormats,
|
|
76
|
+
onCreate: async (ctx) => this.process([ctx.path]),
|
|
77
|
+
onChange: async (ctx) => this.process([ctx.path]),
|
|
78
|
+
onDelete: async (ctx) => this.deleteProcessedImagesbyPath(ctx.path),
|
|
79
|
+
onError: (error) => logger.error('Watcher error', { error }),
|
|
80
|
+
};
|
|
81
|
+
super({
|
|
82
|
+
...config,
|
|
83
|
+
name: 'ecopages-image-processor',
|
|
84
|
+
description: 'A Processor for optimizing images.',
|
|
85
|
+
capabilities: [
|
|
86
|
+
{
|
|
87
|
+
kind: 'image',
|
|
88
|
+
extensions: extensionPatterns,
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
watch: config.watch ? mergeProcessorOptions(defaultWatchConfig, config.watch) : defaultWatchConfig,
|
|
92
|
+
});
|
|
156
93
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
module: this.name,
|
|
160
|
-
subPath: "virtual-module.ts"
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
getGeneratedOutputPath(src) {
|
|
164
|
-
if (!this.resolvedConfig) {
|
|
165
|
-
throw new Error("ImageProcessor not initialized");
|
|
94
|
+
get buildPlugins() {
|
|
95
|
+
return [createImagePluginBundler(this.processedImages)];
|
|
166
96
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
hasGeneratedOutputs() {
|
|
170
|
-
if (!this.resolvedConfig) {
|
|
171
|
-
return false;
|
|
97
|
+
get plugins() {
|
|
98
|
+
return [createImagePlugin(this.processedImages)];
|
|
172
99
|
}
|
|
173
|
-
|
|
174
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Replaces image-map contents without swapping the backing object.
|
|
102
|
+
*
|
|
103
|
+
* @remarks
|
|
104
|
+
* The build/runtime virtual-module plugins close over `processedImages`, so
|
|
105
|
+
* mutating the existing object keeps those plugins live after preparation.
|
|
106
|
+
*/
|
|
107
|
+
replaceProcessedImages(images) {
|
|
108
|
+
for (const key of Object.keys(this.processedImages)) {
|
|
109
|
+
delete this.processedImages[key];
|
|
110
|
+
}
|
|
111
|
+
Object.assign(this.processedImages, images);
|
|
175
112
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Resolves processor config and creates the sharp-backed worker instance.
|
|
115
|
+
*/
|
|
116
|
+
initializeProcessor() {
|
|
117
|
+
if (this.processor && this.resolvedConfig) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (!this.context) {
|
|
121
|
+
throw new Error('ImageProcessor requires context to be set');
|
|
122
|
+
}
|
|
123
|
+
logger.debug('Setting up image processor', {
|
|
124
|
+
srcDir: this.context.srcDir,
|
|
125
|
+
distDir: this.context.distDir,
|
|
126
|
+
});
|
|
127
|
+
const defaultConfig = {
|
|
128
|
+
sourceDir: `${this.context.srcDir}/public/assets/images`,
|
|
129
|
+
outputDir: `${this.context.distDir}/assets/optimized`,
|
|
130
|
+
publicPath: '/assets/optimized',
|
|
131
|
+
sizes: [],
|
|
132
|
+
quality: 80,
|
|
133
|
+
format: 'webp',
|
|
134
|
+
};
|
|
135
|
+
const config = this.options ? mergeProcessorOptions(defaultConfig, this.options) : defaultConfig;
|
|
136
|
+
this.resolvedConfig = config;
|
|
137
|
+
this.processor = new ImageProcessor(config, {
|
|
138
|
+
readCache: (key) => this.readCache(key),
|
|
139
|
+
writeCache: (key, data) => this.writeCache(key, data),
|
|
140
|
+
});
|
|
141
|
+
if (this.watchConfig) {
|
|
142
|
+
this.watchConfig.paths = [config.sourceDir];
|
|
143
|
+
}
|
|
185
144
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
145
|
+
/**
|
|
146
|
+
* Prepares build-facing processor state before config build seals the app manifest.
|
|
147
|
+
*
|
|
148
|
+
* @remarks
|
|
149
|
+
* Image processing stays in {@link setup}; this hook only initializes config and
|
|
150
|
+
* restores any previously generated virtual-module state from disk.
|
|
151
|
+
*/
|
|
152
|
+
async prepareBuildContributions() {
|
|
153
|
+
if (this.buildContributionsPrepared) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
// Lit SSR workers import eco.config in an isolated thread; image outputs are
|
|
157
|
+
// already prepared on the main dev server thread.
|
|
158
|
+
if (isLitStaticRenderWorkerThread()) {
|
|
159
|
+
this.buildContributionsPrepared = true;
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
this.initializeProcessor();
|
|
163
|
+
await this.syncProcessedImagesFromDisk();
|
|
164
|
+
this.buildContributionsPrepared = true;
|
|
191
165
|
}
|
|
192
|
-
|
|
193
|
-
|
|
166
|
+
getRuntimeVirtualModulePath() {
|
|
167
|
+
if (!this.context) {
|
|
168
|
+
throw new Error('ImageProcessor requires context to be set');
|
|
169
|
+
}
|
|
170
|
+
return resolveGeneratedPath('cache', {
|
|
171
|
+
root: this.context.distDir,
|
|
172
|
+
module: this.name,
|
|
173
|
+
subPath: 'virtual-module.ts',
|
|
174
|
+
});
|
|
194
175
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
* Generate dependencies for processor.
|
|
201
|
-
* It is ossible to define which one should be included in the final bundle based on the environment.
|
|
202
|
-
* @returns
|
|
203
|
-
*/
|
|
204
|
-
generateDependencies() {
|
|
205
|
-
const deps = [];
|
|
206
|
-
if (process.env.NODE_ENV === "development") {
|
|
176
|
+
getGeneratedOutputPath(src) {
|
|
177
|
+
if (!this.resolvedConfig) {
|
|
178
|
+
throw new Error('ImageProcessor not initialized');
|
|
179
|
+
}
|
|
180
|
+
return path.join(this.resolvedConfig.outputDir, path.basename(src));
|
|
207
181
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Restores in-memory image state from previously generated dist artifacts.
|
|
184
|
+
*
|
|
185
|
+
* @returns `true` when state was restored without invoking sharp.
|
|
186
|
+
*/
|
|
187
|
+
async syncProcessedImagesFromDisk() {
|
|
188
|
+
if (!this.resolvedConfig) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
const sourceImages = await getSourceImagePaths(this.resolvedConfig);
|
|
192
|
+
const imageMap = await loadProcessedImagesFromDisk({
|
|
193
|
+
resolvedConfig: this.resolvedConfig,
|
|
194
|
+
runtimeVirtualModulePath: this.getRuntimeVirtualModulePath(),
|
|
195
|
+
});
|
|
196
|
+
if (imageMap === null) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
if (sourceImages.length > 0 && Object.keys(imageMap).length !== sourceImages.length) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
this.replaceProcessedImages(imageMap);
|
|
203
|
+
if (Object.keys(imageMap).length > 0) {
|
|
204
|
+
this.generateTypes();
|
|
205
|
+
}
|
|
206
|
+
return true;
|
|
216
207
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
throw new Error("ImageProcessor not initialized");
|
|
208
|
+
async ensureGeneratedOutputs() {
|
|
209
|
+
if (!this.processor) {
|
|
210
|
+
throw new Error('ImageProcessor not initialized');
|
|
211
|
+
}
|
|
212
|
+
if (await this.syncProcessedImagesFromDisk()) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
this.replaceProcessedImages(await this.processor.processDirectory());
|
|
216
|
+
this.generateTypes();
|
|
227
217
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
if (result) {
|
|
233
|
-
this.processedImages[path.basename(image)] = result;
|
|
218
|
+
writeGeneratedFile(filePath, content) {
|
|
219
|
+
fileSystem.ensureDir(path.dirname(filePath));
|
|
220
|
+
if (fileSystem.exists(filePath) && fileSystem.readFileSync(filePath) === content) {
|
|
221
|
+
return;
|
|
234
222
|
}
|
|
235
|
-
|
|
236
|
-
logger.error("Failed to process image", { image, error });
|
|
237
|
-
}
|
|
223
|
+
fileSystem.write(filePath, content);
|
|
238
224
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
225
|
+
/**
|
|
226
|
+
* Ensures optimized image outputs and virtual-module state exist on disk.
|
|
227
|
+
*/
|
|
228
|
+
async setup() {
|
|
229
|
+
if (isLitStaticRenderWorkerThread()) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
this.initializeProcessor();
|
|
233
|
+
await this.ensureGeneratedOutputs();
|
|
248
234
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
await fileSystem.removeAsync(cachePath);
|
|
271
|
-
logger.debug("Deleted cache file for image", { cachePath });
|
|
272
|
-
} catch (error) {
|
|
273
|
-
logger.error("Failed to delete cache file for image", { imagePath, error });
|
|
274
|
-
}
|
|
275
|
-
delete this.processedImages[path.basename(imagePath)];
|
|
276
|
-
this.generateTypes();
|
|
277
|
-
} catch (error) {
|
|
278
|
-
logger.error("Failed to delete processed images", { path: imagePath, error });
|
|
235
|
+
/**
|
|
236
|
+
* Process images.
|
|
237
|
+
* @param images
|
|
238
|
+
*/
|
|
239
|
+
async process(images) {
|
|
240
|
+
if (!this.processor) {
|
|
241
|
+
throw new Error('ImageProcessor not initialized');
|
|
242
|
+
}
|
|
243
|
+
logger.debug('Processing images', { count: images.length });
|
|
244
|
+
for (const image of images) {
|
|
245
|
+
try {
|
|
246
|
+
const result = await this.processor.processImage(image);
|
|
247
|
+
if (result) {
|
|
248
|
+
this.processedImages[path.basename(image)] = result;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
logger.error('Failed to process image', { image, error });
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
this.generateTypes();
|
|
279
256
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
257
|
+
/**
|
|
258
|
+
* Delete processed images using the original image path.
|
|
259
|
+
* @param imagePath
|
|
260
|
+
*/
|
|
261
|
+
async deleteProcessedImagesbyPath(imagePath) {
|
|
262
|
+
if (!this.processor) {
|
|
263
|
+
throw new Error('ImageProcessor not initialized');
|
|
264
|
+
}
|
|
265
|
+
logger.debug('Deleting processed images', { path: imagePath });
|
|
266
|
+
try {
|
|
267
|
+
const baseNameWithoutExt = path.basename(imagePath, path.extname(imagePath));
|
|
268
|
+
if (!this.options) {
|
|
269
|
+
throw new Error('Options not set');
|
|
270
|
+
}
|
|
271
|
+
const outputDir = this.options.outputDir;
|
|
272
|
+
const files = await fileSystem.glob([`${outputDir}/${baseNameWithoutExt}-*`]);
|
|
273
|
+
await Promise.all(files.map(async (file) => {
|
|
274
|
+
try {
|
|
275
|
+
await fileSystem.removeAsync(file);
|
|
276
|
+
logger.debug('Deleted processed image', { file });
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
logger.error('Failed to delete processed image', { file, error });
|
|
280
|
+
}
|
|
281
|
+
}));
|
|
282
|
+
try {
|
|
283
|
+
const cacheKey = this.processedImages[path.basename(imagePath)].cacheKey;
|
|
284
|
+
const cachePath = this.getCachePath(cacheKey);
|
|
285
|
+
await fileSystem.removeAsync(cachePath);
|
|
286
|
+
logger.debug('Deleted cache file for image', { cachePath });
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
logger.error('Failed to delete cache file for image', { imagePath, error });
|
|
290
|
+
}
|
|
291
|
+
delete this.processedImages[path.basename(imagePath)];
|
|
292
|
+
this.generateTypes();
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
logger.error('Failed to delete processed images', { path: imagePath, error });
|
|
296
|
+
}
|
|
287
297
|
}
|
|
288
|
-
|
|
298
|
+
/**
|
|
299
|
+
* Generate types for the virtual module.
|
|
300
|
+
*/
|
|
301
|
+
generateTypes() {
|
|
302
|
+
if (!this.options?.outputDir) {
|
|
303
|
+
throw new Error('Output directory not set');
|
|
304
|
+
}
|
|
305
|
+
const content = `
|
|
289
306
|
/**
|
|
290
307
|
* Do not edit manually. This file is auto-generated.
|
|
291
308
|
* This file contains the type definitions for the virtual module "ecopages:images".
|
|
@@ -294,53 +311,54 @@ class ImageProcessorPlugin extends Processor {
|
|
|
294
311
|
${IMAGE_VIRTUAL_MODULE_TYPES}
|
|
295
312
|
|
|
296
313
|
declare module "ecopages:images" {
|
|
297
|
-
${Object.keys(this.processedImages)
|
|
314
|
+
${Object.keys(this.processedImages)
|
|
315
|
+
.map((key) => `export const ${anyCaseToCamelCase(key)}: ImageSpecifications;`)
|
|
316
|
+
.join('\n ')}
|
|
298
317
|
}`;
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
318
|
+
if (!this.context)
|
|
319
|
+
throw new Error('Processor is not configured correctly');
|
|
320
|
+
const typesDir = resolveGeneratedPath('types', {
|
|
321
|
+
root: this.context.rootDir,
|
|
322
|
+
module: this.name,
|
|
323
|
+
subPath: 'virtual-module.d.ts',
|
|
324
|
+
});
|
|
325
|
+
this.writeGeneratedFile(typesDir, content);
|
|
326
|
+
logger.debug('Generated types for virtual module', { typesDir });
|
|
327
|
+
const indexTypesDir = resolveGeneratedPath('types', {
|
|
328
|
+
root: this.context.rootDir,
|
|
329
|
+
module: this.name,
|
|
330
|
+
subPath: 'index.d.ts',
|
|
331
|
+
});
|
|
332
|
+
const indexContent = 'import "./virtual-module.d.js";';
|
|
333
|
+
this.writeGeneratedFile(indexTypesDir, indexContent);
|
|
334
|
+
logger.debug('Generated index types for virtual module', { indexTypesDir });
|
|
335
|
+
const runtimeVirtualModulePath = resolveGeneratedPath('cache', {
|
|
336
|
+
root: this.context.distDir,
|
|
337
|
+
module: this.name,
|
|
338
|
+
subPath: 'virtual-module.ts',
|
|
339
|
+
});
|
|
340
|
+
const runtimeModuleContent = Object.entries(this.processedImages)
|
|
341
|
+
.map(([key, value]) => {
|
|
342
|
+
return `export const ${anyCaseToCamelCase(key)} = ${JSON.stringify(value, null, 2)} as const;`;
|
|
343
|
+
})
|
|
344
|
+
.join('\n\n');
|
|
345
|
+
this.writeGeneratedFile(runtimeVirtualModulePath, runtimeModuleContent);
|
|
346
|
+
logger.debug('Generated runtime virtual module for images', { runtimeVirtualModulePath });
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Teardown the image processor.
|
|
350
|
+
*/
|
|
351
|
+
async teardown() {
|
|
352
|
+
logger.debug('Tearing down image processor');
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Get the image processor instance.
|
|
356
|
+
* @returns The image processor instance.
|
|
357
|
+
*/
|
|
358
|
+
getProcessor() {
|
|
359
|
+
return this.processor;
|
|
360
|
+
}
|
|
339
361
|
}
|
|
340
|
-
const imageProcessorPlugin = (config) => {
|
|
341
|
-
|
|
342
|
-
};
|
|
343
|
-
export {
|
|
344
|
-
ImageProcessorPlugin,
|
|
345
|
-
imageProcessorPlugin
|
|
362
|
+
export const imageProcessorPlugin = (config) => {
|
|
363
|
+
return new ImageProcessorPlugin(config);
|
|
346
364
|
};
|
package/src/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/src/utils.js
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
function anyCaseToCamelCase(str) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
export function anyCaseToCamelCase(str) {
|
|
2
|
+
if (!str)
|
|
3
|
+
return '';
|
|
4
|
+
let result = '';
|
|
5
|
+
let capitalize = false;
|
|
6
|
+
for (let i = 0; i < str.length; i++) {
|
|
7
|
+
const char = str[i];
|
|
8
|
+
if (/[0-9]/.test(char)) {
|
|
9
|
+
result += char;
|
|
10
|
+
capitalize = true;
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
if (/[a-zA-Z]/.test(char)) {
|
|
14
|
+
if (capitalize) {
|
|
15
|
+
result += char.toUpperCase();
|
|
16
|
+
capitalize = false;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
result += char.toLowerCase();
|
|
20
|
+
}
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
capitalize = true;
|
|
11
24
|
}
|
|
12
|
-
|
|
13
|
-
if (capitalize) {
|
|
14
|
-
result += char.toUpperCase();
|
|
15
|
-
capitalize = false;
|
|
16
|
-
} else {
|
|
17
|
-
result += char.toLowerCase();
|
|
18
|
-
}
|
|
19
|
-
continue;
|
|
20
|
-
}
|
|
21
|
-
capitalize = true;
|
|
22
|
-
}
|
|
23
|
-
return result;
|
|
25
|
+
return result;
|
|
24
26
|
}
|
|
25
|
-
export {
|
|
26
|
-
anyCaseToCamelCase
|
|
27
|
-
};
|