CSS, JS, and TS entries
An entry is a file used as the starting point for build processing. For example, when scripts.ts is the starting point for a script loaded from HTML, the build processes that file along with the code and CSS it imports.
You can declare CSS, JavaScript, and TypeScript as entries by referencing them with link or script in HTML. pluginSsg() processes the referenced files with Vite and replaces the URLs in the HTML with the built URLs. No additional plugin is needed.
Here, add regular JavaScript to the home page to display a greeting when a button is clicked. See Island for UI that uses React state.
Table of Contents
- Create a browser script
- Reference an entry from HTML
- Check entry output
- Load CSS from JavaScript
- Supported HTML references
- Public scripts
Create a browser script
Create src/assets/scripts.ts. This file runs in the browser, so it can use document and window.
document.querySelector("[data-greeting]")?.addEventListener("click", () => {
window.alert("Hello!")
})If you do not use TypeScript, write the same code in scripts.js. Keep browser logic in this file rather than at the top level of pages or layouts that generate static HTML.
Reference an entry from HTML
Reference CSS with link and JavaScript or TypeScript with script. The globals.css used here is created in CSS bundling.
import { Head } from "minista/head"
export default function Page() {
return (
<>
<Head>
<link rel="stylesheet" href="/src/layouts/globals.css" />
<script type="module" src="/src/assets/scripts.ts" />
</Head>
<h1>My Site</h1>
<button type="button" data-greeting>
Say hello
</button>
</>
)
}The leading / makes the path relative to the project root. For example, /src/assets/scripts.ts refers to src/assets/scripts.ts in your project. Set type="module" on the script element.
When referencing CSS this way, remove the import of the same file to avoid loading it twice. Move references to the layout for entries shared by all pages.
Check entry output
Running npm run build performs the following steps.
- Inspect the
hrefattributes oflinkelements and thesrcattributes ofscriptelements in the HTML generated from pages and Layout to find source file references. - Process the referenced files and their imported dependencies with Vite to output CSS and JavaScript. TypeScript is converted to JavaScript.
- Replace the source file URLs in the HTML with the output file URLs.
src/
├── assets/
│ └── scripts.ts
├── layouts/
│ ├── globals.css
│ └── index.tsx
└── pages/
└── index.tsxdist/
├── index.html
└── assets/
├── globals-[hash].css
└── scripts-[hash].jsIn the generated HTML, the references written in Head above become:
<link rel="stylesheet" href="/assets/globals-[hash].css" />
<script type="module" src="/assets/scripts-[hash].js"></script>[hash] represents a string determined by the file's content. The browser loads the output CSS and JavaScript, so you do not need to upload src/ to your host. Actual file names and splitting depend on configuration and dependencies, and URLs reflect Vite's base.
Load CSS from JavaScript
You can also import CSS from an entry.
import "../layouts/globals.css"
document.querySelector("[data-greeting]")?.addEventListener("click", () => {
window.alert("Hello!")
})With this method, remove the page's link. At build time, a stylesheet is added to pages that reference this script. Load it from other pages or a shared layout if they also use the styles.
Supported HTML references
In this guide, CSS is referenced through the href attribute of a link element, and JavaScript or TypeScript through the src attribute of a script element.
Processing applies to references that start with / and point to existing files within the project root. External URLs starting with https:// or //, and relative URLs in HTML such as ./scripts.js, are unchanged. By contrast, Vite resolves relative imports inside an entry, such as import "../layouts/globals.css", relative to the importing file.
Images can also be referenced directly through attributes such as src or srcset on img, but not every URL attribute in HTML is supported. For example, writing a source file path in the content attribute of an Open Graph meta element does not output the image or replace the URL. Use a URL obtained by importing the image or a public file URL instead.
See Image bundling for loading images and choosing references for Open Graph, and HTML assets in pluginSsg for the full list of supported elements and attributes.
Public scripts
Place existing CSS and JavaScript in public/ to use them without processing.
<Head>
<link rel="stylesheet" href="/css/legacy.css" />
<script src="/js/legacy.js" defer />
</Head>This example references public/css/legacy.css and public/js/legacy.js. Source entries and public files can be used on the same page.
Public files are copied to the output directory unchanged, without bundling. Their reference URLs in HTML reflect Vite's base. See public for file placement.
Next, Image optimization adds a plugin to adjust image sizes and formats.