Skip to content

Commit 9ec1fc7

Browse files
committed
.
1 parent 923cdc8 commit 9ec1fc7

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

src/index.ts

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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'
66

77
import type {
88
Adapter,
@@ -13,20 +13,20 @@ import type {
1313
MimeType,
1414
AdapterResizeResponse,
1515
TransformParams,
16-
} from "./types"
16+
} from './types'
1717

1818
const DEFAULTS = {
1919
quality: 85,
2020
placeholder: false,
2121
placeholderSize: 40,
22-
name: "[hash]-[width].[ext]",
22+
name: '[hash]-[width].[ext]',
2323
steps: 4,
2424
esModule: false,
2525
emitFile: true,
2626
rotate: 0,
2727
cacheDirectory: false,
2828
cacheCompression: true,
29-
cacheIdentifier: "",
29+
cacheIdentifier: '',
3030
}
3131

3232
/**
@@ -41,12 +41,20 @@ const DEFAULTS = {
4141
*/
4242
export default function loader(this: LoaderContext, content: Buffer): void {
4343
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
4450
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.
4654
const options: Options = Object.assign({}, DEFAULTS, getOptions(this), parsedResourceQuery)
4755

4856
// @ts-ignore
49-
validate(schema, options, { name: "Responsive Loader" })
57+
validate(schema, options, { name: 'Responsive Loader' })
5058

5159
/**
5260
* Parses options and set defaults options
@@ -63,18 +71,8 @@ export default function loader(this: LoaderContext, content: Buffer): void {
6371
cacheOptions,
6472
} = parseOptions(this, options)
6573

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-
7674
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'))
7876
return
7977
}
8078

@@ -83,8 +81,8 @@ export default function loader(this: LoaderContext, content: Buffer): void {
8381
context: outputContext,
8482
content: data,
8583
})
86-
.replace(/\[width\]/gi, width + "")
87-
.replace(/\[height\]/gi, height + "")
84+
.replace(/\[width\]/gi, width + '')
85+
.replace(/\[height\]/gi, height + '')
8886

8987
const { outputPath, publicPath } = getOutputAndPublicPath(fileName, {
9088
outputPath: options.outputPath,
@@ -110,7 +108,7 @@ export default function loader(this: LoaderContext, content: Buffer): void {
110108
const { path } = createFile({ data: content, width: 100, height: 100 })
111109
loaderCallback(
112110
null,
113-
`${options.esModule ? "export default" : "module.exports ="} {
111+
`${options.esModule ? 'export default' : 'module.exports ='} {
114112
srcSet: ${path},
115113
images: [{path:${path},width:100,height:100}],
116114
src: ${path},
@@ -169,7 +167,7 @@ export async function transform({
169167
adapterOptions,
170168
esModule,
171169
}: TransformParams): Promise<string> {
172-
const adapter: Adapter = adapterModule || require("./adapters/jimp")
170+
const adapter: Adapter = adapterModule || require('./adapters/jimp')
173171
const img = adapter(resourcePath)
174172
const results = await transformations({ img, sizes, mime, outputPlaceholder, placeholderSize, adapterOptions })
175173

@@ -184,15 +182,15 @@ export async function transform({
184182
}
185183

186184
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(',')
188186
const firstImage = files[0]
189187

190-
return `${esModule ? "export default" : "module.exports ="} {
188+
return `${esModule ? 'export default' : 'module.exports ='} {
191189
srcSet: ${srcset},
192190
images: [${images}],
193191
src: ${firstImage.path},
194192
toString: function(){return ${firstImage.path}},
195-
${placeholder ? "placeholder: " + placeholder + "," : ""}
193+
${placeholder ? 'placeholder: ' + placeholder + ',' : ''}
196194
width: ${firstImage.width},
197195
height: ${firstImage.height}
198196
}`

0 commit comments

Comments
 (0)