-
Notifications
You must be signed in to change notification settings - Fork 27.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: codemod next/image
within monorepo
#46047
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
52e2ced
fix: codemod `next/image` within monorepo
styfle cbcb004
Merge branch 'canary' into fix-monorepo-image-codemod
styfle 03fed32
Update tests to handle tmpdir
styfle 2900f80
Make `toObj()` recursive
styfle 0c2abf1
Merge branch 'canary' into fix-monorepo-image-codemod
styfle bb67101
Fix case for multiple keys
styfle 7ff8af8
Merge branch 'canary' into fix-monorepo-image-codemod
kodiakhq[bot] 060b171
Merge branch 'canary' into fix-monorepo-image-codemod
kodiakhq[bot] 4654c61
Merge branch 'canary' into fix-monorepo-image-codemod
kodiakhq[bot] 3951b6f
Merge branch 'canary' into fix-monorepo-image-codemod
kodiakhq[bot] 368abe5
Merge branch 'canary' into fix-monorepo-image-codemod
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...transforms/__testfixtures__/next-image-experimental-loader/many-keys/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,21 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
module.exports = { | ||
reactStrictMode: true, | ||
|
||
images: { | ||
loader: "cloudinary", | ||
path: "https://example.com/" | ||
}, | ||
|
||
async redirects() { | ||
return [ | ||
{ | ||
source: '/source', | ||
destination: '/dest', | ||
permanent: true, | ||
}, | ||
]; | ||
}, | ||
} |
6 changes: 6 additions & 0 deletions
6
...rms/__testfixtures__/next-image-experimental-loader/many-keys/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) | ||
} |
21 changes: 21 additions & 0 deletions
21
...ransforms/__testfixtures__/next-image-experimental-loader/many-keys/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,21 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
module.exports = { | ||
reactStrictMode: true, | ||
|
||
images: { | ||
loader: "custom", | ||
loaderFile: "./cloudinary-loader.js" | ||
}, | ||
|
||
async redirects() { | ||
return [ | ||
{ | ||
source: '/source', | ||
destination: '/dest', | ||
permanent: true, | ||
}, | ||
]; | ||
}, | ||
} |
6 changes: 6 additions & 0 deletions
6
...sforms/__testfixtures__/next-image-experimental-loader/monorepo/input/app1/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/" | ||
}, | ||
} |
6 changes: 6 additions & 0 deletions
6
...sforms/__testfixtures__/next-image-experimental-loader/monorepo/input/app2/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/" | ||
}, | ||
} |
10 changes: 10 additions & 0 deletions
10
...orms/__testfixtures__/next-image-experimental-loader/monorepo/output/app1/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
...forms/__testfixtures__/next-image-experimental-loader/monorepo/output/app1/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" | ||
}, | ||
} |
6 changes: 6 additions & 0 deletions
6
...__testfixtures__/next-image-experimental-loader/monorepo/output/app2/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
...forms/__testfixtures__/next-image-experimental-loader/monorepo/output/app2/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" | ||
}, | ||
} |
26 changes: 18 additions & 8 deletions
26
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 |
---|---|---|
@@ -1,36 +1,46 @@ | ||
/* global jest */ | ||
jest.autoMockOff() | ||
const Runner = require('jscodeshift/dist/Runner'); | ||
const { cp, mkdir, rm, readdir, readFile } = require('fs/promises') | ||
const { cp, mkdir, mkdtemp, rm, readdir, readFile, stat } = require('fs/promises') | ||
const { readdirSync } = require('fs') | ||
const { tmpdir } = require('os') | ||
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 } | ||
const opts = { recursive: true, force: 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') | ||
const filePath = join(dir, file) | ||
const s = await stat(filePath) | ||
if (s.isDirectory()) { | ||
obj[file] = await toObj(filePath) | ||
} else { | ||
obj[file] = await readFile(filePath, 'utf8') | ||
} | ||
} | ||
return obj | ||
} | ||
|
||
it.each(readdirSync(fixtureDir))('should transform loader %s', async (loader) => { | ||
const tmp = await mkdtemp(join(tmpdir(), `next-image-experimental-${loader}-`)) | ||
const originalCwd = process.cwd() | ||
try { | ||
await mkdir(join(fixtureDir, 'tmp'), opts) | ||
await cp(join(fixtureDir, loader, 'input'), join(fixtureDir, 'tmp'), opts) | ||
process.chdir(join(fixtureDir, 'tmp')) | ||
await mkdir(tmp, opts) | ||
await cp(join(fixtureDir, loader, 'input'), tmp, opts) | ||
process.chdir(tmp) | ||
const result = await Runner.run(transform, [`.`], {}) | ||
expect(result.error).toBe(0) | ||
expect( | ||
await toObj(join(fixtureDir, 'tmp')) | ||
await toObj(tmp) | ||
).toStrictEqual( | ||
await toObj(join(fixtureDir, loader, 'output')) | ||
) | ||
} finally { | ||
await rm(join(fixtureDir, 'tmp'), opts) | ||
await rm(tmp, opts) | ||
process.chdir(originalCwd) | ||
} | ||
}) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:)