-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathresponsive-loader.js
57 lines (51 loc) · 1.54 KB
/
responsive-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const path = require('path');
const { getFileLoaderOptions } = require('./file-loader');
/**
* Build options for the webpack responsive loader
*
* @param {object} nextConfig - next.js configuration
* @param {object} detectedLoaders - all detected and installed loaders
* @returns {object}
*/
const getResponsiveLoaderOptions = ({
responsive,
...nextConfig
}, isServer, detectedLoaders) => {
let adapter = responsive ? responsive.adapter : undefined;
if (!adapter && detectedLoaders.responsiveAdapter === 'sharp') {
adapter = require(`${detectedLoaders.responsive}${path.sep}sharp`); // eslint-disable-line
}
return {
...getFileLoaderOptions(nextConfig, isServer),
name: '[name]-[width]-[hash].[ext]',
...(responsive || {}),
adapter,
};
};
/**
* Apply the responsive loader to the webpack configuration
*
* @param {object} webpackConfig - webpack configuration
* @param {object} nextConfig - next.js configuration
* @param {boolean} isServer - if the build is for the server
* @param {object} detectedLoaders - all detected and installed loaders
* @returns {object}
*/
const applyResponsiveLoader = (webpackConfig, nextConfig, isServer, detectedLoaders) => {
webpackConfig.module.rules.push({
test: /\.(jpe?g|png)$/i,
oneOf: [
{
use: {
loader: 'responsive-loader',
options: getResponsiveLoaderOptions(nextConfig, isServer, detectedLoaders),
},
},
],
});
return webpackConfig;
};
module.exports = {
getResponsiveLoaderOptions,
applyResponsiveLoader,
};