vite2next
v1.0.10
Published
Automated tool to migrate Vite projects to Next.js
Readme
vite2next
A command-line tool that automatically migrates Vite projects to Next.js following the official Next.js migration guide.
Features
- Installs Next.js dependencies
- Creates Next.js configuration file
- Updates TypeScript configuration
- Creates root layout based on your existing index.html
- Sets up entry page to maintain SPA (Single Page Application) pattern
- Converts App.css styles to Tailwind CSS classes (optional)
- Helps with static image imports
- Migrates environment variables from Vite format to Next.js
- Updates scripts in package.json
- Removes Vite-related files
- Configures compatibility with React Router
- Migrates static assets
Installation
Global Installation
npm install -g vite2nextLocal Usage (without installation)
npx vite2nextUsage
Navigate to your Vite project directory and run:
vite2nextThe tool will guide you through the migration process with interactive prompts.
Options
-y, --yes: Skip all confirmations.--skip-install: Skip dependency installation.--tailwind: Convert App.css styles to Tailwind CSS classes.[project-directory]: Specify a target directory (defaults to current directory).
Examples:
# Migrate current directory
vite2next
# Migrate a specific directory
vite2next ./my-vite-project
# Skip all confirmations
vite2next -y
# Skip dependency installation
vite2next --skip-install
# Convert App.css to Tailwind CSS
vite2next --tailwindMigration Steps
The tool follows the official Next.js migration guide and performs these steps:
- Install Next.js Dependency: Adds Next.js to the project dependencies.
- Create Next.js Configuration: Creates a
next.config.mjsfile based on Vite settings. - Update TypeScript Configuration: Adjusts your
tsconfig.jsonif you're using TypeScript. - Create Root Layout: Creates a root layout component based on your
index.html. - Create Entry Page: Sets up appropriate pages using the App Router.
- Convert App.css to Tailwind (optional): Converts App.css styles to inline Tailwind CSS classes.
- Update Static Image Imports: Adds support and helpers for image importing.
- Migrate Environment Variables: Updates your environment variables from
VITE_toNEXT_PUBLIC_. - Update Scripts in package.json: Updates scripts to use Next.js commands.
- Cleanup: Removes Vite-related files and dependencies.
- Configure React Router Compatibility: Facilitates migration from React Router to Next.js App Router.
- Migrate Static Assets: Helps properly configure static assets in Next.js.
After Migration
After migration is complete, your application will be running on Next.js as a Single Page Application. From there, you can incrementally adopt more Next.js features:
- Migrate from React Router to Next.js App Router
- Optimize images with the
<Image>component - Optimize fonts with
next/font - Optimize scripts with the
<Script>component - Add Server Components and server-side data fetching
- Add API routes
- And much more!
For detailed instructions on how to adapt your code after migration, refer to the documentation files created in the docs/ folder of your project.
Tailwind CSS Conversion
When using the --tailwind option, the tool will:
- Find App.css files in your Vite project
- Convert CSS rules to equivalent Tailwind CSS classes using the css-to-tailwindcss package
- Update components that use these CSS classes to use inline Tailwind classes
- Create a backup of your App.css file before removal
- Remove the original App.css file
This allows for a more seamless transition to Next.js with Tailwind CSS, which is a popular styling approach in the Next.js ecosystem. The css-to-tailwindcss package provides comprehensive conversion capabilities for most CSS properties, respecting your Tailwind configuration.
How to Use in Your Own Projects
To use this tool in your projects, you have several options:
As a global dependency: Install it with
npm install -g vite2nextand use in any projectAs a development dependency: Add to your project with
npm install --save-dev vite2nextRun directly: Use
npx vite2nextin any project without prior installationIntegrate into your own CLI: Import the specific modules you need:
import { updatePackageJson } from 'vite2next/api';
import { migrateDependencies } from 'vite2next/api';
import { convertAppCssToTailwind } from 'vite2next/api';
// Use in your own script
await updatePackageJson('./my-project');
await migrateDependencies('./my-project', false);
await convertAppCssToTailwind('./my-project');License
MIT
