Skip to content

Commit

Permalink
add codemod for vite plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Walker-GM committed May 6, 2024
1 parent 954cdac commit da7d237
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 15 deletions.
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',
)
})
})

This file was deleted.

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')
})
})
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)
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)
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()
}

0 comments on commit da7d237

Please sign in to comment.