Skip to content

marko-js/vite

Repository files navigation


@marko/vite
TypeScript Styled with prettier Build status NPM Version Downloads

A Marko plugin for Vite.

Installation

npm install @marko/vite

Example config

import { defineConfig } from "vite";
import marko from "@marko/vite";
export default defineConfig({
  plugins: [marko()],
});

Base paths

When deploying an application under a nested public path, use Vite's base option to specify a path to prefix all assets. This option can also be specified with the BASE_URL environment variable.

If the base path is not static, see the basePathVar option for handling more advanced cases.

Browser asset references

With @marko/vite when a static relative path is used for certain native tag attributes, the relative asset will be imported and processed by Vite.

As an example, with the following template, the logo.svg will be imported and processed as if it was a import at the root of the file.

<img src="./logo.svg">

// Would produce a Vite processed asset and update the src, eg with the following output
<img src="/assets/logo-TwEWmgMb.svg">

Most common image, media, and font filetypes are transformed automatically but some file types such as .js and .css files will not be. Generally these should be imported directly and not rendered as markup. To force the transformation of a path, add ?url to it. You can see the list of elements and their attributes which are processed here.

Linked Mode

By default this plugin operates in linked mode (you can disabled this by passing linked: false as an option). In linked mode the plugin automatically discovers all of the entry .marko files while compiling the server, and tells Vite which modules to load in the browser.

With this you do not create .html files for Vite, it's Marko all the way down! Scripts, styles and other content that would have been injected into the .html files is instead automatically injected into your .marko templates.

In this mode you must use the Vite SSR API.

Here's an example using express.

import { createServer } from "vite";

const app = express();
let loadTemplate;

if (process.env.NODE_ENV === "production") {
  // Use Vite's built asset in prod mode.
  loadTemplate = () => import("./dist");
} else {
  // Hookup the vite dev server.
  const vite = await createViteServer({
    server: { middlewareMode: true }
  });

  app.use(vite.middlewares);
  loadTemplate = () => vite.ssrLoadModule("./template.marko");
}

app.get("/", async (req, res) => {
  const template = (await loadTemplate()).default;
  // When the template is loaded, it will automaticall have `vite` assets inlined.
  template.render({ hello: "world" }, res);
);

app.listen(3000);

For a more real world setup check out our vite express example app.

Options

options.babelConfig

You can manually override Marko's Babel configuration by passing a babelConfig object to the @marko/vite plugin. If no babel configuration is specified, babel related config files will not be considered.

marko({
  babelConfig: {
    presets: ["@babel/preset-env"],
  },
});

options.runtimeId

In some cases you may want to embed multiple isolated copies of Marko on the page. Since Marko relies on some window properties to initialize this can cause issues. For example, by default Marko will read the server rendered hydration code from window.$components. In Marko you can change these window properties by rendering with { $global: { runtimeId: "MY_MARKO_RUNTIME_ID" } } as input on the server side.

This plugin exposes a runtimeId option produces output that automatically sets $global.runtimeId on the server side and initializes properly in the browser.

marko({ runtimeId: "MY_MARKO_RUNTIME_ID" });

options.linked

Set this to false to opt out of linked mode. When this is false, the plugin will only handle resolving and transforming .marko files.

options.basePathVar

Set this to variable/identifier which all asset base paths should be prefixed with. All asset paths used by Vite will either be relative (if possible) or prefixed with this identifier. The identifier must be defined as a string before any other server code executes.

First configure @marko/vite.

marko({ basePathVar: "__MY_ASSET_BASE_PATH__" });

Then ensure you set that variable at runtime.

globalThis.__MY_ASSET_BASE_PATH__ = getAssetUrl(); // Note this must end with a `/`.
require("./dist/index.mjs"); // load the built vite app.

Code of Conduct

This project adheres to the eBay Code of Conduct. By participating in this project you agree to abide by its terms.