-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathnext-image-experimental.ts
330 lines (314 loc) · 9.84 KB
/
next-image-experimental.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { join, parse } from 'path'
import { writeFileSync } from 'fs'
import type {
API,
Collection,
FileInfo,
ImportDefaultSpecifier,
JSCodeshift,
JSXAttribute,
Options,
} from 'jscodeshift'
function findAndReplaceProps(
j: JSCodeshift,
root: Collection,
tagName: string
) {
const layoutToStyle: Record<string, Record<string, string> | null> = {
intrinsic: { maxWidth: '100%', height: 'auto' },
responsive: { width: '100%', height: 'auto' },
fill: null,
fixed: null,
}
const layoutToSizes: Record<string, string | null> = {
intrinsic: null,
responsive: '100vw',
fill: '100vw',
fixed: null,
}
root
.find(j.JSXElement)
.filter(
(el) =>
el.value.openingElement.name &&
el.value.openingElement.name.type === 'JSXIdentifier' &&
el.value.openingElement.name.name === tagName
)
.forEach((el) => {
let layout = 'intrinsic'
let objectFit = null
let objectPosition = null
let styleExpProps = []
let sizesAttr: JSXAttribute | null = null
const attributes = el.node.openingElement.attributes?.filter((a) => {
if (a.type !== 'JSXAttribute') {
return true
}
if (a.name.name === 'layout' && 'value' in a.value) {
layout = String(a.value.value)
return false
}
if (a.name.name === 'objectFit' && 'value' in a.value) {
objectFit = String(a.value.value)
return false
}
if (a.name.name === 'objectPosition' && 'value' in a.value) {
objectPosition = String(a.value.value)
return false
}
if (a.name.name === 'lazyBoundary') {
return false
}
if (a.name.name === 'lazyRoot') {
return false
}
if (a.name.name === 'style') {
if (
a.value?.type === 'JSXExpressionContainer' &&
a.value.expression.type === 'ObjectExpression'
) {
styleExpProps = a.value.expression.properties
} else if (
a.value?.type === 'JSXExpressionContainer' &&
a.value.expression.type === 'Identifier'
) {
styleExpProps = [
j.spreadElement(j.identifier(a.value.expression.name)),
]
} else {
console.warn('Unknown style attribute value detected', a.value)
}
return false
}
if (a.name.name === 'sizes') {
sizesAttr = a
return false
}
if (a.name.name === 'lazyBoundary') {
return false
}
if (a.name.name === 'lazyRoot') {
return false
}
return true
})
if (layout === 'fill') {
attributes.push(j.jsxAttribute(j.jsxIdentifier('fill')))
}
const sizes = layoutToSizes[layout]
if (sizes && !sizesAttr) {
sizesAttr = j.jsxAttribute(j.jsxIdentifier('sizes'), j.literal(sizes))
}
if (sizesAttr) {
attributes.push(sizesAttr)
}
let style = layoutToStyle[layout]
if (style || objectFit || objectPosition) {
if (!style) {
style = {}
}
if (objectFit) {
style.objectFit = objectFit
}
if (objectPosition) {
style.objectPosition = objectPosition
}
Object.entries(style).forEach(([key, value]) => {
styleExpProps.push(
j.objectProperty(j.identifier(key), j.stringLiteral(value))
)
})
const styleAttribute = j.jsxAttribute(
j.jsxIdentifier('style'),
j.jsxExpressionContainer(j.objectExpression(styleExpProps))
)
attributes.push(styleAttribute)
}
// TODO: should we add `alt=""` attribute?
// We should probably let the use it manually.
j(el).replaceWith(
j.jsxElement(
j.jsxOpeningElement(
el.node.openingElement.name,
attributes,
el.node.openingElement.selfClosing
),
el.node.closingElement,
el.node.children
)
)
})
}
function nextConfigTransformer(
j: JSCodeshift,
root: Collection,
appDir: string
) {
let pathPrefix = ''
let loaderType = ''
root.find(j.ObjectExpression).forEach((o) => {
;(o.value.properties || []).forEach((images) => {
if (
images.type === 'ObjectProperty' &&
images.key.type === 'Identifier' &&
images.key.name === 'images' &&
images.value.type === 'ObjectExpression' &&
images.value.properties
) {
const properties = images.value.properties.filter((p) => {
if (
p.type === 'ObjectProperty' &&
p.key.type === 'Identifier' &&
p.key.name === 'loader' &&
'value' in p.value
) {
if (
p.value.value === 'imgix' ||
p.value.value === 'cloudinary' ||
p.value.value === 'akamai'
) {
loaderType = p.value.value
p.value.value = 'custom'
}
}
if (
p.type === 'ObjectProperty' &&
p.key.type === 'Identifier' &&
p.key.name === 'path' &&
'value' in p.value
) {
pathPrefix = String(p.value.value)
return false
}
return true
})
if (loaderType && pathPrefix) {
const importSpecifier = `./${loaderType}-loader.js`
const filePath = join(appDir, importSpecifier)
properties.push(
j.property(
'init',
j.identifier('loaderFile'),
j.literal(importSpecifier)
)
)
images.value.properties = properties
const normalizeSrc = `const normalizeSrc = (src) => src[0] === '/' ? src.slice(1) : src`
if (loaderType === 'imgix') {
writeFileSync(
filePath,
`${normalizeSrc}
export default function imgixLoader({ src, width, quality }) {
const url = new URL('${pathPrefix}' + normalizeSrc(src))
const params = url.searchParams
params.set('auto', params.getAll('auto').join(',') || 'format')
params.set('fit', params.get('fit') || 'max')
params.set('w', params.get('w') || width.toString())
if (quality) { params.set('q', quality.toString()) }
return url.href
}`
.split('\n')
.map((l) => l.trim())
.join('\n')
)
} else if (loaderType === 'cloudinary') {
writeFileSync(
filePath,
`${normalizeSrc}
export default function cloudinaryLoader({ src, width, quality }) {
const params = ['f_auto', 'c_limit', 'w_' + width, 'q_' + (quality || 'auto')]
const paramsString = params.join(',') + '/'
return '${pathPrefix}' + paramsString + normalizeSrc(src)
}`
.split('\n')
.map((l) => l.trim())
.join('\n')
)
} else if (loaderType === 'akamai') {
writeFileSync(
filePath,
`${normalizeSrc}
export default function akamaiLoader({ src, width, quality }) {
return '${pathPrefix}' + normalizeSrc(src) + '?imwidth=' + width
}`
.split('\n')
.map((l) => l.trim())
.join('\n')
)
}
}
}
})
})
return root
}
export default function transformer(
file: FileInfo,
api: API,
options: Options
) {
const j = api.jscodeshift.withParser('tsx')
const root = j(file.source)
const parsed = parse(file.path || '/')
const isConfig =
parsed.base === 'next.config.js' ||
parsed.base === 'next.config.ts' ||
parsed.base === 'next.config.mjs' ||
parsed.base === 'next.config.cjs'
if (isConfig) {
const result = nextConfigTransformer(j, root, parsed.dir)
return result.toSource()
}
// Before: import Image from "next/legacy/image"
// After: import Image from "next/image"
root
.find(j.ImportDeclaration, {
source: { value: 'next/legacy/image' },
})
.forEach((imageImport) => {
const defaultSpecifier = imageImport.node.specifiers?.find(
(node) => node.type === 'ImportDefaultSpecifier'
) as ImportDefaultSpecifier | undefined
const tagName = defaultSpecifier?.local?.name
imageImport.node.source = j.stringLiteral('next/image')
if (tagName) {
findAndReplaceProps(j, root, tagName)
}
})
// Before: const Image = await import("next/legacy/image")
// After: const Image = await import("next/image")
root.find(j.AwaitExpression).forEach((awaitExp) => {
const arg = awaitExp.value.argument
if (arg?.type === 'CallExpression' && arg.callee.type === 'Import') {
if (
arg.arguments[0].type === 'StringLiteral' &&
arg.arguments[0].value === 'next/legacy/image'
) {
arg.arguments[0] = j.stringLiteral('next/image')
}
}
})
// Before: const Image = require("next/legacy/image")
// After: const Image = require("next/image")
root.find(j.CallExpression).forEach((requireExp) => {
if (
requireExp?.value?.callee?.type === 'Identifier' &&
requireExp.value.callee.name === 'require'
) {
let firstArg = requireExp.value.arguments[0]
if (
firstArg &&
firstArg.type === 'StringLiteral' &&
firstArg.value === 'next/legacy/image'
) {
const tagName = requireExp?.parentPath?.value?.id?.name
if (tagName) {
requireExp.value.arguments[0] = j.stringLiteral('next/image')
findAndReplaceProps(j, root, tagName)
}
}
}
})
// TODO: do the same transforms for dynamic imports
return root.toSource(options)
}