UNPKG

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