| 2 | import { createParserFromPath } from '../lib/parser' |
| 3 | |
| 4 | export default function transformer(file: FileInfo, _api: API) { |
| 5 | // Run on App Router page/layout/route files, except for test environment. |
| 6 | if ( |
| 7 | process.env.NODE_ENV !== 'test' && |
| 8 | !/[/\\]app[/\\](?:.*[/\\])?(page|layout|route)(\.[^/\\]*)?$/.test(file.path) |
| 9 | ) { |
| 10 | return file.source |
| 11 | } |
| 12 | |
| 13 | const j = createParserFromPath(file.path) |
| 14 | const root = j(file.source) |
| 15 | |
| 16 | let hasChanges = false |
| 17 | |
| 18 | // Remove export const experimental_ppr = boolean |
| 19 | const directExports = root.find(j.ExportNamedDeclaration, { |
| 20 | declaration: { |
| 21 | type: 'VariableDeclaration', |
| 22 | declarations: [ |
| 23 | { |
| 24 | id: { name: 'experimental_ppr' }, |
| 25 | }, |
| 26 | ], |
| 27 | }, |
| 28 | }) |
| 29 | |
| 30 | if (directExports.size() > 0) { |
| 31 | directExports.remove() |
| 32 | hasChanges = true |
| 33 | } |
| 34 | |
| 35 | // Remove const experimental_ppr = boolean declarations |
| 36 | const variableDeclarations = root.find(j.VariableDeclaration).filter((path) => |
| 37 | path.node.declarations.some((decl) => { |
| 38 | if (j.VariableDeclarator.check(decl) && j.Identifier.check(decl.id)) { |
| 39 | return decl.id.name === 'experimental_ppr' |
| 40 | } |
| 41 | return false |
| 42 | }) |
| 43 | ) |
| 44 | |
| 45 | if (variableDeclarations.size() > 0) { |
| 46 | variableDeclarations.remove() |
| 47 | hasChanges = true |
| 48 | } |
| 49 | |
| 50 | // Handle export { experimental_ppr } and export { experimental_ppr, other } |
| 51 | const namedExports = root |
| 52 | .find(j.ExportNamedDeclaration) |
| 53 | .filter((path) => path.node.specifiers && path.node.specifiers.length > 0) |
| 54 | |
| 55 | namedExports.forEach((path) => { |
| 56 | const specifiers = path.node.specifiers |
| 57 | if (!specifiers) return |
| 58 | |
| 59 | const filteredSpecifiers = specifiers.filter((spec) => { |
| 60 | if (j.ExportSpecifier.check(spec) && j.Identifier.check(spec.local)) { |
| 61 | return spec.local.name !== 'experimental_ppr' |