klap icon indicating copy to clipboard operation
klap copied to clipboard

Programmable API

Open v1rtl opened this issue 4 years ago • 1 comments

Would be great to havea programmable API to use Klap for SSR apps.

something like this would be great:

import { App } from '@tinyhttp/app'
import { Bundler } from 'klap'

const isProd = process.env.NODE_ENV === 'production'

const app = new App()

const bundler = new Bundler({ source: 'index.html', dev: !isProd  })

app.use(bundler.middleware)

if (!isProd) {
  app.get('*', async (req, res) => {
    await bundler.bundle()
   
    res.sendFile('index.html')
 })
}

// ...

v1rtl avatar May 10 '21 10:05 v1rtl

👋 @talentlessguy

This sounds like a great idea and as it turns out klap already has a JS api. It is largely undocumented though, since the primary use for klap was from command line.

For the basic JS, I think something like this should work: (untested code below)

const { klap } = require("klap");
const isProd = process.env.NODE_ENV === "production";

const command = isProd ? "build" : "start";
const pkg = { name: "my-project-name", version: "5.4.0", };

(async () => {
  await klap(command, pkg);
})();

It would be great to formalize this API and document it to be able to create a middleware like you noted above. However, the actual middleware should be outside the core for klap to keep the scope limited for this project.

osdevisnot avatar May 13 '21 06:05 osdevisnot