Giter Club home page Giter Club logo

Comments (1)

petermasking avatar petermasking commented on May 27, 2024

I've managed to create a Vite plugin that works in dev mode, but can build a fully shippable package. To test the plugin follow next steps to create a simple React app. You can also use Vue or Angular if preferred.

Step 1
Register the local version of Jitar and the plugin.

cd path/to/jitar-repo/jitar
tsc
npm link

cd ../plugins/vite
tsc
npm link

step 2
Setup a basic React app with Vite.

cd path/to/some/projects
npm create -y vite@latest jitar-react --template react-ts
cd jitar-react
npm install

Step 3
Add Jitar and the plugin to the app.

npm link jitar jitar-vite-plugin

Add the plugin to the Vite config (project root).

vite.config.ts

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import jitar from 'jitar-vite-plugin'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), jitar('./src', 'shared', 'http://localhost:3000')]
})

And add a Jitar config file to the root of the project.

jitar.json

{
    "url": "http://127.0.0.1:3000",
    "standalone":
    {
        "source": "./dist",
        "cache": "./cache",
        "index": "index.html"
    }
}

Step 4
Extend the app with an procedure.

First create a shared folder into the src folder, and create a sayHello.ts file in the shared folder with the following content.

export default async function sayHello(name: string): Promise<string>
{
    return `Hello, ${name}!`;
}

Call the procedure from the App react component.

src/App.tsx

import { useState } from 'react'
import reactLogo from './assets/react.svg'
import './App.css'
import sayHello from './shared/sayHello'

const message = await sayHello('Vite + React + Jitar')

function App()
{
  const [count, setCount] = useState(0)

  return (
    <div className="App">
      <!-- Only add the message to the heading -->
      <h1>{message}</h1>
      <!-- The rest is left out this example -->
    </div>
  )
}

export default App

And create a segment file for the procedure.

default.segment.json

{
    "./shared/sayHello.js": { "default": { "access": "public" } }
}

Step 5
Running and building.

Create a bootstrap script for Jitar in the src folder.

import { startServer } from 'jitar';

const moduleImporter = async (specifier: string) => import(specifier);

startServer(moduleImporter);

Update the tsconfig file to emit the transpiled code to the dist folder.

{
  "compilerOptions": {
    "...", "...",
    "noEmit": false,
    "outDir": "dist",
    "...", "..."
  },
  "...", "..."
}

Add a script to the package.json to run Jitar, and reverse the build order.

{
  "...": "...",
  "scripts": {
    "dev": "vite",
    "jitar": "node --experimental-network-imports --experimental-fetch dist/jitar.js --config=jitar.json",
    "build": "vite build && tsc",
    "preview": "vite preview"
  },
  "...": "..."
}

The app needs to be build before starting Jitar.

npm run build
npm run jitar

The build app should now be available under http://localhost:3000.

The app can also run in dev mode (in a separate terminal, don't stop Jitar).

npm run dev

The build app should also be available under http://localhost:5173.

from jitar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.