To set up a project from scratch using React, Tailwind CSS, and Shadcn, but without using any pre-built boilerplates like create-next-app or create-react-app, you can manually configure the setup using Webpack or other similar bundlers. Below is a guide for setting this up with Webpack:
Create a new project directory and initialize a new npm project:
mkdir my-shadcn-app cd my-shadcn-app npm init -y
Install React, ReactDOM, webpack, and webpack-dev-server:
npm install react react-dom npm install --save-dev webpack webpack-cli webpack-dev-server babel-loader @babel/core @babel/preset-env @babel/preset-react html-webpack-plugin
Install Tailwind CSS and its peer dependencies:
npm install -D tailwindcss postcss autoprefixer npx tailwindcss init
This creates the tailwind.config.js file.
Create a webpack.config.js file for configuring Webpack:
touch webpack.config.js
Inside webpack.config.js, add the following:
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/index.jsx', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, mode: 'development', module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env', '@babel/preset-react'], }, }, }, { test: /\.css$/, use: ['style-loader', 'css-loader', 'postcss-loader'], }, ], }, resolve: { extensions: ['.js', '.jsx'], }, plugins: [ new HtmlWebpackPlugin({ template: './public/index.html', }), ], devServer: { static: './dist', hot: true, }, };
Create a .babelrc file for Babel configuration:
touch .babelrc
Inside .babelrc, add the following:
{ "presets": ["@babel/preset-env", "@babel/preset-react"] }
Update your tailwind.config.js file to include the paths to your components:
/** @type {import('tailwindcss').Config} */ module.exports = { content: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'], theme: { extend: {}, }, plugins: [], };
Create the necessary folders and files for your React application:
mkdir src public touch src/index.jsx src/App.jsx src/index.css public/index.html
My Shadcn App
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; import './index.css'; ReactDOM.render(, document.getElementById('root'));
import React from 'react'; const App = () => { return (); }; export default App;Hello Shadcn!
@tailwind base; @tailwind components; @tailwind utilities;
Now that you have the basic setup, install the Shadcn package and its components. Install the Shadcn CLI and Tailwind components for your React project:
npx shadcn-init
Follow the on-screen instructions to install components and generate the Shadcn library for your project.
Update your package.json to add a start script to run the Webpack development server:
"scripts": { "start": "webpack serve --open" }
Run the development server with:
npm start
This should open up your app in the browser, and you'll see "Hello Shadcn!" styled with Tailwind CSS. You can now continue adding Shadcn components to your React project.
(Generated with AI assistance)
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3