1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createPlaceholder = exports.getOutputAndPublicPath = exports.parseOptions = void 0;
|
4 | const path = require("path");
|
5 | const version = '3';
|
6 | var 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 = {}));
|
14 | var 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 = {}));
|
21 | function parseOptions(resourcePath, options) {
|
22 | var _a;
|
23 | const outputPlaceholder = Boolean(options.placeholder);
|
24 | const placeholderSize = parseInt(options.placeholderSize + '', 10);
|
25 |
|
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 |
|
33 |
|
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 |
|
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 | }
|
91 | exports.parseOptions = parseOptions;
|
92 | const createPlaceholder = ({ data }, mime) => {
|
93 | return `"data:${mime};base64,${data.toString('base64')}"`;
|
94 | };
|
95 | exports.createPlaceholder = createPlaceholder;
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | const 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 | };
|
137 | exports.getOutputAndPublicPath = getOutputAndPublicPath;
|