danieljmann96 / friends-wordle

0 stars 0 forks source link

Cool, do this #9

Open danieljmann96 opened 7 months ago

danieljmann96 commented 7 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 7 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 7 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}
); }

danieljmann96 commented 2 months ago

https://ticketing.wembleystadium.com/performance/365c414d-ec49-420d-b987-35c2b5cf0c33

danieljmann96 commented 2 months ago

https://ticketing.wembleystadium.com/performance/564c64c7-c6ef-49e2-a3ae-aaf36c215f80

danieljmann96 commented 1 month ago

import React, { useEffect } from 'react'; // Import the worker using Vite's special import syntax for Web Workers const MyWorker = new Worker(new URL('../my-worker.ts', import.meta.url), { type: 'module' }); const MyComponent = () => { useEffect(() => { MyWorker.postMessage('Hello from React!'); MyWorker.onmessage = (event) => { console.log('Message from worker:', event.data); }; return () => { MyWorker.terminate(); // Clean up the worker when component unmounts }; }, []); return

Check the console for messages from the Web Worker.
; }; export default MyComponent;

danieljmann96 commented 1 month ago

import { defineConfig } from 'vite'; import { VitePWA } from 'vite-plugin-pwa'; export default defineConfig({ plugins: [ VitePWA({ strategies: 'injectManifest', srcDir: 'src', filename: 'service-worker.ts', // Your TypeScript service worker injectManifest: { globPatterns: ['*/.{js,css,html,ico,png,svg}'] } }) ], build: { // Ensure TypeScript gets processed correctly in the service worker rollupOptions: { input: './src/service-worker.ts', }, }, });