-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
954cdac
commit da7d237
Showing
7 changed files
with
155 additions
and
15 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
packages/cli/src/commands/setup/middleware/ogImage/__codemod_tests__/middleware.ts
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,18 @@ | ||
import { describe, it } from 'vitest' | ||
|
||
describe('Middleware codemod', () => { | ||
it('Handles the default TSX case', async () => { | ||
await matchTransformSnapshot('codemodMiddleware', 'defaultTsx') | ||
}) | ||
|
||
it('Handles when OgImageMiddleware is already imported', async () => { | ||
await matchTransformSnapshot('codemodMiddleware', 'alreadyContainsImport') | ||
}) | ||
|
||
it('Handles when registerMiddleware function is already defined', async () => { | ||
await matchTransformSnapshot( | ||
'codemodMiddleware', | ||
'registerFunctionAlreadyDefined', | ||
) | ||
}) | ||
}) |
15 changes: 0 additions & 15 deletions
15
packages/cli/src/commands/setup/middleware/ogImage/__codemod_tests__/ogImage.ts
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
packages/cli/src/commands/setup/middleware/ogImage/__codemod_tests__/vitePlugin.ts
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,7 @@ | ||
import { describe, it } from 'vitest' | ||
|
||
describe('Vite plugin codemod', () => { | ||
it('Handles the default vite config case', async () => { | ||
await matchTransformSnapshot('codemodVitePlugin', 'defaultViteConfig') | ||
}) | ||
}) |
19 changes: 19 additions & 0 deletions
19
...es/cli/src/commands/setup/middleware/ogImage/__testfixtures__/defaultViteConfig.input.tsx
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,19 @@ | ||
import dns from 'dns' | ||
|
||
import type { UserConfig } from 'vite' | ||
import { defineConfig } from 'vite' | ||
|
||
import redwood from '@redwoodjs/vite' | ||
|
||
// So that Vite will load on localhost instead of `127.0.0.1`. | ||
// See: https://vitejs.dev/config/server-options.html#server-host. | ||
dns.setDefaultResultOrder('verbatim') | ||
|
||
const viteConfig: UserConfig = { | ||
plugins: [redwood()], | ||
optimizeDeps: { | ||
force: true, | ||
}, | ||
} | ||
|
||
export default defineConfig(viteConfig) |
20 changes: 20 additions & 0 deletions
20
...s/cli/src/commands/setup/middleware/ogImage/__testfixtures__/defaultViteConfig.output.tsx
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,20 @@ | ||
import vitePluginOgImageGen from '@redwoodjs/ogimage-gen/plugin' | ||
import dns from 'dns' | ||
|
||
import type { UserConfig } from 'vite' | ||
import { defineConfig } from 'vite' | ||
|
||
import redwood from '@redwoodjs/vite' | ||
|
||
// So that Vite will load on localhost instead of `127.0.0.1`. | ||
// See: https://vitejs.dev/config/server-options.html#server-host. | ||
dns.setDefaultResultOrder('verbatim') | ||
|
||
const viteConfig: UserConfig = { | ||
plugins: [redwood(), vitePluginOgImageGen()], | ||
optimizeDeps: { | ||
force: true, | ||
}, | ||
} | ||
|
||
export default defineConfig(viteConfig) |
File renamed without changes.
91 changes: 91 additions & 0 deletions
91
packages/cli/src/commands/setup/middleware/ogImage/codemodVitePlugin.ts
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,91 @@ | ||
import type { FileInfo, API } from 'jscodeshift' | ||
|
||
export default function transform(file: FileInfo, api: API) { | ||
const j = api.jscodeshift | ||
const ast = j(file.source) | ||
|
||
// Insert `import vitePluginOgImageGen from '@redwoodjs/ogimage-gen/plugin'` at the top of the file | ||
const needsImport = | ||
ast.find(j.ImportDeclaration, { | ||
specifiers: [ | ||
{ | ||
type: 'ImportDefaultSpecifier', | ||
local: { | ||
name: 'vitePluginOgImageGen', | ||
}, | ||
}, | ||
], | ||
source: { | ||
value: '@redwoodjs/ogimage-gen/plugin', | ||
type: 'StringLiteral', | ||
}, | ||
}).length === 0 | ||
if (needsImport) { | ||
ast | ||
.find(j.ImportDeclaration) | ||
.at(0) | ||
.insertBefore( | ||
j.importDeclaration( | ||
[j.importDefaultSpecifier(j.identifier('vitePluginOgImageGen'))], | ||
j.stringLiteral('@redwoodjs/ogimage-gen/plugin'), | ||
), | ||
) | ||
} | ||
|
||
// Find the `viteConfig` variable | ||
const viteConfigVariable = ast.find(j.VariableDeclaration, { | ||
declarations(value) { | ||
if (value.length !== 1) { | ||
return false | ||
} | ||
|
||
const declaration = value[0] | ||
if (declaration.type !== 'VariableDeclarator') { | ||
return false | ||
} | ||
|
||
return ( | ||
declaration.id.type === 'Identifier' && | ||
declaration.id.name === 'viteConfig' | ||
) | ||
}, | ||
}) | ||
|
||
if (viteConfigVariable.length === 0) { | ||
throw new Error('Could not find the `viteConfig` variable') | ||
} | ||
|
||
// Find the `plugins` array in the `viteConfig` variable | ||
const pluginsArray = viteConfigVariable.find(j.ObjectExpression, { | ||
properties(value) { | ||
if (!value) { | ||
return false | ||
} | ||
|
||
return value.some( | ||
(property) => | ||
property.type === 'ObjectProperty' && | ||
property.key.type === 'Identifier' && | ||
property.key.name === 'plugins', | ||
) | ||
}, | ||
}) | ||
|
||
if (pluginsArray.length === 0) { | ||
throw new Error( | ||
'Could not find the `plugins` array in the `viteConfig` variable', | ||
) | ||
} | ||
|
||
// Add `vitePluginOgImageGen()` to the `plugins` array | ||
pluginsArray | ||
.find(j.ArrayExpression) | ||
.at(0) | ||
.replaceWith((nodePath) => { | ||
const elements = nodePath.value.elements | ||
elements.push(j.callExpression(j.identifier('vitePluginOgImageGen'), [])) | ||
return nodePath.value | ||
}) | ||
|
||
return ast.toSource() | ||
} |