Skip to content

Commit

Permalink
fix: codemod next/image within monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle committed Feb 17, 2023
1 parent be5a9f5 commit 52e2ced
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
images: {
loader: "imgix",
path: "https://example.com/"
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
images: {
loader: "cloudinary",
path: "https://example.com/"
},
}
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
images: {
loader: "custom",
loaderFile: "./imgix-loader.js"
},
}
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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
images: {
loader: "custom",
loaderFile: "./cloudinary-loader.js"
},
}
20 changes: 13 additions & 7 deletions packages/next-codemod/transforms/next-image-experimental.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join, parse } from 'path'
import { writeFileSync } from 'fs'
import type {
API,
Expand Down Expand Up @@ -148,7 +149,11 @@ function findAndReplaceProps(
})
}

function nextConfigTransformer(j: JSCodeshift, root: Collection) {
function nextConfigTransformer(
j: JSCodeshift,
root: Collection,
appDir: string
) {
let pathPrefix = ''
let loaderType = ''
root.find(j.ObjectExpression).forEach((o) => {
Expand Down Expand Up @@ -196,7 +201,7 @@ function nextConfigTransformer(j: JSCodeshift, root: Collection) {
const normalizeSrc = `const normalizeSrc = (src) => src[0] === '/' ? src.slice(1) : src`
if (loaderType === 'imgix') {
writeFileSync(
filename,
join(appDir, filename),
`${normalizeSrc}
export default function imgixLoader({ src, width, quality }) {
const url = new URL('${pathPrefix}' + normalizeSrc(src))
Expand Down Expand Up @@ -250,14 +255,15 @@ export default function transformer(
const j = api.jscodeshift.withParser('tsx')
const root = j(file.source)

const parsed = parse(file.path)
const isConfig =
file.path === 'next.config.js' ||
file.path === 'next.config.ts' ||
file.path === 'next.config.mjs' ||
file.path === 'next.config.cjs'
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)
const result = nextConfigTransformer(j, root, parsed.dir)
return result.toSource()
}

Expand Down

0 comments on commit 52e2ced

Please sign in to comment.