Skip to content

Commit

Permalink
Implement query option for image-trace-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilwanner committed Mar 10, 2019
1 parent 22b74f4 commit 2c7e890
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/loaders/image-trace-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Build options for the webpack image trace loader
*
* @param {object} nextConfig - next.js configuration
* @returns {object}
*/
const getImageTraceLoaderOptions = ({
imageTrace,
}) => ({
...(imageTrace || {}),
});

module.exports = {
getImageTraceLoaderOptions,
};
2 changes: 1 addition & 1 deletion lib/loaders/webp-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const applyWebpLoader = (webpackConfig, nextConfig, optimize, isServer, detectLo
*/
const getWebpResourceQuery = (nextConfig, isServer) => {
const urlLoaderOptions = getUrlLoaderOptions(nextConfig, isServer);
const imageName = urlLoaderOptions.name.indexOf('[ext]')
const imageName = urlLoaderOptions.name.indexOf('[ext]') >= 0
? urlLoaderOptions.name.replace('[ext]', '[ext].webp')
: `${urlLoaderOptions.name}.webp`;

Expand Down
13 changes: 13 additions & 0 deletions lib/resource-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { getUrlLoaderOptions } = require('./loaders/url-loader');
const { getFileLoaderOptions } = require('./loaders/file-loader');
const { getLqipLoaderOptions } = require('./loaders/lqip-loader');
const { getResponsiveLoaderOptions } = require('./loaders/responsive-loader');
const { getImageTraceLoaderOptions } = require('./loaders/image-trace-loader');

/**
* Configure the common resource queries
Expand Down Expand Up @@ -75,6 +76,17 @@ const queries = [
],
optimize: false,
},

// ?trace: generate svg image traces for placeholders
{
test: 'trace',
loaders: [
'image-trace-loader',
'url-loader',
],
optimize: true,
combinations: ['original'],
},
];

/**
Expand Down Expand Up @@ -115,6 +127,7 @@ const getResourceQueries = (
'file-loader': getFileLoaderOptions(nextConfig, isServer),
'lqip-loader': getLqipLoaderOptions(nextConfig, isServer),
'responsive-loader': getResponsiveLoaderOptions(nextConfig, isServer, detectLoaders),
'image-trace-loader': getImageTraceLoaderOptions(nextConfig),
};

return queries.map((queryConfig) => {
Expand Down

0 comments on commit 2c7e890

Please sign in to comment.