1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.parseOptions = parseOptions;
|
7 | exports.createPlaceholder = exports.getOutputAndPublicPath = void 0;
|
8 |
|
9 | var _path = _interopRequireDefault(require("path"));
|
10 |
|
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
12 |
|
13 | const MIMES = {
|
14 | jpg: "image/jpeg",
|
15 | jpeg: "image/jpeg",
|
16 | png: "image/png",
|
17 | webp: "image/webp"
|
18 | };
|
19 | const EXTS = {
|
20 | "image/jpeg": "jpg",
|
21 | "image/png": "png",
|
22 | "image/webp": "webp"
|
23 | };
|
24 |
|
25 | function 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;
|
29 |
|
30 | const quality = parseInt(options.quality, 10) || defaults.quality;
|
31 |
|
32 | const background = options.background;
|
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 |
|
81 | const createPlaceholder = ({
|
82 | data
|
83 | }, mime) => {
|
84 | return `"data:${mime};base64,${data.toString("base64")}"`;
|
85 | };
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | exports.createPlaceholder = createPlaceholder;
|
102 |
|
103 | const 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 |
|
137 | exports.getOutputAndPublicPath = getOutputAndPublicPath; |
\ | No newline at end of file |