danieljmann96 / friends-wordle

0 stars 0 forks source link

Cool, do this #9

Open danieljmann96 opened 4 months ago

danieljmann96 commented 4 months ago

import { defineConfig } from 'vite'; import tsToJsPlugin from './path/to/vite-plugin-ts-to-js'; // Adjust the path accordingly export default defineConfig({ plugins: [ tsToJsPlugin(), // Apply the custom plugin ], // Include the TypeScript file in the build process src: './path/to/your/ts/file/example.ts', });

// vite-plugin-ts-to-js.js const ts = require('typescript'); const fs = require('fs'); const path = require('path'); module.exports = function tsToJsPlugin() { return { name: 'ts-to-js', async transform(code, id) { if (id.endsWith('.ts')) { // Transpile TypeScript to JavaScript const transpiled = ts.transpileModule(code, { compilerOptions: { target: ts.ScriptTarget.ESNext, // or any other target you prefer module: ts.ModuleKind.ESNext, // or any other module format you prefer }, }); // Get the filename without the extension const fileName = path.basename(id, '.ts'); // Write the transpiled code to a new JavaScript file fs.writeFileSync(path.join(path.dirname(id), ${fileName}.js), transpiled.outputText); // Return an empty object to indicate that the file is handled return {}; } }, }; };

danieljmann96 commented 4 months ago

// vite.config.js import { defineConfig } from 'vite'; import tsToJsPlugin from './path/to/vite-plugin-ts-to-js'; // Adjust the path accordingly import fs from 'fs'; import path from 'path'; let builtFilePath = ''; // Variable to store the path of the built JavaScript file export default defineConfig({ plugins: [ tsToJsPlugin(), // Apply the custom plugin ], // Include the TypeScript file in the build process src: './path/to/your/ts/file/example.ts', // Hook into the Vite server lifecycle server: { // Called when the Vite server is stopped stop() { // Perform cleanup action to delete the built JavaScript file if (builtFilePath) { fs.unlinkSync(builtFilePath); console.log(Deleted ${builtFilePath}); } }, }, // Hook into the Vite build lifecycle build: { // Called after each build onEnd() { // Save the path of the built JavaScript file for cleanup builtFilePath = path.resolve(__dirname, 'dist', 'example.js'); // Adjust the path and filename accordingly }, }, });

danieljmann96 commented 4 months ago

// service-worker.js self.addEventListener('message', event => { console.log('Message received in service worker:', event.data); // Process the received message (e.g., transform it, fetch data, etc.) const processedMessage = event.data.toUpperCase(); // Send a response back to the client self.clients.matchAll().then(clients => { clients.forEach(client => { client.postMessage(processedMessage); }); }); });

import React, { useState, useEffect } from 'react'; function MessagingComponent() { const [inputValue, setInputValue] = useState(''); const [receivedMessage, setReceivedMessage] = useState(''); useEffect(() => { // Register the service worker if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/service-worker.js') .then(registration => { console.log('Service worker registered:', registration); // Listen for messages from the service worker navigator.serviceWorker.addEventListener('message', event => { console.log('Message received from service worker:', event.data); setReceivedMessage(event.data); }); }) .catch(error => { console.error('Service worker registration failed:', error); }); } else { console.error('Service workers are not supported.'); } }, []); const sendMessageToServiceWorker = () => { if ('serviceWorker' in navigator && navigator.serviceWorker.controller) { // Send message to service worker navigator.serviceWorker.controller.postMessage(inputValue); setInputValue(''); } else { console.error('No service worker available to send message.'); } }; return (

<input type="text" value={inputValue} onChange={(e) => setInputValue(e.target.value)} placeholder="Type a message..." />
Received Message: {receivedMessage}
); }