Skip to content

Commit

Permalink
Add format to loader output
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilwanner committed Jun 13, 2020
1 parent 04a9ea2 commit 8830ff8
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/processLoaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ import { defaultFurtherLoaderOptions } from './options';
* Enrich previous loader result with new information
*
* @param {string | string[]} result Previous loader result
* @param {{ width?: number; height?: number }} originalImageInfo Metadata of original image
* @param {{ width?: number; height?: number; format?: string }} originalImageInfo Metadata of original image
* @param {ImageOptions} imageOptions Image options
* @returns {string} Enriched result
*/
const enrichResult = (
result: string | string[],
originalImageInfo: { width?: number; height?: number },
originalImageInfo: { width?: number; height?: number; format?: string },
imageOptions: ImageOptions,
): string => {
const width = imageOptions.resize ? imageOptions.width : originalImageInfo.width;
const height = imageOptions.resize ? imageOptions.height : originalImageInfo.height;
const format = imageOptions.convert ? imageOptions.convert : originalImageInfo.format;

// an array means it was not processed by the url-/file-loader and the result should still be an array
// instead of a string. so in this case, append the additional export information to the array prototype
if (Array.isArray(result)) {
return `var res = ${JSON.stringify(result)};res.width=${width};res.height=${height};module.exports = res;`;
return `var res = ${JSON.stringify(result)};res.width=${width};res.height=${height};res.format=${JSON.stringify(
format || '',
)};module.exports = res;`;
}

if (result.indexOf('module.exports') < 0) {
Expand All @@ -32,23 +35,25 @@ const enrichResult = (

const output = result.replace(/((module\.exports\s*=)\s*)([^\s].*[^;])(;$|$)/g, 'var src = $3;');

return `${output}module.exports = {src:src,width:${width},height:${height},toString:function(){return src;}};`;
return `${output}module.exports = {src:src,width:${width},height:${height},format:${JSON.stringify(
format || '',
)},toString:function(){return src;}};`;
};

/**
* Process further loaders (url-loader & file-loader)
*
* @param {loader.LoaderContext} context Optimized images loader context
* @param {Buffer | string} image Processed image
* @param {{ width?: number; height?: number }} originalImageInfo Metadata of original image
* @param {{ width?: number; height?: number; format?: string }} originalImageInfo Metadata of original image
* @param {ImageOptions} imageOptions Image options
* @param {OptionObject} loaderOptions Options for further loaders
* @returns {string} Processed loader output
*/
const processLoaders = (
context: loader.LoaderContext,
image: Buffer | string | string[],
originalImageInfo: { width?: number; height?: number },
originalImageInfo: { width?: number; height?: number; format?: string },
imageOptions: ImageOptions,
loaderOptions: OptionObject,
): string => {
Expand Down

0 comments on commit 8830ff8

Please sign in to comment.