Skip to content

Commit 2d831b7

Browse files
committed
webpack 5
1 parent dad7403 commit 2d831b7

18 files changed

+3604
-3487
lines changed

package-lock.json

Lines changed: 3312 additions & 2574 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"@types/find-cache-dir": "^3.2.0",
5959
"@types/jest": "^26.0.23",
6060
"@types/json-schema": "^7.0.7",
61-
"@types/node": "^15.12.0",
61+
"@types/node": "^14.17.0",
6262
"@types/sharp": "^0.28.3",
6363
"@types/webpack": "^5.28.0",
6464
"@typescript-eslint/eslint-plugin": "^4.26.0",
@@ -71,7 +71,7 @@
7171
"prettier-eslint": "^12.0.0",
7272
"sharp": "^0.28.3",
7373
"typescript": "^4.3.2",
74-
"webpack": "5.37.0",
74+
"webpack": "^5.37.0",
7575
"webpack-cli": "^4.7.0"
7676
},
7777
"jest": {

src/index.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { parseOptions, getOutputAndPublicPath, createPlaceholder } from './utils
66
import { cache } from './cache'
77
import type { LoaderContext } from 'webpack'
88

9-
import { interpolateName } from './interpolateName'
9+
import interpolateName from './interpolateName'
1010
import { parseQuery } from './parseQuery'
1111

1212
import type {
@@ -43,40 +43,36 @@ const DEFAULTS = {
4343
*
4444
* @return {loaderCallback} loaderCallback Result
4545
*/
46-
export default function loader(this: LoaderContext<any>, content: string): void {
46+
export default function loader(this: LoaderContext<Options>, content: string): void {
4747
const loaderCallback = this.async()
4848
if (typeof loaderCallback == 'undefined') {
4949
new Error('Responsive loader callback error')
5050
return
5151
}
5252

53+
// Parsers the query string and options
5354
const parsedResourceQuery = this.resourceQuery ? parseQuery(this.resourceQuery) : {}
54-
// Combines defaults, webpack options and query options,
55-
// later sources' properties overwrite earlier ones.
56-
const options: Options = Object.assign({}, DEFAULTS, this.getOptions(), parsedResourceQuery)
57-
// // Object representation of the query string
5855

59-
// // Combines defaults, webpack options and query options,
60-
// // later sources' properties overwrite earlier ones.
61-
// const options: Options = Object.assign({}, DEFAULTS, getOptions(this), parsedResourceQuery)
56+
// Combines defaults, webpack options and query options,
57+
const options = { ...DEFAULTS, ...this.getOptions(), ...parsedResourceQuery }
6258

6359
validate(schema as JSONSchema7, options, { name: 'Responsive Loader' })
6460

65-
/**
66-
* Parses options and set defaults options
67-
*/
68-
const { outputContext, mime, ext, name, sizes, outputPlaceholder, placeholderSize, imageOptions, cacheOptions } =
69-
parseOptions(this, options)
61+
const outputContext = options.context || this.rootContext
62+
const { mime, ext, name, sizes, outputPlaceholder, placeholderSize, imageOptions, cacheOptions } = parseOptions(
63+
this.resourcePath,
64+
options
65+
)
7066

7167
if (!mime) {
7268
loaderCallback(new Error('No mime type for file with extension ' + ext + ' supported'))
7369
return
7470
}
7571

7672
const createFile = ({ data, width, height }: AdapterResizeResponse) => {
77-
const fileName = interpolateName(this, name, {
73+
const fileName = interpolateName(this.resourcePath, this.resourceQuery, name, {
7874
context: outputContext,
79-
content: data,
75+
content: data.toString(),
8076
})
8177
.replace(/\[width\]/gi, width + '')
8278
.replace(/\[height\]/gi, height + '')
@@ -114,12 +110,10 @@ export default function loader(this: LoaderContext<any>, content: string): void
114110
)
115111
return
116112
}
117-
/**
118-
* The full config is passed to the adapter, later sources' properties overwrite earlier ones.
119-
*/
113+
// The full config is passed to the adapter, later sources' properties overwrite earlier ones.
120114
const adapterOptions = Object.assign({}, options, imageOptions)
121115

122-
const transformParams: TransformParams = {
116+
const transformParams = {
123117
adapterModule: options.adapter,
124118
resourcePath: this.resourcePath,
125119
adapterOptions,
@@ -155,8 +149,8 @@ async function orchestrate(params: OrchestrateParams) {
155149
// Transform based on the parameters
156150
export async function transform({
157151
adapterModule,
158-
createFile,
159152
resourcePath,
153+
createFile,
160154
sizes,
161155
mime,
162156
outputPlaceholder,
@@ -180,16 +174,16 @@ export async function transform({
180174

181175
const srcset = files.map((f) => f.src).join('+","+')
182176
const images = files.map((f) => `{path: ${f.path},width: ${f.width},height: ${f.height}}`).join(',')
183-
const firstImage = files[0]
177+
const defaultImage = outputPlaceholder ? files[files.length - 2] : files[files.length - 1]
184178

185179
return `${esModule ? 'export default' : 'module.exports ='} {
186180
srcSet: ${srcset},
187181
images: [${images}],
188-
src: ${firstImage.path},
189-
toString: function(){return ${firstImage.path}},
182+
src: ${defaultImage.path},
183+
toString: function(){return ${defaultImage.path}},
190184
${placeholder ? 'placeholder: ' + placeholder + ',' : ''}
191-
width: ${firstImage.width},
192-
height: ${firstImage.height}
185+
width: ${defaultImage.width},
186+
height: ${defaultImage.height}
193187
}`
194188
}
195189

src/interpolateName.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
import type { LoaderContext } from 'webpack'
2+
33
import { util } from 'webpack'
44
import * as path from 'path'
55

@@ -8,7 +8,12 @@ type Options = {
88
content: string
99
}
1010

11-
function interpolateName(loaderContext: LoaderContext<any>, name: string, options: Options): string {
11+
export default function interpolateName(
12+
loaderResourcePath: string,
13+
loaderResourceQuery: string,
14+
name: string,
15+
options: Options
16+
): string {
1217
const filename = name || '[hash].[ext]'
1318

1419
const context = options.context
@@ -20,9 +25,9 @@ function interpolateName(loaderContext: LoaderContext<any>, name: string, option
2025
let folder = ''
2126
let query = ''
2227

23-
if (loaderContext.resourcePath) {
24-
const parsed = path.parse(loaderContext.resourcePath)
25-
let resourcePath = loaderContext.resourcePath
28+
if (loaderResourcePath) {
29+
const parsed = path.parse(loaderResourcePath)
30+
let resourcePath = loaderResourcePath
2631

2732
if (parsed.ext) {
2833
ext = parsed.ext.substr(1)
@@ -50,8 +55,8 @@ function interpolateName(loaderContext: LoaderContext<any>, name: string, option
5055
}
5156
}
5257

53-
if (loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1) {
54-
query = loaderContext.resourceQuery
58+
if (loaderResourceQuery && loaderResourceQuery.length > 1) {
59+
query = loaderResourceQuery
5560

5661
const hashIdx = query.indexOf('#')
5762

@@ -79,14 +84,5 @@ function interpolateName(loaderContext: LoaderContext<any>, name: string, option
7984
.replace(/\[folder\]/gi, () => folder)
8085
.replace(/\[query\]/gi, () => query)
8186

82-
// if (
83-
// typeof loaderContext.getOptions() === 'object' &&
84-
// typeof loaderContext.getOptions().customInterpolateName === 'function'
85-
// ) {
86-
// url = loaderContext.getOptions().customInterpolateName.call(loaderContext, url, name, options)
87-
// }
88-
8987
return url
9088
}
91-
92-
export { interpolateName }

src/types.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
declare type Options = {
1+
export type Options = {
22
size?: string | number
33
sizes?: [string | number]
44
min?: string | number
@@ -44,17 +44,17 @@ export interface AdapterImplementation {
4444
metadata: () => Promise<{ width: number; height: number }>
4545
resize: (config: { width: number; mime: string; options: Options }) => Promise<AdapterResizeResponse>
4646
}
47-
export type AdapterResizeResponse = { data: string; width: number; height: number }
47+
export type AdapterResizeResponse = { data: string | Buffer; width: number; height: number }
4848

4949
export interface TransformParams {
5050
adapterModule: Adapter | undefined
51+
resourcePath: string
5152
createFile: ({ data, width, height }: AdapterResizeResponse) => {
5253
src: string
5354
path: string
5455
width: number
5556
height: number
5657
}
57-
resourcePath: string
5858
outputPlaceholder: boolean
5959
placeholderSize: number
6060
mime: MimeType

src/utils.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as path from 'path'
22
import type { Options, MimeType, ImageOptions, CacheOptions } from './types'
3-
import type { LoaderContext } from 'webpack'
43

54
const version = '3'
65

@@ -20,7 +19,6 @@ enum EXTS {
2019
}
2120

2221
type ParsedOptions = {
23-
outputContext: string
2422
outputPlaceholder: boolean
2523
placeholderSize: number
2624
name: string
@@ -31,10 +29,7 @@ type ParsedOptions = {
3129
cacheOptions: CacheOptions
3230
}
3331

34-
function parseOptions(loaderContext: LoaderContext<any>, options: Options): ParsedOptions {
35-
const outputContext: string = options.context || loaderContext.rootContext
36-
// <path>/<to>/<folder>/responsive-loader
37-
32+
function parseOptions(resourcePath: string, options: Options): ParsedOptions {
3833
const outputPlaceholder = Boolean(options.placeholder)
3934
const placeholderSize: number = parseInt(options.placeholderSize + '', 10)
4035

@@ -54,7 +49,7 @@ function parseOptions(loaderContext: LoaderContext<any>, options: Options): Pars
5449
mime = MIMES[options.format]
5550
ext = EXTS[mime]
5651
} else {
57-
ext = path.extname(loaderContext.resourcePath).replace(/\./, '')
52+
ext = path.extname(resourcePath).replace(/\./, '')
5853
switch (ext) {
5954
case 'jpg':
6055
case 'jpeg':
@@ -98,7 +93,6 @@ function parseOptions(loaderContext: LoaderContext<any>, options: Options): Pars
9893
cacheCompression: Boolean(options.cacheCompression),
9994
}
10095
return {
101-
outputContext,
10296
ext,
10397
mime,
10498
name,

0 commit comments

Comments
 (0)