UNPKG

4.97 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = loader;
7exports.raw = void 0;
8
9var _path = _interopRequireDefault(require("path"));
10
11var _loaderUtils = require("loader-utils");
12
13var _schemaUtils = require("schema-utils");
14
15var _utils = require("./utils");
16
17var _schema = _interopRequireDefault(require("./schema.json"));
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21const DEFAULTS = {
22 outputPlaceholder: false,
23 placeholderSize: 40,
24 quality: 85,
25 name: "[hash]-[width].[ext]",
26 steps: 4,
27 esModule: false,
28 emitFile: true,
29 rotate: 0
30};
31/**
32 * **Responsive Loader**
33 *
34 * Creates multiple images from one source image, and returns a srcset
35 * [Responsive Images Loader](https://github.com/dazuaz/responsive-loader)
36 *
37 * @method loader
38 *
39 * @param {Buffer} content Source
40 *
41 * @return {loaderCallback} loaderCallback Result
42 */
43// module.exports = function loader(content: Buffer) {
44
45function loader(content) {
46 const loaderCallback = this.async();
47 const parsedResourceQuery = this.resourceQuery ? (0, _loaderUtils.parseQuery)(this.resourceQuery) : {}; // combine webpack options with query options
48
49 const options = Object.assign({}, (0, _loaderUtils.getOptions)(this), parsedResourceQuery);
50 (0, _schemaUtils.validate)(_schema.default, options, "Responsive Loader"); // parses options and set defaults options
51
52 const {
53 outputContext,
54 outputPlaceholder,
55 placeholderSize,
56 quality,
57 background,
58 rotate,
59 progressive,
60 mime,
61 ext,
62 name,
63 generatedSizes,
64 esModule,
65 emitFile
66 } = (0, _utils.parseOptions)(this, options, DEFAULTS);
67 const sizes = parsedResourceQuery.size || parsedResourceQuery.sizes || generatedSizes || options.size || options.sizes || [Number.MAX_SAFE_INTEGER];
68
69 if (!sizes) {
70 return loaderCallback(null, content);
71 }
72
73 if (!mime) {
74 return loaderCallback(new Error("No mime type for file with extension " + ext + " supported"));
75 }
76
77 const createFile = ({
78 data,
79 width,
80 height
81 }) => {
82 const fileName = (0, _loaderUtils.interpolateName)(this, name, {
83 context: outputContext,
84 content: data
85 }).replace(/\[width\]/gi, width).replace(/\[height\]/gi, height);
86 const {
87 outputPath,
88 publicPath
89 } = (0, _utils.getOutputAndPublicPath)(fileName, options);
90
91 if (emitFile) {
92 this.emitFile(outputPath, data);
93 }
94
95 return {
96 src: publicPath + `+${JSON.stringify(` ${width}w`)}`,
97 path: publicPath,
98 width: width,
99 height: height
100 };
101 }; // Disable processing of images by this loader (useful in development)
102
103
104 if (options.disable) {
105 const {
106 path
107 } = createFile({
108 data: content,
109 width: "100",
110 height: "100"
111 });
112 loaderCallback(null, `${esModule ? "export default" : "module.exports ="} {
113 srcSet:${path},
114 images:[{path:${path},width:100,height:100}],
115 src: ${path},
116 toString:function(){return ${path}}
117 };`);
118 return;
119 }
120
121 const adapter = options.adapter || require("./adapters/jimp"); // The config that is passed to the adapters
122
123
124 const adapterOptions = Object.assign({}, options, {
125 quality,
126 background,
127 rotate,
128 progressive
129 });
130 const img = adapter(this.resourcePath);
131 img.metadata().then(metadata => {
132 let promises = [];
133 const widthsToGenerate = new Set();
134 (Array.isArray(sizes) ? sizes : [sizes]).forEach(size => {
135 const width = Math.min(metadata.width, parseInt(size, 10)); // Only resize images if they aren't an exact copy of one already being resized...
136
137 if (!widthsToGenerate.has(width)) {
138 widthsToGenerate.add(width);
139 promises.push(img.resize({
140 width,
141 mime,
142 options: adapterOptions
143 }));
144 }
145 });
146
147 if (outputPlaceholder) {
148 promises.push(img.resize({
149 width: placeholderSize,
150 options: adapterOptions,
151 mime
152 }));
153 }
154
155 return Promise.all(promises).then(results => outputPlaceholder ? {
156 files: results.slice(0, -1).map(createFile),
157 placeholder: (0, _utils.createPlaceholder)(results[results.length - 1], mime)
158 } : {
159 files: results.map(createFile)
160 });
161 }).then(({
162 files,
163 placeholder
164 }) => {
165 const srcset = files.map(f => f.src).join('+","+');
166 const images = files.map(f => `{path: ${f.path},width: ${f.width},height: ${f.height}}`).join(",");
167 const firstImage = files[0];
168 loaderCallback(null, `${esModule ? "export default" : "module.exports ="} {
169 srcSet: ${srcset},
170 images:[ ${images}],
171 src: ${firstImage.path},
172 toString:function(){return ${firstImage.path}},
173 placeholder: ${placeholder},
174 width: ${firstImage.width},
175 height: ${firstImage.height}
176 }`);
177 }).catch(err => loaderCallback(err));
178}
179
180const raw = true;
181exports.raw = raw;
\No newline at end of file