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

Builtin Vite support for backend #760

Merged
merged 84 commits into from
Mar 31, 2023
Merged

Builtin Vite support for backend #760

merged 84 commits into from
Mar 31, 2023

Conversation

haikyuu
Copy link
Collaborator

@haikyuu haikyuu commented Oct 28, 2022

This PR adds support for using Vite as a bundler for backend and front-end Imba projects with no configuration.

What's new?

  • The vite plugin is now packaged with Imba. import imbaPlugin from 'imba/plugin'
  • Added an imba test command that is a wrapper of vitest with default configuration
  • You can now run a server with Imba and have Vite take care of bundling. Here is how that's done imba server.imba --vite. It automatically runs in watch mode and live reloads.
  • Paths configured in tsconfig.json are automatically picked. This is done by this vite plugin in order to match the built-in bundler behaviour.
  • Added support for iife imports using import head from "./head.imba?iife"
  • Added a import {setupVite} from 'imba/plugin' method that sets up vite middleware ++, this is the initial API:
import {setupVite} from 'imba/plugin'
const app = express()
# This looks definitely weird. So that will most probably change in the future
# This does the middleware setup for us, and the callback receives the path of the dist folder
await setupVite app, import.meta.env.MODE, do(dist)
  app.use compression()
  app.use serveStatic(dist, index: false)

app.use "/home", do(req, res)
  let html = <html lang="en">
  <head>
    <script type="module" src="./src/Home.imba">
  <body>
    <Home>
res.status(200).set("Content-Type": "text/html").end String html

Notes

  • Styles are auto injected in development (and then removed) to fix a flash of unstyled content
  • In production, each entry point that's detected (static src in a script tag: ./src/Home.imba in the example above) is treated as an entry point and is passed as such to Vite to perform the client build. The script tag is transformed to scripts and styles based on the manifest file.

@haikyuu haikyuu self-assigned this Oct 28, 2022
@haikyuu haikyuu marked this pull request as ready for review November 3, 2022 09:40
@haikyuu haikyuu requested a review from somebee November 10, 2022 05:20
@@ -0,0 +1,47 @@
# imba$stdlib=1
import np from 'path'
import * as Vite from "vite"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this end up importing vite whenever you run imba in node? Or do you know if it is removed by tree-shaking in esbuild?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply 🙂 There seem to be issues related to this

Running a script complains about

❯ imba index.imba
▲ [WARNING] "esbuild" should be marked as external for use with "require.resolve" [require-resolve-not-external]

    ../imba/packages/imba/node_modules/esbuild/lib/main.js:1718:36:
      1718 │   const libMainJS = require.resolve("esbuild");
           ╵                                     ~~~~~~~~~

✘ [ERROR] No loader is configured for ".node" files: ../imba/packages/imba/node_modules/fsevents/fsevents.node

    ../imba/packages/imba/node_modules/fsevents/fsevents.js:13:23:
      13 │ const Native = require("./fsevents.node");
         ╵                        ~~~~~~~~~~~~~~~~~

× failed with 1 errors

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed setupVite from imba directly into imba/plugin. It was indeed causing the issue :) Nice catch

@haikyuu
Copy link
Collaborator Author

haikyuu commented Jan 28, 2023

Rebased with master and fixed a bug where it fails if no tsconfig file is found (We use our own fork while waiting on this PR upstream aleclarson/vite-tsconfig-paths#97)

@haikyuu
Copy link
Collaborator Author

haikyuu commented Jan 28, 2023

Here is a list of things I consider important for complete Vite support

  • Make sure Vite and its other dependencies don't affect people using the built-in bundler
  • Fix bug in this PR in above comment
  • Switch to a single imba.config.js file for configuring the bundling of client, server and tests
  • Out of the box (no config needed) vite xp for client only, SSR and tests
  • Support imba serve (no argument (looks for index.html), and with html, imba or js/ts files
  • Support running scripts once. Now only --watch is supported when using --vite flag.
  • Add imba preview
  • Add imba build with no arguments (looks for index.html file)

The first two are mandatory to merge this PR. But the others can be done later in separate PRs

@haikyuu
Copy link
Collaborator Author

haikyuu commented Jan 31, 2023

rebased with master to have a green CI

@haikyuu haikyuu merged commit f11d948 into master Mar 31, 2023
@haikyuu haikyuu deleted the vite-iife branch March 31, 2023 12:53
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

Successfully merging this pull request may close these issues.

None yet

3 participants