Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/vite static site #825

Merged
merged 8 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"build": "yarn run build:tsc && yarn run build:vite",
"build:tsc": "tsc || exit 0",
"build:vite": "vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
Expand Down
50 changes: 45 additions & 5 deletions apps/vite/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { tamaguiExtractPlugin, tamaguiPlugin } from '@tamagui/vite-plugin';
import react from '@vitejs/plugin-react-swc';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import { resolve } from 'path';
import esbuildFlowPlugin from 'esbuild-plugin-flow';
import { TanStackRouterVite } from '@tanstack/router-vite-plugin';
import { tamaguiExtractPlugin, tamaguiPlugin } from '@tamagui/vite-plugin';
import * as esbuild from 'esbuild';
import { readFileSync } from 'fs';

const shouldExtract = process.env.EXTRACT === '1';

Expand All @@ -14,6 +16,7 @@ const tamaguiConfig = {

// https://tamagui.dev/docs/intro/installation
const extensions = [
'.mjs',
'.web.tsx',
'.tsx',
'.web.ts',
Expand All @@ -30,8 +33,19 @@ const extensions = [

const development = process.env.NODE_ENV === 'development';

const rollupPlugin = (matchers: RegExp[]) => ({
name: 'js-in-jsx',
load(id: string) {
if (matchers.some((matcher) => matcher.test(id)) && id.endsWith('.js')) {
const file = readFileSync(id, { encoding: 'utf-8' });
return esbuild.transformSync(file, { loader: 'jsx', jsx: 'automatic' });
}
},
});

export default defineConfig({
clearScreen: true,
cacheDir: '../../node_modules/.vite/vite-app',
plugins: [
react(),
TanStackRouterVite(),
Expand Down Expand Up @@ -69,13 +83,39 @@ export default defineConfig({
// https://github.com/vitejs/vite-plugin-react/issues/192#issuecomment-1627384670
jsx: 'automatic',
// need either this or the plugin below
loader: { '.js': 'jsx' },
loader: {
'.js': 'jsx'
},
plugins: [
esbuildFlowPlugin(/\.(flow|jsx?)$/, (path) =>
/\.jsx$/.test(path) ? 'jsx' : 'jsx',
esbuildFlowPlugin(
/\.(flow|jsx?)$/,
(path) => (/\.jsx$/.test(path) ? 'jsx' : 'jsx'),
),
],
},
include: ['@packrat/validations'],
exclude: [],
},
build: {
commonjsOptions: { transformMixedEsModules: true },
rollupOptions: {
plugins: [
rollupPlugin([/react-native-vector-icons/, /@expo\/vector-icons/, /react-native-table-component/]),
],
},
},
server: {
port: 4200,
host: 'localhost',
},
preview: {
port: 4300,
host: 'localhost',
},
test: {
globals: true,
cache: { dir: '../../node_modules/.vitest' },
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
});
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
"build:expo": "yarn expo:export",
"build:ios": "cd apps/expo && yarn build:ios",
"build:prod:docker": "docker build -t packrat-app -f Dockerfile.prod .",
"build:prod": "yarn && yarn build:tsc && yarn build:next:static",
"build:prod": "yarn && yarn build:tsc && yarn build:web",
"build:prod:cloudflare-next": "cd apps/next && yarn build:cloudflare",
"build:tsc:packages": "cd packages/validations && yarn build",
"build:tsc:server": "cd server && yarn build",
"build:tsc": "yarn build:tsc:packages",
"build:validations": "cd packages/validations && yarn build",
"build:web": "cd apps/next && yarn build",
"build:web": "yarn build:vite && yarn build:next:static",
"build:next:static": "cd apps/next && yarn build && yarn next export",
"build:vite": "cd apps/vite && yarn build",
"build": "yarn workspaces foreach --all run build",
"check-deps": "check-dependency-version-consistency .",
"check-deps-fix": "yarn check-deps --fix --ignore-dep uuid",
Expand Down
60 changes: 60 additions & 0 deletions packages/cli/src/doctor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { prompt } = require('enquirer');
const fs = require('fs');
const path = require('path');

// Adjust this path if your script is not located in the root of PackRat
const appsPath = path.join(__dirname, '../../../apps');

async function checkConfigFiles(appName, configFileNames) {
console.log(`\nChecking ${appName}...`);
const appDir = path.join(appsPath, appName);

console.log('appDir:', appDir);

for (const fileName of configFileNames) {
const filePath = path.join(appDir, fileName);
if (fs.existsSync(filePath)) {
console.log(`✅ ${fileName} exists.`);
} else {
console.error(`❌ ${fileName} does not exist.`);
}
}
}

async function checkPackageJsonDependencies(appName) {
const packageJsonPath = path.join(appsPath, appName, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
console.error('❌ package.json does not exist.');
return;
}

const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
console.log(`Dependencies for ${appName}:`);
console.log(packageJson.dependencies);
// Implement additional logic for version checks as needed
}

async function runDiagnostics() {
const response = await prompt([
{
type: 'select',
name: 'appName',
message: 'Select an app to diagnose:',
choices: ['bun-server', 'expo', 'next', 'tauri', 'vite'],
}
]);

const configFileNames = {
'expo': ['app.json'],
'next': ['next.config.js'],
'vite': ['vite.config.js'],
// Assuming generic checks for Bun and Tauri, adjust based on real config files
'bun-server': ['package.json'],
'tauri': ['package.json'],
};

await checkConfigFiles(response.appName, configFileNames[response.appName]);
await checkPackageJsonDependencies(response.appName);
}

runDiagnostics().catch(console.error);
14 changes: 9 additions & 5 deletions server/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,26 @@ router.route('/hello', helloRouter);
// // Static routes for serving the React Native Web app
// if (process.env.NODE_ENV === 'production') {
// const __dirname = path.resolve();
// const serverType = process.env.REACT_APP_SERVER_TYPE || 'vite';

// // Serve the client's index.html file at the root route
// router.get('/', (req, res) => {
// // Attach the CSRF token cookie to the response
// // Attach the CSRF token cookie to the response
// // res.cookie("XSRF-TOKEN", req.csrfToken());

// res.sendFile(path.resolve(__dirname, '../apps/next', 'out', 'index.html'));
// const basePath = serverType === 'next' ? '../apps/next/out' : '../apps/vite/dist';
// res.sendFile(path.resolve(__dirname, basePath, 'index.html'));
// });

// // Serve the static assets from the client's dist app
// router.use(express.static(path.join(__dirname, '../apps/next/out')));
// // Serve the static assets
// const staticPath = serverType === 'next' ? '../apps/next/out' : '../apps/vite/dist';
// router.use(express.static(path.join(__dirname, staticPath)));

// // Serve the client's index.html file at all other routes NOT starting with /api
// router.get(/^(?!\/?api).*/, (req, res) => {
// // res.cookie("XSRF-TOKEN", req.csrfToken());
// res.sendFile(path.resolve(__dirname, '../apps/next', 'out', 'index.html'));
// const basePath = serverType === 'next' ? '../apps/next/out' : '../apps/vite/dist';
// res.sendFile(path.resolve(__dirname, basePath, 'index.html'));
// });
// }

Expand Down
Loading