-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
next-image-experimental
codemod to handle loaders (#41633)
As a follow up to #41585, this PR updates the `next-image-experimental` codemod to change `loader` to `loaderFile`.
- Loading branch information
Showing
12 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...od/transforms/__testfixtures__/next-image-experimental-loader/akamai/input/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
images: { | ||
loader: "akamai", | ||
path: "https://example.com/", | ||
}, | ||
} |
4 changes: 4 additions & 0 deletions
4
...transforms/__testfixtures__/next-image-experimental-loader/akamai/output/akamai-loader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const normalizeSrc = (src) => src[0] === '/' ? src.slice(1) : src | ||
export default function akamaiLoader({ src, width, quality }) { | ||
return 'https://example.com/' + normalizeSrc(src) + '?imwidth=' + width | ||
} |
6 changes: 6 additions & 0 deletions
6
...d/transforms/__testfixtures__/next-image-experimental-loader/akamai/output/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
images: { | ||
loader: "custom", | ||
loaderFile: "./akamai-loader.js", | ||
}, | ||
} |
6 changes: 6 additions & 0 deletions
6
...ransforms/__testfixtures__/next-image-experimental-loader/cloudinary/input/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
images: { | ||
loader: "cloudinary", | ||
path: "https://example.com/", | ||
}, | ||
} |
6 changes: 6 additions & 0 deletions
6
...ms/__testfixtures__/next-image-experimental-loader/cloudinary/output/cloudinary-loader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const normalizeSrc = (src) => src[0] === '/' ? src.slice(1) : src | ||
export default function cloudinaryLoader({ src, width, quality }) { | ||
const params = ['f_auto', 'c_limit', 'w_' + width, 'q_' + (quality || 'auto')] | ||
const paramsString = params.join(',') + '/' | ||
return 'https://example.com/' + paramsString + normalizeSrc(src) | ||
} |
6 changes: 6 additions & 0 deletions
6
...ansforms/__testfixtures__/next-image-experimental-loader/cloudinary/output/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
images: { | ||
loader: "custom", | ||
loaderFile: "./cloudinary-loader.js", | ||
}, | ||
} |
6 changes: 6 additions & 0 deletions
6
...mod/transforms/__testfixtures__/next-image-experimental-loader/imgix/input/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
images: { | ||
loader: "imgix", | ||
path: "https://example.com/", | ||
}, | ||
} |
10 changes: 10 additions & 0 deletions
10
...d/transforms/__testfixtures__/next-image-experimental-loader/imgix/output/imgix-loader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const normalizeSrc = (src) => src[0] === '/' ? src.slice(1) : src | ||
export default function imgixLoader({ src, width, quality }) { | ||
const url = new URL('https://example.com/' + 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 | ||
} |
6 changes: 6 additions & 0 deletions
6
...od/transforms/__testfixtures__/next-image-experimental-loader/imgix/output/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
images: { | ||
loader: "custom", | ||
loaderFile: "./imgix-loader.js", | ||
}, | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/next-codemod/transforms/__tests__/next-image-experimental-loader.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* global jest */ | ||
jest.autoMockOff() | ||
const Runner = require('jscodeshift/dist/Runner'); | ||
const { cp, mkdir, rm, readdir, readFile } = require('fs/promises') | ||
const { join } = require('path') | ||
|
||
const fixtureDir = join(__dirname, '..', '__testfixtures__', 'next-image-experimental-loader') | ||
const transform = join(__dirname, '..', 'next-image-experimental.js') | ||
const opts = { recursive: true } | ||
|
||
async function toObj(dir) { | ||
const obj = {} | ||
const files = await readdir(dir) | ||
for (const file of files) { | ||
obj[file] = await readFile(join(dir, file), 'utf8') | ||
} | ||
return obj | ||
} | ||
it.each(['imgix', 'cloudinary', 'akamai'])('should transform loader %s', async (loader) => { | ||
try { | ||
await mkdir(join(fixtureDir, 'tmp'), opts) | ||
await cp(join(fixtureDir, loader, 'input'), join(fixtureDir, 'tmp'), opts) | ||
process.chdir(join(fixtureDir, 'tmp')) | ||
const result = await Runner.run(transform, [`.`], {}) | ||
expect(result.error).toBe(0) | ||
expect( | ||
await toObj(join(fixtureDir, 'tmp')) | ||
).toStrictEqual( | ||
await toObj(join(fixtureDir, loader, 'output')) | ||
) | ||
} finally { | ||
await rm(join(fixtureDir, 'tmp'), opts) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters