4.1.20 • Published 29 days ago

@marko/vite v4.1.20

Weekly downloads
-
License
MIT
Repository
github
Last release
29 days ago

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.

4.1.20

29 days ago

4.1.18

3 months ago

4.1.19

3 months ago

4.1.11

6 months ago

4.1.16

4 months ago

4.1.17

3 months ago

4.1.12

6 months ago

4.1.13

4 months ago

4.1.14

4 months ago

4.1.15

4 months ago

4.1.9

7 months ago

4.1.10

7 months ago

4.1.8

7 months ago

4.1.7

7 months ago

4.1.4

8 months ago

4.1.6

8 months ago

4.1.5

8 months ago

4.1.3

9 months ago

4.1.2

9 months ago

3.0.1

1 year ago

3.0.0

1 year ago

4.0.4

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.0.3

1 year ago

4.0.2

1 year ago

3.1.3

1 year ago

3.1.2

1 year ago

3.1.1

1 year ago

3.1.0

1 year ago

3.1.6

1 year ago

3.1.5

1 year ago

3.1.4

1 year ago

2.4.9

1 year ago

4.1.0

1 year ago

4.1.1

12 months ago

2.4.1

2 years ago

2.4.3

2 years ago

2.4.2

2 years ago

2.4.5

2 years ago

2.4.4

2 years ago

2.4.7

1 year ago

2.4.6

1 year ago

2.4.8

1 year ago

2.4.0

2 years ago

2.3.13

2 years ago

2.3.14

2 years ago

2.3.12

2 years ago

2.3.11

2 years ago

2.3.10

2 years ago

2.3.9

2 years ago

2.3.6

2 years ago

2.3.5

2 years ago

2.3.8

2 years ago

2.3.7

2 years ago

2.0.3

2 years ago

2.3.4

2 years ago

2.3.2

3 years ago

2.3.3

2 years ago

2.3.0

3 years ago

2.2.15

3 years ago

2.3.1

3 years ago

2.2.13

3 years ago

2.2.14

3 years ago

2.2.11

3 years ago

2.2.12

3 years ago

2.2.10

3 years ago

2.2.9

3 years ago

2.2.8

3 years ago

2.2.1

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.5

3 years ago

2.2.4

3 years ago

2.2.7

3 years ago

2.2.6

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.2.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago