1
- import { parseQuery , getOptions , interpolateName } from " loader-utils"
2
- import { validate } from " schema-utils"
3
- import * as schema from " ./schema.json"
4
- import { parseOptions , getOutputAndPublicPath , createPlaceholder } from " ./utils"
5
- import { cache } from " ./cache"
1
+ import { parseQuery , getOptions , interpolateName } from ' loader-utils'
2
+ import { validate } from ' schema-utils'
3
+ import * as schema from ' ./schema.json'
4
+ import { parseOptions , getOutputAndPublicPath , createPlaceholder } from ' ./utils'
5
+ import { cache } from ' ./cache'
6
6
7
7
import type {
8
8
Adapter ,
@@ -13,20 +13,20 @@ import type {
13
13
MimeType ,
14
14
AdapterResizeResponse ,
15
15
TransformParams ,
16
- } from " ./types"
16
+ } from ' ./types'
17
17
18
18
const DEFAULTS = {
19
19
quality : 85 ,
20
20
placeholder : false ,
21
21
placeholderSize : 40 ,
22
- name : " [hash]-[width].[ext]" ,
22
+ name : ' [hash]-[width].[ext]' ,
23
23
steps : 4 ,
24
24
esModule : false ,
25
25
emitFile : true ,
26
26
rotate : 0 ,
27
27
cacheDirectory : false ,
28
28
cacheCompression : true ,
29
- cacheIdentifier : "" ,
29
+ cacheIdentifier : '' ,
30
30
}
31
31
32
32
/**
@@ -41,12 +41,20 @@ const DEFAULTS = {
41
41
*/
42
42
export default function loader ( this : LoaderContext , content : Buffer ) : void {
43
43
const loaderCallback = this . async ( )
44
+ if ( typeof loaderCallback == 'undefined' ) {
45
+ new Error ( 'Responsive loader callback error' )
46
+ return
47
+ }
48
+
49
+ // Object representation of the query string
44
50
const parsedResourceQuery = this . resourceQuery ? parseQuery ( this . resourceQuery ) : { }
45
- // combine webpack options with query options, later sources' properties overwrite earlier ones.
51
+
52
+ // Combines defaults, webpack options and query options,
53
+ // later sources' properties overwrite earlier ones.
46
54
const options : Options = Object . assign ( { } , DEFAULTS , getOptions ( this ) , parsedResourceQuery )
47
55
48
56
// @ts -ignore
49
- validate ( schema , options , { name : " Responsive Loader" } )
57
+ validate ( schema , options , { name : ' Responsive Loader' } )
50
58
51
59
/**
52
60
* Parses options and set defaults options
@@ -63,18 +71,8 @@ export default function loader(this: LoaderContext, content: Buffer): void {
63
71
cacheOptions,
64
72
} = parseOptions ( this , options )
65
73
66
- if ( typeof loaderCallback == "undefined" ) {
67
- new Error ( "Responsive loader callback error" )
68
- return
69
- }
70
-
71
- if ( ! sizes . length ) {
72
- loaderCallback ( null , content )
73
- return
74
- }
75
-
76
74
if ( ! mime ) {
77
- loaderCallback ( new Error ( " No mime type for file with extension " + ext + " supported" ) )
75
+ loaderCallback ( new Error ( ' No mime type for file with extension ' + ext + ' supported' ) )
78
76
return
79
77
}
80
78
@@ -83,8 +81,8 @@ export default function loader(this: LoaderContext, content: Buffer): void {
83
81
context : outputContext ,
84
82
content : data ,
85
83
} )
86
- . replace ( / \[ w i d t h \] / gi, width + "" )
87
- . replace ( / \[ h e i g h t \] / gi, height + "" )
84
+ . replace ( / \[ w i d t h \] / gi, width + '' )
85
+ . replace ( / \[ h e i g h t \] / gi, height + '' )
88
86
89
87
const { outputPath, publicPath } = getOutputAndPublicPath ( fileName , {
90
88
outputPath : options . outputPath ,
@@ -110,7 +108,7 @@ export default function loader(this: LoaderContext, content: Buffer): void {
110
108
const { path } = createFile ( { data : content , width : 100 , height : 100 } )
111
109
loaderCallback (
112
110
null ,
113
- `${ options . esModule ? " export default" : " module.exports =" } {
111
+ `${ options . esModule ? ' export default' : ' module.exports =' } {
114
112
srcSet: ${ path } ,
115
113
images: [{path:${ path } ,width:100,height:100}],
116
114
src: ${ path } ,
@@ -169,7 +167,7 @@ export async function transform({
169
167
adapterOptions,
170
168
esModule,
171
169
} : TransformParams ) : Promise < string > {
172
- const adapter : Adapter = adapterModule || require ( " ./adapters/jimp" )
170
+ const adapter : Adapter = adapterModule || require ( ' ./adapters/jimp' )
173
171
const img = adapter ( resourcePath )
174
172
const results = await transformations ( { img, sizes, mime, outputPlaceholder, placeholderSize, adapterOptions } )
175
173
@@ -184,15 +182,15 @@ export async function transform({
184
182
}
185
183
186
184
const srcset = files . map ( ( f ) => f . src ) . join ( '+","+' )
187
- const images = files . map ( ( f ) => `{path: ${ f . path } ,width: ${ f . width } ,height: ${ f . height } }` ) . join ( "," )
185
+ const images = files . map ( ( f ) => `{path: ${ f . path } ,width: ${ f . width } ,height: ${ f . height } }` ) . join ( ',' )
188
186
const firstImage = files [ 0 ]
189
187
190
- return `${ esModule ? " export default" : " module.exports =" } {
188
+ return `${ esModule ? ' export default' : ' module.exports =' } {
191
189
srcSet: ${ srcset } ,
192
190
images: [${ images } ],
193
191
src: ${ firstImage . path } ,
194
192
toString: function(){return ${ firstImage . path } },
195
- ${ placeholder ? " placeholder: " + placeholder + "," : "" }
193
+ ${ placeholder ? ' placeholder: ' + placeholder + ',' : '' }
196
194
width: ${ firstImage . width } ,
197
195
height: ${ firstImage . height }
198
196
}`
0 commit comments