-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: codemod
next/image
within monorepo (#46047)
Fix for running the `next/image` codemod within a monorepo Related to https://twitter.com/codercatdev/status/1625972398448078848 --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
2b18d5d
commit b954bbe
Showing
11 changed files
with
188 additions
and
78 deletions.
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