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

Programmable API #249

Open
talentlessguy opened this issue May 10, 2021 · 1 comment
Open

Programmable API #249

talentlessguy opened this issue May 10, 2021 · 1 comment

Comments

@talentlessguy
Copy link
Contributor

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')
 })
}

// ...
@osdevisnot
Copy link
Owner

osdevisnot commented May 13, 2021

👋 @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants