UNPKG

4.34 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createPlaceholder = exports.getOutputAndPublicPath = exports.parseOptions = void 0;
4const path = require("path");
5const version = '3';
6var MIMES;
7(function (MIMES) {
8 MIMES["jpg"] = "image/jpeg";
9 MIMES["jpeg"] = "image/jpeg";
10 MIMES["png"] = "image/png";
11 MIMES["webp"] = "image/webp";
12 MIMES["avif"] = "image/avif";
13})(MIMES || (MIMES = {}));
14var EXTS;
15(function (EXTS) {
16 EXTS["image/jpeg"] = "jpg";
17 EXTS["image/png"] = "png";
18 EXTS["image/webp"] = "webp";
19 EXTS["image/avif"] = "avif";
20})(EXTS || (EXTS = {}));
21function parseOptions(resourcePath, options) {
22 var _a;
23 const outputPlaceholder = Boolean(options.placeholder);
24 const placeholderSize = parseInt(options.placeholderSize + '', 10);
25 // Adapter compression options
26 const imageOptions = {
27 quality: parseInt(options.quality + '', 10),
28 rotate: parseInt(options.rotate + '', 10),
29 background: options.background,
30 progressive: Boolean(options.progressive),
31 };
32 // let mime: MimeType | undefined
33 // let ext: FileExt | string
34 let mime;
35 let ext;
36 if (options.format) {
37 mime = MIMES[options.format];
38 ext = EXTS[mime];
39 }
40 else {
41 ext = path.extname(resourcePath).replace(/\./, '');
42 switch (ext) {
43 case 'jpg':
44 case 'jpeg':
45 case 'png':
46 case 'webp':
47 case 'avif':
48 mime = MIMES[ext];
49 break;
50 default:
51 mime = undefined;
52 break;
53 }
54 }
55 const name = options.name.replace(/\[ext\]/gi, ext);
56 const min = options.min !== undefined ? parseInt(options.min + '', 10) : undefined;
57 const max = options.max !== undefined ? parseInt(options.max + '', 10) : undefined;
58 const steps = parseInt(options.steps + '', 10);
59 let generatedSizes;
60 if (typeof min === 'number' && max) {
61 generatedSizes = [];
62 for (let step = 0; step < steps; step++) {
63 const size = min + ((max - min) / (steps - 1)) * step;
64 generatedSizes.push(Math.ceil(size));
65 }
66 }
67 const size = parseInt(options.size + '', 10);
68 const sizes = size
69 ? [size]
70 : ((_a = options.sizes) === null || _a === void 0 ? void 0 : _a.map((size) => parseInt(size + '', 10))) || generatedSizes || [Number.MAX_SAFE_INTEGER];
71 // Cache options
72 const cacheOptions = {
73 cacheDirectory: options.cacheDirectory,
74 cacheIdentifier: JSON.stringify({
75 options,
76 'responsive-loader': version,
77 }),
78 cacheCompression: Boolean(options.cacheCompression),
79 };
80 return {
81 ext,
82 mime,
83 name,
84 sizes,
85 outputPlaceholder,
86 placeholderSize,
87 cacheOptions,
88 imageOptions,
89 };
90}
91exports.parseOptions = parseOptions;
92const createPlaceholder = ({ data }, mime) => {
93 return `"data:${mime};base64,${data.toString('base64')}"`;
94};
95exports.createPlaceholder = createPlaceholder;
96/**
97 * **Responsive Loader Paths**
98 *
99 * Returns the output and public path
100 *
101 * @method getOutputAndPublicPath
102 *
103 * @param {string} fileName
104 * @param {Config} outputPath
105 * @param {Config} publicPath
106 *
107 * @return {Config} Paths Result
108 */
109const getOutputAndPublicPath = (fileName, { outputPath: configOutputPath, publicPath: configPublicPath }) => {
110 let outputPath = fileName;
111 if (configOutputPath) {
112 if (typeof configOutputPath === 'function') {
113 outputPath = configOutputPath(fileName);
114 }
115 else {
116 outputPath = path.posix.join(configOutputPath, fileName);
117 }
118 }
119 let publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`;
120 if (configPublicPath) {
121 if (typeof configPublicPath === 'function') {
122 publicPath = configPublicPath(fileName);
123 }
124 else if (configPublicPath.endsWith('/')) {
125 publicPath = configPublicPath + fileName;
126 }
127 else {
128 publicPath = `${configPublicPath}/${fileName}`;
129 }
130 publicPath = JSON.stringify(publicPath);
131 }
132 return {
133 outputPath,
134 publicPath,
135 };
136};
137exports.getOutputAndPublicPath = getOutputAndPublicPath;