Unverified Commit 0354e5c1 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Delete obsolete codemod `next-dynamic-access-named-export` (#72016)

parent 63b66bb8
...@@ -189,46 +189,6 @@ export function GET(req: NextRequest) { ...@@ -189,46 +189,6 @@ export function GET(req: NextRequest) {
} }
``` ```
#### Transform `next/dynamic` imports accessing named exports to return an object with a `default` property
##### `next-dynamic-access-named-export`
```bash filename="Terminal"
npx @next/codemod@latest next-dynamic-access-named-export .
```
This codemod transforms dynamic imports using `next/dynamic` to ensure they return an object with a `default` property when accessing named exports. This aligns the behavior with `React.lazy` and addresses issues with accessing named exports from client components in server components.
For example:
```js
import dynamic from 'next/dynamic'
const ComponentA = dynamic(() =>
import('../components/a').then((mod) => mod.default)
)
const ComponentB = dynamic(() =>
import('../components/b').then((mod) => mod.ComponentB)
)
```
Transforms into:
```js
import dynamic from 'next/dynamic'
const ComponentA = dynamic(() =>
import('../components/a').then((mod) => ({ default: mod.default }))
)
const ComponentB = dynamic(() =>
import('../components/b').then((mod) => ({ default: mod.ComponentB }))
)
```
> **Good to know**: This codemod only affects dynamic imports using `next/dynamic` and access named exports.
### 14.0 ### 14.0
#### Migrate `ImageResponse` imports #### Migrate `ImageResponse` imports
......
suggests `next-dynamic-access-named-export` transform Prompts for React 19 upgrade with a recommendation to do so
Suggests adding `--turbopack` to `next dev` script
Suggests `app-dir-runtime-config-experimental-edge` transform
Suggests `next-async-request-api` transform
Suggests `next-request-geo-ip` transform
{
"name": "next-15-canary-43-installed",
"scripts": {
"dev": "next dev"
},
"dependencies": {
"next": "15.0.0-canary.43",
"react": "18.2.0",
"react-dom": "18.2.0"
}
}
does not suggest `next-dynamic-access-named-export` transform
{
"name": "next-15-canary-44-installed",
"scripts": {
"dev": "next dev"
},
"dependencies": {
"next": "15.0.0-canary.44",
"react": "18.2.0",
"react-dom": "18.2.0"
}
}
...@@ -99,12 +99,6 @@ export const TRANSFORMER_INQUIRER_CHOICES = [ ...@@ -99,12 +99,6 @@ export const TRANSFORMER_INQUIRER_CHOICES = [
value: 'next-og-import', value: 'next-og-import',
version: '14.0.0', version: '14.0.0',
}, },
{
title:
'Transform `next/dynamic` imports accessing named exports to return an object with a `default` property',
value: 'next-dynamic-access-named-export',
version: '15.0.0-canary.44',
},
{ {
title: title:
'Install `@vercel/functions` to replace `geo` and `ip` properties on `NextRequest`', 'Install `@vercel/functions` to replace `geo` and `ip` properties on `NextRequest`',
......
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(
() => import('./component').then(mod => {
return mod.default;
})
)
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(
() => import('./component').then(mod => {
return {
default: mod.default
};
})
)
import type { JSX } from 'react'
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(
() => import('./component').then(mod => {
return mod.default;
})
)
import type { JSX } from 'react'
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(
() => import('./component').then(mod => {
return {
default: mod.default
};
})
)
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(
() => import('./component').then(mod => mod.Component)
)
\ No newline at end of file
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(
() => import('./component').then(mod => ({
default: mod.Component
}))
)
\ No newline at end of file
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(
() => import('./component')
)
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(
() => import('./component')
)
import dynamic from 'my-dynamic-call'
const DynamicComponent = dynamic(
() => import('./component').then(mod => {
return mod.Component;
})
)
import dynamic from 'my-dynamic-call'
const DynamicComponent = dynamic(
() => import('./component').then(mod => {
return mod.Component;
})
)
import dynamic from 'next/dynamic'
const DynamicImportSourceNextDynamic1 = dynamic(() => import(source).then(mod => mod))
const DynamicImportSourceNextDynamic2 = dynamic(async () => {
const mod = await import(source)
return mod.Component
})
import dynamic from 'next/dynamic'
const DynamicImportSourceNextDynamic1 = dynamic(() => import(source).then(mod => mod))
const DynamicImportSourceNextDynamic2 = dynamic(async () => {
const mod = await import(source)
return mod.Component
})
/* global jest */
jest.autoMockOff()
const defineTest = require('jscodeshift/dist/testUtils').defineTest
const { readdirSync } = require('fs')
const { join } = require('path')
const fixtureDir = 'next-dynamic-access-named-export'
const fixtureDirPath = join(__dirname, '..', '__testfixtures__', fixtureDir)
const fixtures = readdirSync(fixtureDirPath)
.filter(file => /\.input\.(js|tsx)$/.test(file))
for (const file of fixtures) {
const fixture = file.replace(/\.input\.(js|tsx)/, '');
const isTsx = file.endsWith('.tsx')
const prefix = `${fixtureDir}/${fixture}`;
defineTest(__dirname, fixtureDir, null, prefix, { parser: isTsx ? 'tsx' : 'babel' });
}
import type { FileInfo, API, ImportDeclaration } from 'jscodeshift'
import { createParserFromPath } from '../lib/parser'
export default function transformer(file: FileInfo, _api: API) {
const j = createParserFromPath(file.path)
const root = j(file.source)
// Find the import declaration for 'next/dynamic'
const dynamicImportDeclaration = root.find(j.ImportDeclaration, {
source: { value: 'next/dynamic' },
})
// If the import declaration is found
if (dynamicImportDeclaration.size() > 0) {
const importDecl: ImportDeclaration = dynamicImportDeclaration.get(0).node
const dynamicImportName = importDecl.specifiers?.[0]?.local?.name
if (!dynamicImportName) {
return file.source
}
// Find call expressions where the callee is the imported 'dynamic'
root
.find(j.CallExpression, {
callee: { name: dynamicImportName },
})
.forEach((path) => {
const arrowFunction = path.node.arguments[0]
// Ensure the argument is an ArrowFunctionExpression
if (arrowFunction && arrowFunction.type === 'ArrowFunctionExpression') {
const importCall = arrowFunction.body
// Ensure the parent of the import call is a CallExpression with a .then
if (
importCall &&
importCall.type === 'CallExpression' &&
importCall.callee.type === 'MemberExpression' &&
'name' in importCall.callee.property &&
importCall.callee.property.name === 'then'
) {
const thenFunction = importCall.arguments[0]
// handle case of block statement case `=> { return mod.Component }`
// transform to`=> { return { default: mod.Component } }`
if (
thenFunction &&
thenFunction.type === 'ArrowFunctionExpression' &&
thenFunction.body.type === 'BlockStatement'
) {
const returnStatement = thenFunction.body.body[0]
// Ensure the body of the arrow function has a return statement with a MemberExpression
if (
returnStatement &&
returnStatement.type === 'ReturnStatement' &&
returnStatement.argument?.type === 'MemberExpression'
) {
returnStatement.argument = j.objectExpression([
j.property(
'init',
j.identifier('default'),
returnStatement.argument
),
])
}
}
// handle case `=> mod.Component`
// transform to`=> ({ default: mod.Component })`
if (
thenFunction &&
thenFunction.type === 'ArrowFunctionExpression' &&
thenFunction.body.type === 'MemberExpression'
) {
thenFunction.body = j.objectExpression([
j.property('init', j.identifier('default'), thenFunction.body),
])
}
}
}
})
}
return root.toSource()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment