Giter Club home page Giter Club logo

vite-plugin-bruh's Introduction

vite-plugin-bruh - A vite plugin to integrate with bruh

Install

npm i -D vite-plugin-bruh or use npm init bruh with the vite template to quickly get started.

Use

Example vite.config.mjs file:

import { defineConfig } from "vite"
import bruh from "vite-plugin-bruh"

export default defineConfig({
  plugins: [
    bruh({
      // Regex for the page render file extention
      // Defaults to /\.html\.(mjs|jsx?|tsx?)$/
      htmlRenderFileExtention,
      // Absolute path of the root pages directory
      // Defaults to vite's root
      root,
      // A (hopefully) temporary workaround of allowed node module imports
      // Adds to the default array of ["fs", "path", "crypto"]
      external
    })
  ]
})

This allows you to use the typical vite for development and vite build for production. vite-plugin-bruh will automatically allow you to prerender html files before vite sees them.

Here is an example project structure:

.
├── index.css
├── index.html.jsx
├── index.mjs
├── package-lock.json
├── package.json
└── vite.config.mjs

How it works

Upon a page request for /x in dev:

  1. The x.html.mjs (or x/index.html.mjs, ...js/jsx/ts/tsx) file is imported
  2. The default export is called and awaited
  3. The returned string is exposed to vite as if it were from x.html (or x/index.html)

At build time, all x.html.mjs files are automatically included as entry points (as if they were x.html)

If this is index.html.mjs:

export default async () =>
`<!doctype html>
<html>
  <head>
    ...
  </head>

  <body>
    ...
  </body>
</html>
`

Vite sees this as if index.html existed and contained:

<!doctype html>
<html>
  <head>
    ...
  </head>

  <body>
    ...
  </body>
</html>

During dev, vite will automatically and quickly reload the page as index.html.mjs and its imports are edited.

JSX/TSX

This plugin automatically includes jsx support for bruh, meaning that you can freely write jsx content in both render files (x.html.jsx) and hydrate files (x.jsx, what vite typically handles).

Current Caveats

Vite currently needs an explicit array of node core modules that it should allow to passthrough its module graph. You need to specify them in the external option, the defaults are just ["fs", "path", "crypto"] right now.

If you want to use import.meta.url, vite will currently give a (non URL!) absolute path that is "relative" to your vite root. The easiest workaround is to just do something like this:

import path from "path"

// A path relative to where the `vite` command is run
path.resolve("a/path/relative/to/the/vite/currentWorkingDirectory")

// Instead of what you would expect to work
new URL("a/path/relative/to/this/file", import.meta.url).pathname

vite-plugin-bruh's People

Contributors

wlib avatar

Watchers

 avatar  avatar  avatar  avatar

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.